[SCSI] mpt fusion: Adding DeviceResetCtx for internal Device reset frame
[safe/jmp/linux-2.6] / drivers / message / fusion / mptsas.c
1 /*
2  *  linux/drivers/message/fusion/mptsas.c
3  *      For use with LSI PCI chip/adapter(s)
4  *      running LSI Fusion MPT (Message Passing Technology) firmware.
5  *
6  *  Copyright (c) 1999-2008 LSI Corporation
7  *  (mailto:DL-MPTFusionLinux@lsi.com)
8  */
9 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
10 /*
11     This program is free software; you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation; version 2 of the License.
14
15     This program is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20     NO WARRANTY
21     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25     solely responsible for determining the appropriateness of using and
26     distributing the Program and assumes all risks associated with its
27     exercise of rights under this Agreement, including but not limited to
28     the risks and costs of program errors, damage to or loss of data,
29     programs or equipment, and unavailability or interruption of operations.
30
31     DISCLAIMER OF LIABILITY
32     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40     You should have received a copy of the GNU General Public License
41     along with this program; if not, write to the Free Software
42     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
43 */
44 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
45
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/init.h>
49 #include <linux/errno.h>
50 #include <linux/jiffies.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>        /* for mdelay */
53
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_transport_sas.h>
59 #include <scsi/scsi_dbg.h>
60
61 #include "mptbase.h"
62 #include "mptscsih.h"
63 #include "mptsas.h"
64
65
66 #define my_NAME         "Fusion MPT SAS Host driver"
67 #define my_VERSION      MPT_LINUX_VERSION_COMMON
68 #define MYNAM           "mptsas"
69
70 /*
71  * Reserved channel for integrated raid
72  */
73 #define MPTSAS_RAID_CHANNEL     1
74
75 MODULE_AUTHOR(MODULEAUTHOR);
76 MODULE_DESCRIPTION(my_NAME);
77 MODULE_LICENSE("GPL");
78 MODULE_VERSION(my_VERSION);
79
80 static int mpt_pt_clear;
81 module_param(mpt_pt_clear, int, 0);
82 MODULE_PARM_DESC(mpt_pt_clear,
83                 " Clear persistency table: enable=1  "
84                 "(default=MPTSCSIH_PT_CLEAR=0)");
85
86 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
87 #define MPTSAS_MAX_LUN (16895)
88 static int max_lun = MPTSAS_MAX_LUN;
89 module_param(max_lun, int, 0);
90 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
91
92 static u8       mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
93 static u8       mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
94 static u8       mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
95 static u8       mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
96 static u8       mptsasDeviceResetCtx = MPT_MAX_PROTOCOL_DRIVERS;
97
98 static void mptsas_hotplug_work(struct work_struct *work);
99
100 static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
101                                         MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
102 {
103         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
104             "---- IO UNIT PAGE 0 ------------\n", ioc->name));
105         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
106             ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
107         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
108             ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
109         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
110             ioc->name, phy_data->Port));
111         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
112             ioc->name, phy_data->PortFlags));
113         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
114             ioc->name, phy_data->PhyFlags));
115         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
116             ioc->name, phy_data->NegotiatedLinkRate));
117         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
118             "Controller PHY Device Info=0x%X\n", ioc->name,
119             le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
120         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
121             ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
122 }
123
124 static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
125 {
126         __le64 sas_address;
127
128         memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
129
130         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
131             "---- SAS PHY PAGE 0 ------------\n", ioc->name));
132         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
133             "Attached Device Handle=0x%X\n", ioc->name,
134             le16_to_cpu(pg0->AttachedDevHandle)));
135         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
136             ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
137         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
138             "Attached PHY Identifier=0x%X\n", ioc->name,
139             pg0->AttachedPhyIdentifier));
140         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
141             ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
142         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
143             ioc->name,  pg0->ProgrammedLinkRate));
144         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
145             ioc->name, pg0->ChangeCount));
146         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
147             ioc->name, le32_to_cpu(pg0->PhyInfo)));
148 }
149
150 static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
151 {
152         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
153             "---- SAS PHY PAGE 1 ------------\n", ioc->name));
154         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
155             ioc->name,  pg1->InvalidDwordCount));
156         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
157             "Running Disparity Error Count=0x%x\n", ioc->name,
158             pg1->RunningDisparityErrorCount));
159         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
160             "Loss Dword Synch Count=0x%x\n", ioc->name,
161             pg1->LossDwordSynchCount));
162         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
163             "PHY Reset Problem Count=0x%x\n\n", ioc->name,
164             pg1->PhyResetProblemCount));
165 }
166
167 static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
168 {
169         __le64 sas_address;
170
171         memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
172
173         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
174             "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
175         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
176             ioc->name, le16_to_cpu(pg0->DevHandle)));
177         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
178             ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
179         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
180             ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
181         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
182             ioc->name, le16_to_cpu(pg0->Slot)));
183         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
184             ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
185         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
186             ioc->name, pg0->TargetID));
187         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
188             ioc->name, pg0->Bus));
189         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
190             ioc->name, pg0->PhyNum));
191         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
192             ioc->name, le16_to_cpu(pg0->AccessStatus)));
193         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
194             ioc->name, le32_to_cpu(pg0->DeviceInfo)));
195         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
196             ioc->name, le16_to_cpu(pg0->Flags)));
197         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
198             ioc->name, pg0->PhysicalPort));
199 }
200
201 static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
202 {
203         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
204             "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
205         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
206             ioc->name, pg1->PhysicalPort));
207         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
208             ioc->name, pg1->PhyIdentifier));
209         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
210             ioc->name, pg1->NegotiatedLinkRate));
211         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
212             ioc->name, pg1->ProgrammedLinkRate));
213         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
214             ioc->name, pg1->HwLinkRate));
215         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
216             ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
217         dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
218             "Attached Device Handle=0x%X\n\n", ioc->name,
219             le16_to_cpu(pg1->AttachedDevHandle)));
220 }
221
222 static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
223 {
224         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
225         return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
226 }
227
228 static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
229 {
230         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
231         return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
232 }
233
234 static struct mptsas_portinfo *
235 mptsas_get_hba_portinfo(MPT_ADAPTER *ioc)
236 {
237         struct list_head        *head = &ioc->sas_topology;
238         struct mptsas_portinfo  *pi = NULL;
239
240         /* always the first entry on sas_topology list */
241
242         if (!list_empty(head))
243                 pi = list_entry(head->next, struct mptsas_portinfo, list);
244
245         return pi;
246 }
247
248 /*
249  * mptsas_find_portinfo_by_handle
250  *
251  * This function should be called with the sas_topology_mutex already held
252  */
253 static struct mptsas_portinfo *
254 mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
255 {
256         struct mptsas_portinfo *port_info, *rc=NULL;
257         int i;
258
259         list_for_each_entry(port_info, &ioc->sas_topology, list)
260                 for (i = 0; i < port_info->num_phys; i++)
261                         if (port_info->phy_info[i].identify.handle == handle) {
262                                 rc = port_info;
263                                 goto out;
264                         }
265  out:
266         return rc;
267 }
268
269 /*
270  * Returns true if there is a scsi end device
271  */
272 static inline int
273 mptsas_is_end_device(struct mptsas_devinfo * attached)
274 {
275         if ((attached->sas_address) &&
276             (attached->device_info &
277             MPI_SAS_DEVICE_INFO_END_DEVICE) &&
278             ((attached->device_info &
279             MPI_SAS_DEVICE_INFO_SSP_TARGET) |
280             (attached->device_info &
281             MPI_SAS_DEVICE_INFO_STP_TARGET) |
282             (attached->device_info &
283             MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
284                 return 1;
285         else
286                 return 0;
287 }
288
289 /* no mutex */
290 static void
291 mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
292 {
293         struct mptsas_portinfo *port_info;
294         struct mptsas_phyinfo *phy_info;
295         u8      i;
296
297         if (!port_details)
298                 return;
299
300         port_info = port_details->port_info;
301         phy_info = port_info->phy_info;
302
303         dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
304             "bitmask=0x%016llX\n", ioc->name, __func__, port_details,
305             port_details->num_phys, (unsigned long long)
306             port_details->phy_bitmask));
307
308         for (i = 0; i < port_info->num_phys; i++, phy_info++) {
309                 if(phy_info->port_details != port_details)
310                         continue;
311                 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
312                 phy_info->port_details = NULL;
313         }
314         kfree(port_details);
315 }
316
317 static inline struct sas_rphy *
318 mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
319 {
320         if (phy_info->port_details)
321                 return phy_info->port_details->rphy;
322         else
323                 return NULL;
324 }
325
326 static inline void
327 mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
328 {
329         if (phy_info->port_details) {
330                 phy_info->port_details->rphy = rphy;
331                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
332                     ioc->name, rphy));
333         }
334
335         if (rphy) {
336                 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
337                     &rphy->dev, MYIOC_s_FMT "add:", ioc->name));
338                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
339                     ioc->name, rphy, rphy->dev.release));
340         }
341 }
342
343 static inline struct sas_port *
344 mptsas_get_port(struct mptsas_phyinfo *phy_info)
345 {
346         if (phy_info->port_details)
347                 return phy_info->port_details->port;
348         else
349                 return NULL;
350 }
351
352 static inline void
353 mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
354 {
355         if (phy_info->port_details)
356                 phy_info->port_details->port = port;
357
358         if (port) {
359                 dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
360                     &port->dev, MYIOC_s_FMT "add:", ioc->name));
361                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
362                     ioc->name, port, port->dev.release));
363         }
364 }
365
366 static inline struct scsi_target *
367 mptsas_get_starget(struct mptsas_phyinfo *phy_info)
368 {
369         if (phy_info->port_details)
370                 return phy_info->port_details->starget;
371         else
372                 return NULL;
373 }
374
375 static inline void
376 mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
377 starget)
378 {
379         if (phy_info->port_details)
380                 phy_info->port_details->starget = starget;
381 }
382
383
384 /*
385  * mptsas_setup_wide_ports
386  *
387  * Updates for new and existing narrow/wide port configuration
388  * in the sas_topology
389  */
390 static void
391 mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
392 {
393         struct mptsas_portinfo_details * port_details;
394         struct mptsas_phyinfo *phy_info, *phy_info_cmp;
395         u64     sas_address;
396         int     i, j;
397
398         mutex_lock(&ioc->sas_topology_mutex);
399
400         phy_info = port_info->phy_info;
401         for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
402                 if (phy_info->attached.handle)
403                         continue;
404                 port_details = phy_info->port_details;
405                 if (!port_details)
406                         continue;
407                 if (port_details->num_phys < 2)
408                         continue;
409                 /*
410                  * Removing a phy from a port, letting the last
411                  * phy be removed by firmware events.
412                  */
413                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
414                     "%s: [%p]: deleting phy = %d\n",
415                     ioc->name, __func__, port_details, i));
416                 port_details->num_phys--;
417                 port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
418                 memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
419                 sas_port_delete_phy(port_details->port, phy_info->phy);
420                 phy_info->port_details = NULL;
421         }
422
423         /*
424          * Populate and refresh the tree
425          */
426         phy_info = port_info->phy_info;
427         for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
428                 sas_address = phy_info->attached.sas_address;
429                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
430                     ioc->name, i, (unsigned long long)sas_address));
431                 if (!sas_address)
432                         continue;
433                 port_details = phy_info->port_details;
434                 /*
435                  * Forming a port
436                  */
437                 if (!port_details) {
438                         port_details = kzalloc(sizeof(*port_details),
439                                 GFP_KERNEL);
440                         if (!port_details)
441                                 goto out;
442                         port_details->num_phys = 1;
443                         port_details->port_info = port_info;
444                         if (phy_info->phy_id < 64 )
445                                 port_details->phy_bitmask |=
446                                     (1 << phy_info->phy_id);
447                         phy_info->sas_port_add_phy=1;
448                         dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
449                             "phy_id=%d sas_address=0x%018llX\n",
450                             ioc->name, i, (unsigned long long)sas_address));
451                         phy_info->port_details = port_details;
452                 }
453
454                 if (i == port_info->num_phys - 1)
455                         continue;
456                 phy_info_cmp = &port_info->phy_info[i + 1];
457                 for (j = i + 1 ; j < port_info->num_phys ; j++,
458                     phy_info_cmp++) {
459                         if (!phy_info_cmp->attached.sas_address)
460                                 continue;
461                         if (sas_address != phy_info_cmp->attached.sas_address)
462                                 continue;
463                         if (phy_info_cmp->port_details == port_details )
464                                 continue;
465                         dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
466                             "\t\tphy_id=%d sas_address=0x%018llX\n",
467                             ioc->name, j, (unsigned long long)
468                             phy_info_cmp->attached.sas_address));
469                         if (phy_info_cmp->port_details) {
470                                 port_details->rphy =
471                                     mptsas_get_rphy(phy_info_cmp);
472                                 port_details->port =
473                                     mptsas_get_port(phy_info_cmp);
474                                 port_details->starget =
475                                     mptsas_get_starget(phy_info_cmp);
476                                 port_details->num_phys =
477                                         phy_info_cmp->port_details->num_phys;
478                                 if (!phy_info_cmp->port_details->num_phys)
479                                         kfree(phy_info_cmp->port_details);
480                         } else
481                                 phy_info_cmp->sas_port_add_phy=1;
482                         /*
483                          * Adding a phy to a port
484                          */
485                         phy_info_cmp->port_details = port_details;
486                         if (phy_info_cmp->phy_id < 64 )
487                                 port_details->phy_bitmask |=
488                                 (1 << phy_info_cmp->phy_id);
489                         port_details->num_phys++;
490                 }
491         }
492
493  out:
494
495         for (i = 0; i < port_info->num_phys; i++) {
496                 port_details = port_info->phy_info[i].port_details;
497                 if (!port_details)
498                         continue;
499                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
500                     "%s: [%p]: phy_id=%02d num_phys=%02d "
501                     "bitmask=0x%016llX\n", ioc->name, __func__,
502                     port_details, i, port_details->num_phys,
503                     (unsigned long long)port_details->phy_bitmask));
504                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
505                     ioc->name, port_details->port, port_details->rphy));
506         }
507         dsaswideprintk(ioc, printk("\n"));
508         mutex_unlock(&ioc->sas_topology_mutex);
509 }
510
511 /**
512  * csmisas_find_vtarget
513  *
514  * @ioc
515  * @volume_id
516  * @volume_bus
517  *
518  **/
519 static VirtTarget *
520 mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
521 {
522         struct scsi_device              *sdev;
523         VirtDevice                      *vdevice;
524         VirtTarget                      *vtarget = NULL;
525
526         shost_for_each_device(sdev, ioc->sh) {
527                 vdevice = sdev->hostdata;
528                 if ((vdevice == NULL) ||
529                         (vdevice->vtarget == NULL))
530                         continue;
531                 if (vdevice->vtarget->id == id &&
532                         vdevice->vtarget->channel == channel)
533                         vtarget = vdevice->vtarget;
534         }
535         return vtarget;
536 }
537
538 /**
539  * mptsas_target_reset
540  *
541  * Issues TARGET_RESET to end device using handshaking method
542  *
543  * @ioc
544  * @channel
545  * @id
546  *
547  * Returns (1) success
548  *         (0) failure
549  *
550  **/
551 static int
552 mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
553 {
554         MPT_FRAME_HDR   *mf;
555         SCSITaskMgmt_t  *pScsiTm;
556
557         mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
558         if (mf == NULL) {
559                 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
560                         "%s, no msg frames @%d!!\n",
561                         ioc->name, __func__, __LINE__));
562                 return 0;
563         }
564
565         /* Format the Request
566          */
567         pScsiTm = (SCSITaskMgmt_t *) mf;
568         memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
569         pScsiTm->TargetID = id;
570         pScsiTm->Bus = channel;
571         pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
572         pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
573         pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
574
575         DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
576
577         mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
578
579         return 1;
580 }
581
582 /**
583  * mptsas_target_reset_queue
584  *
585  * Receive request for TARGET_RESET after recieving an firmware
586  * event NOT_RESPONDING_EVENT, then put command in link list
587  * and queue if task_queue already in use.
588  *
589  * @ioc
590  * @sas_event_data
591  *
592  **/
593 static void
594 mptsas_target_reset_queue(MPT_ADAPTER *ioc,
595     EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
596 {
597         MPT_SCSI_HOST   *hd = shost_priv(ioc->sh);
598         VirtTarget *vtarget = NULL;
599         struct mptsas_target_reset_event *target_reset_list;
600         u8              id, channel;
601
602         id = sas_event_data->TargetID;
603         channel = sas_event_data->Bus;
604
605         if (!(vtarget = mptsas_find_vtarget(ioc, channel, id)))
606                 return;
607
608         vtarget->deleted = 1; /* block IO */
609
610         target_reset_list = kzalloc(sizeof(*target_reset_list),
611             GFP_ATOMIC);
612         if (!target_reset_list) {
613                 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
614                         "%s, failed to allocate mem @%d..!!\n",
615                         ioc->name, __func__, __LINE__));
616                 return;
617         }
618
619         memcpy(&target_reset_list->sas_event_data, sas_event_data,
620                 sizeof(*sas_event_data));
621         list_add_tail(&target_reset_list->list, &hd->target_reset_list);
622
623         target_reset_list->time_count = jiffies;
624
625         if (mptsas_target_reset(ioc, channel, id)) {
626                 target_reset_list->target_reset_issued = 1;
627         }
628 }
629
630 /**
631  *      mptsas_taskmgmt_complete - Completion for TARGET_RESET after
632  *      NOT_RESPONDING_EVENT, enable work queue to finish off removing device
633  *      from upper layers. then send next TARGET_RESET in the queue.
634  *      @ioc: Pointer to MPT_ADAPTER structure
635  *
636  **/
637 static int
638 mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
639 {
640         MPT_SCSI_HOST   *hd = shost_priv(ioc->sh);
641         struct list_head *head = &hd->target_reset_list;
642         struct mptsas_hotplug_event *ev;
643         EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
644         u8              id, channel;
645         __le64          sas_address;
646         struct mptsas_target_reset_event        *target_reset_list;
647         SCSITaskMgmtReply_t *pScsiTmReply;
648
649         dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed: "
650             "(mf = %p, mr = %p)\n", ioc->name, mf, mr));
651
652         pScsiTmReply = (SCSITaskMgmtReply_t *)mr;
653         if (pScsiTmReply) {
654                 dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
655                     "\tTaskMgmt completed: fw_channel = %d, fw_id = %d,\n"
656                     "\ttask_type = 0x%02X, iocstatus = 0x%04X "
657                     "loginfo = 0x%08X,\n\tresponse_code = 0x%02X, "
658                     "term_cmnds = %d\n", ioc->name,
659                     pScsiTmReply->Bus, pScsiTmReply->TargetID,
660                     pScsiTmReply->TaskType,
661                     le16_to_cpu(pScsiTmReply->IOCStatus),
662                     le32_to_cpu(pScsiTmReply->IOCLogInfo),
663                     pScsiTmReply->ResponseCode,
664                     le32_to_cpu(pScsiTmReply->TerminationCount)));
665
666                 if (pScsiTmReply->ResponseCode)
667                         mptscsih_taskmgmt_response_code(ioc,
668                         pScsiTmReply->ResponseCode);
669         }
670
671         if (pScsiTmReply && (pScsiTmReply->TaskType ==
672             MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK || pScsiTmReply->TaskType ==
673              MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET)) {
674                 ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
675                 ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
676                 memcpy(ioc->taskmgmt_cmds.reply, mr,
677                     min(MPT_DEFAULT_FRAME_SIZE, 4 * mr->u.reply.MsgLength));
678                 if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_PENDING) {
679                         ioc->taskmgmt_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
680                         complete(&ioc->taskmgmt_cmds.done);
681                         return 1;
682                 }
683                 return 0;
684         }
685
686         mpt_clear_taskmgmt_in_progress_flag(ioc);
687
688         if (list_empty(head))
689                 return 1;
690
691         target_reset_list = list_entry(head->next,
692             struct mptsas_target_reset_event, list);
693
694         dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
695             "TaskMgmt: completed (%d seconds)\n",
696             ioc->name, jiffies_to_msecs(jiffies -
697             target_reset_list->time_count)/1000));
698
699         sas_event_data = &target_reset_list->sas_event_data;
700         id = pScsiTmReply->TargetID;
701         channel = pScsiTmReply->Bus;
702         target_reset_list->time_count = jiffies;
703
704         /*
705          * retry target reset
706          */
707         if (!target_reset_list->target_reset_issued) {
708                 if (mptsas_target_reset(ioc, channel, id))
709                         target_reset_list->target_reset_issued = 1;
710                 return 1;
711         }
712
713         /*
714          * enable work queue to remove device from upper layers
715          */
716         list_del(&target_reset_list->list);
717
718         ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
719         if (!ev) {
720                 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
721                     ioc->name,__func__, __LINE__));
722                 return 0;
723         }
724
725         INIT_WORK(&ev->work, mptsas_hotplug_work);
726         ev->ioc = ioc;
727         ev->handle = le16_to_cpu(sas_event_data->DevHandle);
728         ev->parent_handle =
729             le16_to_cpu(sas_event_data->ParentDevHandle);
730         ev->channel = channel;
731         ev->id =id;
732         ev->phy_id = sas_event_data->PhyNum;
733         memcpy(&sas_address, &sas_event_data->SASAddress,
734             sizeof(__le64));
735         ev->sas_address = le64_to_cpu(sas_address);
736         ev->device_info = le32_to_cpu(sas_event_data->DeviceInfo);
737         ev->event_type = MPTSAS_DEL_DEVICE;
738         schedule_work(&ev->work);
739         kfree(target_reset_list);
740
741
742         /*
743          * issue target reset to next device in the queue
744          */
745
746         head = &hd->target_reset_list;
747         if (list_empty(head))
748                 return 1;
749
750         target_reset_list = list_entry(head->next, struct mptsas_target_reset_event,
751             list);
752
753         id = target_reset_list->sas_event_data.TargetID;
754         channel = target_reset_list->sas_event_data.Bus;
755         target_reset_list->time_count = jiffies;
756
757         if (mptsas_target_reset(ioc, channel, id))
758                 target_reset_list->target_reset_issued = 1;
759
760         return 1;
761 }
762
763 /**
764  * mptscsih_ioc_reset
765  *
766  * @ioc
767  * @reset_phase
768  *
769  **/
770 static int
771 mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
772 {
773         MPT_SCSI_HOST   *hd;
774         struct mptsas_target_reset_event *target_reset_list, *n;
775         int rc;
776
777         rc = mptscsih_ioc_reset(ioc, reset_phase);
778
779         if (ioc->bus_type != SAS)
780                 goto out;
781
782         if (reset_phase != MPT_IOC_POST_RESET)
783                 goto out;
784
785         if (!ioc->sh || !ioc->sh->hostdata)
786                 goto out;
787         hd = shost_priv(ioc->sh);
788         if (!hd->ioc)
789                 goto out;
790
791         if (list_empty(&hd->target_reset_list))
792                 goto out;
793
794         /* flush the target_reset_list */
795         list_for_each_entry_safe(target_reset_list, n,
796             &hd->target_reset_list, list) {
797                 list_del(&target_reset_list->list);
798                 kfree(target_reset_list);
799         }
800
801  out:
802         return rc;
803 }
804
805 static int
806 mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
807                 u32 form, u32 form_specific)
808 {
809         ConfigExtendedPageHeader_t hdr;
810         CONFIGPARMS cfg;
811         SasEnclosurePage0_t *buffer;
812         dma_addr_t dma_handle;
813         int error;
814         __le64 le_identifier;
815
816         memset(&hdr, 0, sizeof(hdr));
817         hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
818         hdr.PageNumber = 0;
819         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
820         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
821
822         cfg.cfghdr.ehdr = &hdr;
823         cfg.physAddr = -1;
824         cfg.pageAddr = form + form_specific;
825         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
826         cfg.dir = 0;    /* read */
827         cfg.timeout = 10;
828
829         error = mpt_config(ioc, &cfg);
830         if (error)
831                 goto out;
832         if (!hdr.ExtPageLength) {
833                 error = -ENXIO;
834                 goto out;
835         }
836
837         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
838                         &dma_handle);
839         if (!buffer) {
840                 error = -ENOMEM;
841                 goto out;
842         }
843
844         cfg.physAddr = dma_handle;
845         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
846
847         error = mpt_config(ioc, &cfg);
848         if (error)
849                 goto out_free_consistent;
850
851         /* save config data */
852         memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
853         enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
854         enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
855         enclosure->flags = le16_to_cpu(buffer->Flags);
856         enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
857         enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
858         enclosure->start_id = buffer->StartTargetID;
859         enclosure->start_channel = buffer->StartBus;
860         enclosure->sep_id = buffer->SEPTargetID;
861         enclosure->sep_channel = buffer->SEPBus;
862
863  out_free_consistent:
864         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
865                             buffer, dma_handle);
866  out:
867         return error;
868 }
869
870 static int
871 mptsas_slave_configure(struct scsi_device *sdev)
872 {
873
874         if (sdev->channel == MPTSAS_RAID_CHANNEL)
875                 goto out;
876
877         sas_read_port_mode_page(sdev);
878
879  out:
880         return mptscsih_slave_configure(sdev);
881 }
882
883 static int
884 mptsas_target_alloc(struct scsi_target *starget)
885 {
886         struct Scsi_Host *host = dev_to_shost(&starget->dev);
887         MPT_SCSI_HOST           *hd = shost_priv(host);
888         VirtTarget              *vtarget;
889         u8                      id, channel;
890         struct sas_rphy         *rphy;
891         struct mptsas_portinfo  *p;
892         int                      i;
893         MPT_ADAPTER             *ioc = hd->ioc;
894
895         vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
896         if (!vtarget)
897                 return -ENOMEM;
898
899         vtarget->starget = starget;
900         vtarget->ioc_id = ioc->id;
901         vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
902         id = starget->id;
903         channel = 0;
904
905         /*
906          * RAID volumes placed beyond the last expected port.
907          */
908         if (starget->channel == MPTSAS_RAID_CHANNEL) {
909                 for (i=0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
910                         if (id == ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
911                                 channel = ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
912                 goto out;
913         }
914
915         rphy = dev_to_rphy(starget->dev.parent);
916         mutex_lock(&ioc->sas_topology_mutex);
917         list_for_each_entry(p, &ioc->sas_topology, list) {
918                 for (i = 0; i < p->num_phys; i++) {
919                         if (p->phy_info[i].attached.sas_address !=
920                                         rphy->identify.sas_address)
921                                 continue;
922                         id = p->phy_info[i].attached.id;
923                         channel = p->phy_info[i].attached.channel;
924                         mptsas_set_starget(&p->phy_info[i], starget);
925
926                         /*
927                          * Exposing hidden raid components
928                          */
929                         if (mptscsih_is_phys_disk(ioc, channel, id)) {
930                                 id = mptscsih_raid_id_to_num(ioc,
931                                                 channel, id);
932                                 vtarget->tflags |=
933                                     MPT_TARGET_FLAGS_RAID_COMPONENT;
934                                 p->phy_info[i].attached.phys_disk_num = id;
935                         }
936                         mutex_unlock(&ioc->sas_topology_mutex);
937                         goto out;
938                 }
939         }
940         mutex_unlock(&ioc->sas_topology_mutex);
941
942         kfree(vtarget);
943         return -ENXIO;
944
945  out:
946         vtarget->id = id;
947         vtarget->channel = channel;
948         starget->hostdata = vtarget;
949         return 0;
950 }
951
952 static void
953 mptsas_target_destroy(struct scsi_target *starget)
954 {
955         struct Scsi_Host *host = dev_to_shost(&starget->dev);
956         MPT_SCSI_HOST           *hd = shost_priv(host);
957         struct sas_rphy         *rphy;
958         struct mptsas_portinfo  *p;
959         int                      i;
960         MPT_ADAPTER *ioc = hd->ioc;
961
962         if (!starget->hostdata)
963                 return;
964
965         if (starget->channel == MPTSAS_RAID_CHANNEL)
966                 goto out;
967
968         rphy = dev_to_rphy(starget->dev.parent);
969         list_for_each_entry(p, &ioc->sas_topology, list) {
970                 for (i = 0; i < p->num_phys; i++) {
971                         if (p->phy_info[i].attached.sas_address !=
972                                         rphy->identify.sas_address)
973                                 continue;
974                         mptsas_set_starget(&p->phy_info[i], NULL);
975                         goto out;
976                 }
977         }
978
979  out:
980         kfree(starget->hostdata);
981         starget->hostdata = NULL;
982 }
983
984
985 static int
986 mptsas_slave_alloc(struct scsi_device *sdev)
987 {
988         struct Scsi_Host        *host = sdev->host;
989         MPT_SCSI_HOST           *hd = shost_priv(host);
990         struct sas_rphy         *rphy;
991         struct mptsas_portinfo  *p;
992         VirtDevice              *vdevice;
993         struct scsi_target      *starget;
994         int                     i;
995         MPT_ADAPTER *ioc = hd->ioc;
996
997         vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
998         if (!vdevice) {
999                 printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
1000                                 ioc->name, sizeof(VirtDevice));
1001                 return -ENOMEM;
1002         }
1003         starget = scsi_target(sdev);
1004         vdevice->vtarget = starget->hostdata;
1005
1006         if (sdev->channel == MPTSAS_RAID_CHANNEL)
1007                 goto out;
1008
1009         rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
1010         mutex_lock(&ioc->sas_topology_mutex);
1011         list_for_each_entry(p, &ioc->sas_topology, list) {
1012                 for (i = 0; i < p->num_phys; i++) {
1013                         if (p->phy_info[i].attached.sas_address !=
1014                                         rphy->identify.sas_address)
1015                                 continue;
1016                         vdevice->lun = sdev->lun;
1017                         /*
1018                          * Exposing hidden raid components
1019                          */
1020                         if (mptscsih_is_phys_disk(ioc,
1021                             p->phy_info[i].attached.channel,
1022                             p->phy_info[i].attached.id))
1023                                 sdev->no_uld_attach = 1;
1024                         mutex_unlock(&ioc->sas_topology_mutex);
1025                         goto out;
1026                 }
1027         }
1028         mutex_unlock(&ioc->sas_topology_mutex);
1029
1030         kfree(vdevice);
1031         return -ENXIO;
1032
1033  out:
1034         vdevice->vtarget->num_luns++;
1035         sdev->hostdata = vdevice;
1036         return 0;
1037 }
1038
1039 static int
1040 mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
1041 {
1042         MPT_SCSI_HOST   *hd;
1043         MPT_ADAPTER     *ioc;
1044         VirtDevice      *vdevice = SCpnt->device->hostdata;
1045
1046         if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
1047                 SCpnt->result = DID_NO_CONNECT << 16;
1048                 done(SCpnt);
1049                 return 0;
1050         }
1051
1052         hd = shost_priv(SCpnt->device->host);
1053         ioc = hd->ioc;
1054
1055         if (ioc->sas_discovery_quiesce_io)
1056                 return SCSI_MLQUEUE_HOST_BUSY;
1057
1058 //      scsi_print_command(SCpnt);
1059
1060         return mptscsih_qcmd(SCpnt,done);
1061 }
1062
1063
1064 static struct scsi_host_template mptsas_driver_template = {
1065         .module                         = THIS_MODULE,
1066         .proc_name                      = "mptsas",
1067         .proc_info                      = mptscsih_proc_info,
1068         .name                           = "MPT SPI Host",
1069         .info                           = mptscsih_info,
1070         .queuecommand                   = mptsas_qcmd,
1071         .target_alloc                   = mptsas_target_alloc,
1072         .slave_alloc                    = mptsas_slave_alloc,
1073         .slave_configure                = mptsas_slave_configure,
1074         .target_destroy                 = mptsas_target_destroy,
1075         .slave_destroy                  = mptscsih_slave_destroy,
1076         .change_queue_depth             = mptscsih_change_queue_depth,
1077         .eh_abort_handler               = mptscsih_abort,
1078         .eh_device_reset_handler        = mptscsih_dev_reset,
1079         .eh_bus_reset_handler           = mptscsih_bus_reset,
1080         .eh_host_reset_handler          = mptscsih_host_reset,
1081         .bios_param                     = mptscsih_bios_param,
1082         .can_queue                      = MPT_FC_CAN_QUEUE,
1083         .this_id                        = -1,
1084         .sg_tablesize                   = MPT_SCSI_SG_DEPTH,
1085         .max_sectors                    = 8192,
1086         .cmd_per_lun                    = 7,
1087         .use_clustering                 = ENABLE_CLUSTERING,
1088         .shost_attrs                    = mptscsih_host_attrs,
1089 };
1090
1091 static int mptsas_get_linkerrors(struct sas_phy *phy)
1092 {
1093         MPT_ADAPTER *ioc = phy_to_ioc(phy);
1094         ConfigExtendedPageHeader_t hdr;
1095         CONFIGPARMS cfg;
1096         SasPhyPage1_t *buffer;
1097         dma_addr_t dma_handle;
1098         int error;
1099
1100         /* FIXME: only have link errors on local phys */
1101         if (!scsi_is_sas_phy_local(phy))
1102                 return -EINVAL;
1103
1104         hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
1105         hdr.ExtPageLength = 0;
1106         hdr.PageNumber = 1 /* page number 1*/;
1107         hdr.Reserved1 = 0;
1108         hdr.Reserved2 = 0;
1109         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1110         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1111
1112         cfg.cfghdr.ehdr = &hdr;
1113         cfg.physAddr = -1;
1114         cfg.pageAddr = phy->identify.phy_identifier;
1115         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1116         cfg.dir = 0;    /* read */
1117         cfg.timeout = 10;
1118
1119         error = mpt_config(ioc, &cfg);
1120         if (error)
1121                 return error;
1122         if (!hdr.ExtPageLength)
1123                 return -ENXIO;
1124
1125         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1126                                       &dma_handle);
1127         if (!buffer)
1128                 return -ENOMEM;
1129
1130         cfg.physAddr = dma_handle;
1131         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1132
1133         error = mpt_config(ioc, &cfg);
1134         if (error)
1135                 goto out_free_consistent;
1136
1137         mptsas_print_phy_pg1(ioc, buffer);
1138
1139         phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
1140         phy->running_disparity_error_count =
1141                 le32_to_cpu(buffer->RunningDisparityErrorCount);
1142         phy->loss_of_dword_sync_count =
1143                 le32_to_cpu(buffer->LossDwordSynchCount);
1144         phy->phy_reset_problem_count =
1145                 le32_to_cpu(buffer->PhyResetProblemCount);
1146
1147  out_free_consistent:
1148         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1149                             buffer, dma_handle);
1150         return error;
1151 }
1152
1153 static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
1154                 MPT_FRAME_HDR *reply)
1155 {
1156         ioc->sas_mgmt.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
1157         if (reply != NULL) {
1158                 ioc->sas_mgmt.status |= MPT_MGMT_STATUS_RF_VALID;
1159                 memcpy(ioc->sas_mgmt.reply, reply,
1160                     min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
1161         }
1162         complete(&ioc->sas_mgmt.done);
1163         return 1;
1164 }
1165
1166 static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
1167 {
1168         MPT_ADAPTER *ioc = phy_to_ioc(phy);
1169         SasIoUnitControlRequest_t *req;
1170         SasIoUnitControlReply_t *reply;
1171         MPT_FRAME_HDR *mf;
1172         MPIHeader_t *hdr;
1173         unsigned long timeleft;
1174         int error = -ERESTARTSYS;
1175
1176         /* FIXME: fusion doesn't allow non-local phy reset */
1177         if (!scsi_is_sas_phy_local(phy))
1178                 return -EINVAL;
1179
1180         /* not implemented for expanders */
1181         if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
1182                 return -ENXIO;
1183
1184         if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
1185                 goto out;
1186
1187         mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1188         if (!mf) {
1189                 error = -ENOMEM;
1190                 goto out_unlock;
1191         }
1192
1193         hdr = (MPIHeader_t *) mf;
1194         req = (SasIoUnitControlRequest_t *)mf;
1195         memset(req, 0, sizeof(SasIoUnitControlRequest_t));
1196         req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
1197         req->MsgContext = hdr->MsgContext;
1198         req->Operation = hard_reset ?
1199                 MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
1200         req->PhyNum = phy->identify.phy_identifier;
1201
1202         mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1203
1204         timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
1205                         10 * HZ);
1206         if (!timeleft) {
1207                 /* On timeout reset the board */
1208                 mpt_free_msg_frame(ioc, mf);
1209                 mpt_HardResetHandler(ioc, CAN_SLEEP);
1210                 error = -ETIMEDOUT;
1211                 goto out_unlock;
1212         }
1213
1214         /* a reply frame is expected */
1215         if ((ioc->sas_mgmt.status &
1216             MPT_MGMT_STATUS_RF_VALID) == 0) {
1217                 error = -ENXIO;
1218                 goto out_unlock;
1219         }
1220
1221         /* process the completed Reply Message Frame */
1222         reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
1223         if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
1224                 printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
1225                     ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
1226                 error = -ENXIO;
1227                 goto out_unlock;
1228         }
1229
1230         error = 0;
1231
1232  out_unlock:
1233         mutex_unlock(&ioc->sas_mgmt.mutex);
1234  out:
1235         return error;
1236 }
1237
1238 static int
1239 mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
1240 {
1241         MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1242         int i, error;
1243         struct mptsas_portinfo *p;
1244         struct mptsas_enclosure enclosure_info;
1245         u64 enclosure_handle;
1246
1247         mutex_lock(&ioc->sas_topology_mutex);
1248         list_for_each_entry(p, &ioc->sas_topology, list) {
1249                 for (i = 0; i < p->num_phys; i++) {
1250                         if (p->phy_info[i].attached.sas_address ==
1251                             rphy->identify.sas_address) {
1252                                 enclosure_handle = p->phy_info[i].
1253                                         attached.handle_enclosure;
1254                                 goto found_info;
1255                         }
1256                 }
1257         }
1258         mutex_unlock(&ioc->sas_topology_mutex);
1259         return -ENXIO;
1260
1261  found_info:
1262         mutex_unlock(&ioc->sas_topology_mutex);
1263         memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
1264         error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
1265                         (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
1266                          MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
1267         if (!error)
1268                 *identifier = enclosure_info.enclosure_logical_id;
1269         return error;
1270 }
1271
1272 static int
1273 mptsas_get_bay_identifier(struct sas_rphy *rphy)
1274 {
1275         MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
1276         struct mptsas_portinfo *p;
1277         int i, rc;
1278
1279         mutex_lock(&ioc->sas_topology_mutex);
1280         list_for_each_entry(p, &ioc->sas_topology, list) {
1281                 for (i = 0; i < p->num_phys; i++) {
1282                         if (p->phy_info[i].attached.sas_address ==
1283                             rphy->identify.sas_address) {
1284                                 rc = p->phy_info[i].attached.slot;
1285                                 goto out;
1286                         }
1287                 }
1288         }
1289         rc = -ENXIO;
1290  out:
1291         mutex_unlock(&ioc->sas_topology_mutex);
1292         return rc;
1293 }
1294
1295 static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1296                               struct request *req)
1297 {
1298         MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
1299         MPT_FRAME_HDR *mf;
1300         SmpPassthroughRequest_t *smpreq;
1301         struct request *rsp = req->next_rq;
1302         int ret;
1303         int flagsLength;
1304         unsigned long timeleft;
1305         char *psge;
1306         dma_addr_t dma_addr_in = 0;
1307         dma_addr_t dma_addr_out = 0;
1308         u64 sas_address = 0;
1309
1310         if (!rsp) {
1311                 printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
1312                     ioc->name, __func__);
1313                 return -EINVAL;
1314         }
1315
1316         /* do we need to support multiple segments? */
1317         if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1318                 printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u %u, rsp %u %u\n",
1319                     ioc->name, __func__, req->bio->bi_vcnt, req->data_len,
1320                     rsp->bio->bi_vcnt, rsp->data_len);
1321                 return -EINVAL;
1322         }
1323
1324         ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
1325         if (ret)
1326                 goto out;
1327
1328         mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
1329         if (!mf) {
1330                 ret = -ENOMEM;
1331                 goto out_unlock;
1332         }
1333
1334         smpreq = (SmpPassthroughRequest_t *)mf;
1335         memset(smpreq, 0, sizeof(*smpreq));
1336
1337         smpreq->RequestDataLength = cpu_to_le16(req->data_len - 4);
1338         smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
1339
1340         if (rphy)
1341                 sas_address = rphy->identify.sas_address;
1342         else {
1343                 struct mptsas_portinfo *port_info;
1344
1345                 mutex_lock(&ioc->sas_topology_mutex);
1346                 port_info = mptsas_get_hba_portinfo(ioc);
1347                 if (port_info && port_info->phy_info)
1348                         sas_address =
1349                                 port_info->phy_info[0].phy->identify.sas_address;
1350                 mutex_unlock(&ioc->sas_topology_mutex);
1351         }
1352
1353         *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
1354
1355         psge = (char *)
1356                 (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
1357
1358         /* request */
1359         flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1360                        MPI_SGE_FLAGS_END_OF_BUFFER |
1361                        MPI_SGE_FLAGS_DIRECTION)
1362                        << MPI_SGE_FLAGS_SHIFT;
1363         flagsLength |= (req->data_len - 4);
1364
1365         dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
1366                                       req->data_len, PCI_DMA_BIDIRECTIONAL);
1367         if (!dma_addr_out)
1368                 goto put_mf;
1369         ioc->add_sge(psge, flagsLength, dma_addr_out);
1370         psge += (sizeof(u32) + sizeof(dma_addr_t));
1371
1372         /* response */
1373         flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
1374         flagsLength |= rsp->data_len + 4;
1375         dma_addr_in =  pci_map_single(ioc->pcidev, bio_data(rsp->bio),
1376                                       rsp->data_len, PCI_DMA_BIDIRECTIONAL);
1377         if (!dma_addr_in)
1378                 goto unmap;
1379         ioc->add_sge(psge, flagsLength, dma_addr_in);
1380
1381         mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
1382
1383         timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
1384         if (!timeleft) {
1385                 printk(MYIOC_s_ERR_FMT "%s: smp timeout!\n", ioc->name, __func__);
1386                 /* On timeout reset the board */
1387                 mpt_HardResetHandler(ioc, CAN_SLEEP);
1388                 ret = -ETIMEDOUT;
1389                 goto unmap;
1390         }
1391         mf = NULL;
1392
1393         if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
1394                 SmpPassthroughReply_t *smprep;
1395
1396                 smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
1397                 memcpy(req->sense, smprep, sizeof(*smprep));
1398                 req->sense_len = sizeof(*smprep);
1399                 req->data_len = 0;
1400                 rsp->data_len -= smprep->ResponseDataLength;
1401         } else {
1402                 printk(MYIOC_s_ERR_FMT "%s: smp passthru reply failed to be returned\n",
1403                     ioc->name, __func__);
1404                 ret = -ENXIO;
1405         }
1406 unmap:
1407         if (dma_addr_out)
1408                 pci_unmap_single(ioc->pcidev, dma_addr_out, req->data_len,
1409                                  PCI_DMA_BIDIRECTIONAL);
1410         if (dma_addr_in)
1411                 pci_unmap_single(ioc->pcidev, dma_addr_in, rsp->data_len,
1412                                  PCI_DMA_BIDIRECTIONAL);
1413 put_mf:
1414         if (mf)
1415                 mpt_free_msg_frame(ioc, mf);
1416 out_unlock:
1417         mutex_unlock(&ioc->sas_mgmt.mutex);
1418 out:
1419         return ret;
1420 }
1421
1422 static struct sas_function_template mptsas_transport_functions = {
1423         .get_linkerrors         = mptsas_get_linkerrors,
1424         .get_enclosure_identifier = mptsas_get_enclosure_identifier,
1425         .get_bay_identifier     = mptsas_get_bay_identifier,
1426         .phy_reset              = mptsas_phy_reset,
1427         .smp_handler            = mptsas_smp_handler,
1428 };
1429
1430 static struct scsi_transport_template *mptsas_transport_template;
1431
1432 static int
1433 mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
1434 {
1435         ConfigExtendedPageHeader_t hdr;
1436         CONFIGPARMS cfg;
1437         SasIOUnitPage0_t *buffer;
1438         dma_addr_t dma_handle;
1439         int error, i;
1440
1441         hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
1442         hdr.ExtPageLength = 0;
1443         hdr.PageNumber = 0;
1444         hdr.Reserved1 = 0;
1445         hdr.Reserved2 = 0;
1446         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1447         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1448
1449         cfg.cfghdr.ehdr = &hdr;
1450         cfg.physAddr = -1;
1451         cfg.pageAddr = 0;
1452         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1453         cfg.dir = 0;    /* read */
1454         cfg.timeout = 10;
1455
1456         error = mpt_config(ioc, &cfg);
1457         if (error)
1458                 goto out;
1459         if (!hdr.ExtPageLength) {
1460                 error = -ENXIO;
1461                 goto out;
1462         }
1463
1464         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1465                                             &dma_handle);
1466         if (!buffer) {
1467                 error = -ENOMEM;
1468                 goto out;
1469         }
1470
1471         cfg.physAddr = dma_handle;
1472         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1473
1474         error = mpt_config(ioc, &cfg);
1475         if (error)
1476                 goto out_free_consistent;
1477
1478         port_info->num_phys = buffer->NumPhys;
1479         port_info->phy_info = kcalloc(port_info->num_phys,
1480                 sizeof(*port_info->phy_info),GFP_KERNEL);
1481         if (!port_info->phy_info) {
1482                 error = -ENOMEM;
1483                 goto out_free_consistent;
1484         }
1485
1486         ioc->nvdata_version_persistent =
1487             le16_to_cpu(buffer->NvdataVersionPersistent);
1488         ioc->nvdata_version_default =
1489             le16_to_cpu(buffer->NvdataVersionDefault);
1490
1491         for (i = 0; i < port_info->num_phys; i++) {
1492                 mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
1493                 port_info->phy_info[i].phy_id = i;
1494                 port_info->phy_info[i].port_id =
1495                     buffer->PhyData[i].Port;
1496                 port_info->phy_info[i].negotiated_link_rate =
1497                     buffer->PhyData[i].NegotiatedLinkRate;
1498                 port_info->phy_info[i].portinfo = port_info;
1499                 port_info->phy_info[i].handle =
1500                     le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
1501         }
1502
1503  out_free_consistent:
1504         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1505                             buffer, dma_handle);
1506  out:
1507         return error;
1508 }
1509
1510 static int
1511 mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
1512 {
1513         ConfigExtendedPageHeader_t hdr;
1514         CONFIGPARMS cfg;
1515         SasIOUnitPage1_t *buffer;
1516         dma_addr_t dma_handle;
1517         int error;
1518         u16 device_missing_delay;
1519
1520         memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
1521         memset(&cfg, 0, sizeof(CONFIGPARMS));
1522
1523         cfg.cfghdr.ehdr = &hdr;
1524         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1525         cfg.timeout = 10;
1526         cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1527         cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
1528         cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
1529         cfg.cfghdr.ehdr->PageNumber = 1;
1530
1531         error = mpt_config(ioc, &cfg);
1532         if (error)
1533                 goto out;
1534         if (!hdr.ExtPageLength) {
1535                 error = -ENXIO;
1536                 goto out;
1537         }
1538
1539         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1540                                             &dma_handle);
1541         if (!buffer) {
1542                 error = -ENOMEM;
1543                 goto out;
1544         }
1545
1546         cfg.physAddr = dma_handle;
1547         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1548
1549         error = mpt_config(ioc, &cfg);
1550         if (error)
1551                 goto out_free_consistent;
1552
1553         ioc->io_missing_delay  =
1554             le16_to_cpu(buffer->IODeviceMissingDelay);
1555         device_missing_delay = le16_to_cpu(buffer->ReportDeviceMissingDelay);
1556         ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
1557             (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
1558             device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1559
1560  out_free_consistent:
1561         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1562                             buffer, dma_handle);
1563  out:
1564         return error;
1565 }
1566
1567 static int
1568 mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1569                 u32 form, u32 form_specific)
1570 {
1571         ConfigExtendedPageHeader_t hdr;
1572         CONFIGPARMS cfg;
1573         SasPhyPage0_t *buffer;
1574         dma_addr_t dma_handle;
1575         int error;
1576
1577         hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
1578         hdr.ExtPageLength = 0;
1579         hdr.PageNumber = 0;
1580         hdr.Reserved1 = 0;
1581         hdr.Reserved2 = 0;
1582         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1583         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
1584
1585         cfg.cfghdr.ehdr = &hdr;
1586         cfg.dir = 0;    /* read */
1587         cfg.timeout = 10;
1588
1589         /* Get Phy Pg 0 for each Phy. */
1590         cfg.physAddr = -1;
1591         cfg.pageAddr = form + form_specific;
1592         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1593
1594         error = mpt_config(ioc, &cfg);
1595         if (error)
1596                 goto out;
1597
1598         if (!hdr.ExtPageLength) {
1599                 error = -ENXIO;
1600                 goto out;
1601         }
1602
1603         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1604                                       &dma_handle);
1605         if (!buffer) {
1606                 error = -ENOMEM;
1607                 goto out;
1608         }
1609
1610         cfg.physAddr = dma_handle;
1611         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1612
1613         error = mpt_config(ioc, &cfg);
1614         if (error)
1615                 goto out_free_consistent;
1616
1617         mptsas_print_phy_pg0(ioc, buffer);
1618
1619         phy_info->hw_link_rate = buffer->HwLinkRate;
1620         phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1621         phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1622         phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1623
1624  out_free_consistent:
1625         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1626                             buffer, dma_handle);
1627  out:
1628         return error;
1629 }
1630
1631 static int
1632 mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
1633                 u32 form, u32 form_specific)
1634 {
1635         ConfigExtendedPageHeader_t hdr;
1636         CONFIGPARMS cfg;
1637         SasDevicePage0_t *buffer;
1638         dma_addr_t dma_handle;
1639         __le64 sas_address;
1640         int error=0;
1641
1642         if (ioc->sas_discovery_runtime &&
1643                 mptsas_is_end_device(device_info))
1644                         goto out;
1645
1646         hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
1647         hdr.ExtPageLength = 0;
1648         hdr.PageNumber = 0;
1649         hdr.Reserved1 = 0;
1650         hdr.Reserved2 = 0;
1651         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1652         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
1653
1654         cfg.cfghdr.ehdr = &hdr;
1655         cfg.pageAddr = form + form_specific;
1656         cfg.physAddr = -1;
1657         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1658         cfg.dir = 0;    /* read */
1659         cfg.timeout = 10;
1660
1661         memset(device_info, 0, sizeof(struct mptsas_devinfo));
1662         error = mpt_config(ioc, &cfg);
1663         if (error)
1664                 goto out;
1665         if (!hdr.ExtPageLength) {
1666                 error = -ENXIO;
1667                 goto out;
1668         }
1669
1670         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1671                                       &dma_handle);
1672         if (!buffer) {
1673                 error = -ENOMEM;
1674                 goto out;
1675         }
1676
1677         cfg.physAddr = dma_handle;
1678         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1679
1680         error = mpt_config(ioc, &cfg);
1681         if (error)
1682                 goto out_free_consistent;
1683
1684         mptsas_print_device_pg0(ioc, buffer);
1685
1686         device_info->handle = le16_to_cpu(buffer->DevHandle);
1687         device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
1688         device_info->handle_enclosure =
1689             le16_to_cpu(buffer->EnclosureHandle);
1690         device_info->slot = le16_to_cpu(buffer->Slot);
1691         device_info->phy_id = buffer->PhyNum;
1692         device_info->port_id = buffer->PhysicalPort;
1693         device_info->id = buffer->TargetID;
1694         device_info->phys_disk_num = ~0;
1695         device_info->channel = buffer->Bus;
1696         memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
1697         device_info->sas_address = le64_to_cpu(sas_address);
1698         device_info->device_info =
1699             le32_to_cpu(buffer->DeviceInfo);
1700
1701  out_free_consistent:
1702         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1703                             buffer, dma_handle);
1704  out:
1705         return error;
1706 }
1707
1708 static int
1709 mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
1710                 u32 form, u32 form_specific)
1711 {
1712         ConfigExtendedPageHeader_t hdr;
1713         CONFIGPARMS cfg;
1714         SasExpanderPage0_t *buffer;
1715         dma_addr_t dma_handle;
1716         int i, error;
1717
1718         hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1719         hdr.ExtPageLength = 0;
1720         hdr.PageNumber = 0;
1721         hdr.Reserved1 = 0;
1722         hdr.Reserved2 = 0;
1723         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1724         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1725
1726         cfg.cfghdr.ehdr = &hdr;
1727         cfg.physAddr = -1;
1728         cfg.pageAddr = form + form_specific;
1729         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1730         cfg.dir = 0;    /* read */
1731         cfg.timeout = 10;
1732
1733         memset(port_info, 0, sizeof(struct mptsas_portinfo));
1734         error = mpt_config(ioc, &cfg);
1735         if (error)
1736                 goto out;
1737
1738         if (!hdr.ExtPageLength) {
1739                 error = -ENXIO;
1740                 goto out;
1741         }
1742
1743         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1744                                       &dma_handle);
1745         if (!buffer) {
1746                 error = -ENOMEM;
1747                 goto out;
1748         }
1749
1750         cfg.physAddr = dma_handle;
1751         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1752
1753         error = mpt_config(ioc, &cfg);
1754         if (error)
1755                 goto out_free_consistent;
1756
1757         if (!buffer->NumPhys) {
1758                 error = -ENODEV;
1759                 goto out_free_consistent;
1760         }
1761
1762         /* save config data */
1763         port_info->num_phys = buffer->NumPhys;
1764         port_info->phy_info = kcalloc(port_info->num_phys,
1765                 sizeof(*port_info->phy_info),GFP_KERNEL);
1766         if (!port_info->phy_info) {
1767                 error = -ENOMEM;
1768                 goto out_free_consistent;
1769         }
1770
1771         for (i = 0; i < port_info->num_phys; i++) {
1772                 port_info->phy_info[i].portinfo = port_info;
1773                 port_info->phy_info[i].handle =
1774                     le16_to_cpu(buffer->DevHandle);
1775         }
1776
1777  out_free_consistent:
1778         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1779                             buffer, dma_handle);
1780  out:
1781         return error;
1782 }
1783
1784 static int
1785 mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
1786                 u32 form, u32 form_specific)
1787 {
1788         ConfigExtendedPageHeader_t hdr;
1789         CONFIGPARMS cfg;
1790         SasExpanderPage1_t *buffer;
1791         dma_addr_t dma_handle;
1792         int error=0;
1793
1794         if (ioc->sas_discovery_runtime &&
1795                 mptsas_is_end_device(&phy_info->attached))
1796                         goto out;
1797
1798         hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
1799         hdr.ExtPageLength = 0;
1800         hdr.PageNumber = 1;
1801         hdr.Reserved1 = 0;
1802         hdr.Reserved2 = 0;
1803         hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1804         hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
1805
1806         cfg.cfghdr.ehdr = &hdr;
1807         cfg.physAddr = -1;
1808         cfg.pageAddr = form + form_specific;
1809         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
1810         cfg.dir = 0;    /* read */
1811         cfg.timeout = 10;
1812
1813         error = mpt_config(ioc, &cfg);
1814         if (error)
1815                 goto out;
1816
1817         if (!hdr.ExtPageLength) {
1818                 error = -ENXIO;
1819                 goto out;
1820         }
1821
1822         buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1823                                       &dma_handle);
1824         if (!buffer) {
1825                 error = -ENOMEM;
1826                 goto out;
1827         }
1828
1829         cfg.physAddr = dma_handle;
1830         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
1831
1832         error = mpt_config(ioc, &cfg);
1833         if (error)
1834                 goto out_free_consistent;
1835
1836
1837         mptsas_print_expander_pg1(ioc, buffer);
1838
1839         /* save config data */
1840         phy_info->phy_id = buffer->PhyIdentifier;
1841         phy_info->port_id = buffer->PhysicalPort;
1842         phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
1843         phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
1844         phy_info->hw_link_rate = buffer->HwLinkRate;
1845         phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
1846         phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
1847
1848  out_free_consistent:
1849         pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
1850                             buffer, dma_handle);
1851  out:
1852         return error;
1853 }
1854
1855 static void
1856 mptsas_parse_device_info(struct sas_identify *identify,
1857                 struct mptsas_devinfo *device_info)
1858 {
1859         u16 protocols;
1860
1861         identify->sas_address = device_info->sas_address;
1862         identify->phy_identifier = device_info->phy_id;
1863
1864         /*
1865          * Fill in Phy Initiator Port Protocol.
1866          * Bits 6:3, more than one bit can be set, fall through cases.
1867          */
1868         protocols = device_info->device_info & 0x78;
1869         identify->initiator_port_protocols = 0;
1870         if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
1871                 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
1872         if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
1873                 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
1874         if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
1875                 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
1876         if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
1877                 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
1878
1879         /*
1880          * Fill in Phy Target Port Protocol.
1881          * Bits 10:7, more than one bit can be set, fall through cases.
1882          */
1883         protocols = device_info->device_info & 0x780;
1884         identify->target_port_protocols = 0;
1885         if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
1886                 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
1887         if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
1888                 identify->target_port_protocols |= SAS_PROTOCOL_STP;
1889         if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
1890                 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
1891         if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
1892                 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
1893
1894         /*
1895          * Fill in Attached device type.
1896          */
1897         switch (device_info->device_info &
1898                         MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
1899         case MPI_SAS_DEVICE_INFO_NO_DEVICE:
1900                 identify->device_type = SAS_PHY_UNUSED;
1901                 break;
1902         case MPI_SAS_DEVICE_INFO_END_DEVICE:
1903                 identify->device_type = SAS_END_DEVICE;
1904                 break;
1905         case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
1906                 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
1907                 break;
1908         case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
1909                 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
1910                 break;
1911         }
1912 }
1913
1914 static int mptsas_probe_one_phy(struct device *dev,
1915                 struct mptsas_phyinfo *phy_info, int index, int local)
1916 {
1917         MPT_ADAPTER *ioc;
1918         struct sas_phy *phy;
1919         struct sas_port *port;
1920         int error = 0;
1921
1922         if (!dev) {
1923                 error = -ENODEV;
1924                 goto out;
1925         }
1926
1927         if (!phy_info->phy) {
1928                 phy = sas_phy_alloc(dev, index);
1929                 if (!phy) {
1930                         error = -ENOMEM;
1931                         goto out;
1932                 }
1933         } else
1934                 phy = phy_info->phy;
1935
1936         mptsas_parse_device_info(&phy->identify, &phy_info->identify);
1937
1938         /*
1939          * Set Negotiated link rate.
1940          */
1941         switch (phy_info->negotiated_link_rate) {
1942         case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
1943                 phy->negotiated_linkrate = SAS_PHY_DISABLED;
1944                 break;
1945         case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
1946                 phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
1947                 break;
1948         case MPI_SAS_IOUNIT0_RATE_1_5:
1949                 phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
1950                 break;
1951         case MPI_SAS_IOUNIT0_RATE_3_0:
1952                 phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
1953                 break;
1954         case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
1955         case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
1956         default:
1957                 phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1958                 break;
1959         }
1960
1961         /*
1962          * Set Max hardware link rate.
1963          */
1964         switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1965         case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
1966                 phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1967                 break;
1968         case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1969                 phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1970                 break;
1971         default:
1972                 break;
1973         }
1974
1975         /*
1976          * Set Max programmed link rate.
1977          */
1978         switch (phy_info->programmed_link_rate &
1979                         MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
1980         case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
1981                 phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1982                 break;
1983         case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
1984                 phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
1985                 break;
1986         default:
1987                 break;
1988         }
1989
1990         /*
1991          * Set Min hardware link rate.
1992          */
1993         switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
1994         case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
1995                 phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
1996                 break;
1997         case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
1998                 phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
1999                 break;
2000         default:
2001                 break;
2002         }
2003
2004         /*
2005          * Set Min programmed link rate.
2006          */
2007         switch (phy_info->programmed_link_rate &
2008                         MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
2009         case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
2010                 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
2011                 break;
2012         case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
2013                 phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
2014                 break;
2015         default:
2016                 break;
2017         }
2018
2019         if (!phy_info->phy) {
2020
2021                 error = sas_phy_add(phy);
2022                 if (error) {
2023                         sas_phy_free(phy);
2024                         goto out;
2025                 }
2026                 phy_info->phy = phy;
2027         }
2028
2029         if (!phy_info->attached.handle ||
2030                         !phy_info->port_details)
2031                 goto out;
2032
2033         port = mptsas_get_port(phy_info);
2034         ioc = phy_to_ioc(phy_info->phy);
2035
2036         if (phy_info->sas_port_add_phy) {
2037
2038                 if (!port) {
2039                         port = sas_port_alloc_num(dev);
2040                         if (!port) {
2041                                 error = -ENOMEM;
2042                                 goto out;
2043                         }
2044                         error = sas_port_add(port);
2045                         if (error) {
2046                                 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2047                                         "%s: exit at line=%d\n", ioc->name,
2048                                         __func__, __LINE__));
2049                                 goto out;
2050                         }
2051                         mptsas_set_port(ioc, phy_info, port);
2052                         dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
2053                             "sas_port_alloc: port=%p dev=%p port_id=%d\n",
2054                             ioc->name, port, dev, port->port_identifier));
2055                 }
2056                 dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_port_add_phy: phy_id=%d\n",
2057                     ioc->name, phy_info->phy_id));
2058                 sas_port_add_phy(port, phy_info->phy);
2059                 phy_info->sas_port_add_phy = 0;
2060         }
2061
2062         if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
2063
2064                 struct sas_rphy *rphy;
2065                 struct device *parent;
2066                 struct sas_identify identify;
2067
2068                 parent = dev->parent->parent;
2069                 /*
2070                  * Let the hotplug_work thread handle processing
2071                  * the adding/removing of devices that occur
2072                  * after start of day.
2073                  */
2074                 if (ioc->sas_discovery_runtime &&
2075                         mptsas_is_end_device(&phy_info->attached))
2076                                 goto out;
2077
2078                 mptsas_parse_device_info(&identify, &phy_info->attached);
2079                 if (scsi_is_host_device(parent)) {
2080                         struct mptsas_portinfo *port_info;
2081                         int i;
2082
2083                         mutex_lock(&ioc->sas_topology_mutex);
2084                         port_info = mptsas_get_hba_portinfo(ioc);
2085                         mutex_unlock(&ioc->sas_topology_mutex);
2086
2087                         for (i = 0; i < port_info->num_phys; i++)
2088                                 if (port_info->phy_info[i].identify.sas_address ==
2089                                     identify.sas_address) {
2090                                         sas_port_mark_backlink(port);
2091                                         goto out;
2092                                 }
2093
2094                 } else if (scsi_is_sas_rphy(parent)) {
2095                         struct sas_rphy *parent_rphy = dev_to_rphy(parent);
2096                         if (identify.sas_address ==
2097                             parent_rphy->identify.sas_address) {
2098                                 sas_port_mark_backlink(port);
2099                                 goto out;
2100                         }
2101                 }
2102
2103                 switch (identify.device_type) {
2104                 case SAS_END_DEVICE:
2105                         rphy = sas_end_device_alloc(port);
2106                         break;
2107                 case SAS_EDGE_EXPANDER_DEVICE:
2108                 case SAS_FANOUT_EXPANDER_DEVICE:
2109                         rphy = sas_expander_alloc(port, identify.device_type);
2110                         break;
2111                 default:
2112                         rphy = NULL;
2113                         break;
2114                 }
2115                 if (!rphy) {
2116                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2117                                 "%s: exit at line=%d\n", ioc->name,
2118                                 __func__, __LINE__));
2119                         goto out;
2120                 }
2121
2122                 rphy->identify = identify;
2123                 error = sas_rphy_add(rphy);
2124                 if (error) {
2125                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2126                                 "%s: exit at line=%d\n", ioc->name,
2127                                 __func__, __LINE__));
2128                         sas_rphy_free(rphy);
2129                         goto out;
2130                 }
2131                 mptsas_set_rphy(ioc, phy_info, rphy);
2132         }
2133
2134  out:
2135         return error;
2136 }
2137
2138 static int
2139 mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
2140 {
2141         struct mptsas_portinfo *port_info, *hba;
2142         int error = -ENOMEM, i;
2143
2144         hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
2145         if (! hba)
2146                 goto out;
2147
2148         error = mptsas_sas_io_unit_pg0(ioc, hba);
2149         if (error)
2150                 goto out_free_port_info;
2151
2152         mptsas_sas_io_unit_pg1(ioc);
2153         mutex_lock(&ioc->sas_topology_mutex);
2154         port_info = mptsas_get_hba_portinfo(ioc);
2155         if (!port_info) {
2156                 port_info = hba;
2157                 list_add_tail(&port_info->list, &ioc->sas_topology);
2158         } else {
2159                 for (i = 0; i < hba->num_phys; i++) {
2160                         port_info->phy_info[i].negotiated_link_rate =
2161                                 hba->phy_info[i].negotiated_link_rate;
2162                         port_info->phy_info[i].handle =
2163                                 hba->phy_info[i].handle;
2164                         port_info->phy_info[i].port_id =
2165                                 hba->phy_info[i].port_id;
2166                 }
2167                 kfree(hba->phy_info);
2168                 kfree(hba);
2169                 hba = NULL;
2170         }
2171         mutex_unlock(&ioc->sas_topology_mutex);
2172         for (i = 0; i < port_info->num_phys; i++) {
2173                 mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
2174                         (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
2175                          MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
2176
2177                 mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
2178                         (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2179                          MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2180                          port_info->phy_info[i].handle);
2181                 port_info->phy_info[i].identify.phy_id =
2182                     port_info->phy_info[i].phy_id = i;
2183                 if (port_info->phy_info[i].attached.handle)
2184                         mptsas_sas_device_pg0(ioc,
2185                                 &port_info->phy_info[i].attached,
2186                                 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2187                                  MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2188                                 port_info->phy_info[i].attached.handle);
2189         }
2190
2191         mptsas_setup_wide_ports(ioc, port_info);
2192
2193         for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2194                 mptsas_probe_one_phy(&ioc->sh->shost_gendev,
2195                     &port_info->phy_info[i], ioc->sas_index, 1);
2196
2197         return 0;
2198
2199  out_free_port_info:
2200         kfree(hba);
2201  out:
2202         return error;
2203 }
2204
2205 static int
2206 mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
2207 {
2208         struct mptsas_portinfo *port_info, *p, *ex;
2209         struct device *parent;
2210         struct sas_rphy *rphy;
2211         int error = -ENOMEM, i, j;
2212
2213         ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
2214         if (!ex)
2215                 goto out;
2216
2217         error = mptsas_sas_expander_pg0(ioc, ex,
2218             (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
2219              MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
2220         if (error)
2221                 goto out_free_port_info;
2222
2223         *handle = ex->phy_info[0].handle;
2224
2225         mutex_lock(&ioc->sas_topology_mutex);
2226         port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
2227         if (!port_info) {
2228                 port_info = ex;
2229                 list_add_tail(&port_info->list, &ioc->sas_topology);
2230         } else {
2231                 for (i = 0; i < ex->num_phys; i++) {
2232                         port_info->phy_info[i].handle =
2233                                 ex->phy_info[i].handle;
2234                         port_info->phy_info[i].port_id =
2235                                 ex->phy_info[i].port_id;
2236                 }
2237                 kfree(ex->phy_info);
2238                 kfree(ex);
2239                 ex = NULL;
2240         }
2241         mutex_unlock(&ioc->sas_topology_mutex);
2242
2243         for (i = 0; i < port_info->num_phys; i++) {
2244                 mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
2245                         (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
2246                          MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
2247
2248                 if (port_info->phy_info[i].identify.handle) {
2249                         mptsas_sas_device_pg0(ioc,
2250                                 &port_info->phy_info[i].identify,
2251                                 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2252                                  MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2253                                 port_info->phy_info[i].identify.handle);
2254                         port_info->phy_info[i].identify.phy_id =
2255                             port_info->phy_info[i].phy_id;
2256                 }
2257
2258                 if (port_info->phy_info[i].attached.handle) {
2259                         mptsas_sas_device_pg0(ioc,
2260                                 &port_info->phy_info[i].attached,
2261                                 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
2262                                  MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2263                                 port_info->phy_info[i].attached.handle);
2264                         port_info->phy_info[i].attached.phy_id =
2265                             port_info->phy_info[i].phy_id;
2266                 }
2267         }
2268
2269         parent = &ioc->sh->shost_gendev;
2270         for (i = 0; i < port_info->num_phys; i++) {
2271                 mutex_lock(&ioc->sas_topology_mutex);
2272                 list_for_each_entry(p, &ioc->sas_topology, list) {
2273                         for (j = 0; j < p->num_phys; j++) {
2274                                 if (port_info->phy_info[i].identify.handle !=
2275                                                 p->phy_info[j].attached.handle)
2276                                         continue;
2277                                 rphy = mptsas_get_rphy(&p->phy_info[j]);
2278                                 parent = &rphy->dev;
2279                         }
2280                 }
2281                 mutex_unlock(&ioc->sas_topology_mutex);
2282         }
2283
2284         mptsas_setup_wide_ports(ioc, port_info);
2285
2286         for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
2287                 mptsas_probe_one_phy(parent, &port_info->phy_info[i],
2288                     ioc->sas_index, 0);
2289
2290         return 0;
2291
2292  out_free_port_info:
2293         if (ex) {
2294                 kfree(ex->phy_info);
2295                 kfree(ex);
2296         }
2297  out:
2298         return error;
2299 }
2300
2301 /*
2302  * mptsas_delete_expander_phys
2303  *
2304  *
2305  * This will traverse topology, and remove expanders
2306  * that are no longer present
2307  */
2308 static void
2309 mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
2310 {
2311         struct mptsas_portinfo buffer;
2312         struct mptsas_portinfo *port_info, *n, *parent;
2313         struct mptsas_phyinfo *phy_info;
2314         struct sas_port * port;
2315         int i;
2316         u64     expander_sas_address;
2317
2318         mutex_lock(&ioc->sas_topology_mutex);
2319         list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
2320
2321                 if (!(port_info->phy_info[0].identify.device_info &
2322                     MPI_SAS_DEVICE_INFO_SMP_TARGET))
2323                         continue;
2324
2325                 if (mptsas_sas_expander_pg0(ioc, &buffer,
2326                      (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
2327                      MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
2328                      port_info->phy_info[0].handle)) {
2329
2330                         /*
2331                          * Obtain the port_info instance to the parent port
2332                          */
2333                         parent = mptsas_find_portinfo_by_handle(ioc,
2334                             port_info->phy_info[0].identify.handle_parent);
2335
2336                         if (!parent)
2337                                 goto next_port;
2338
2339                         expander_sas_address =
2340                                 port_info->phy_info[0].identify.sas_address;
2341
2342                         /*
2343                          * Delete rphys in the parent that point
2344                          * to this expander.  The transport layer will
2345                          * cleanup all the children.
2346                          */
2347                         phy_info = parent->phy_info;
2348                         for (i = 0; i < parent->num_phys; i++, phy_info++) {
2349                                 port = mptsas_get_port(phy_info);
2350                                 if (!port)
2351                                         continue;
2352                                 if (phy_info->attached.sas_address !=
2353                                         expander_sas_address)
2354                                         continue;
2355                                 dsaswideprintk(ioc,
2356                                     dev_printk(KERN_DEBUG, &port->dev,
2357                                     MYIOC_s_FMT "delete port (%d)\n", ioc->name,
2358                                     port->port_identifier));
2359                                 sas_port_delete(port);
2360                                 mptsas_port_delete(ioc, phy_info->port_details);
2361                         }
2362  next_port:
2363
2364                         phy_info = port_info->phy_info;
2365                         for (i = 0; i < port_info->num_phys; i++, phy_info++)
2366                                 mptsas_port_delete(ioc, phy_info->port_details);
2367
2368                         list_del(&port_info->list);
2369                         kfree(port_info->phy_info);
2370                         kfree(port_info);
2371                 }
2372                 /*
2373                 * Free this memory allocated from inside
2374                 * mptsas_sas_expander_pg0
2375                 */
2376                 kfree(buffer.phy_info);
2377         }
2378         mutex_unlock(&ioc->sas_topology_mutex);
2379 }
2380
2381 /*
2382  * Start of day discovery
2383  */
2384 static void
2385 mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
2386 {
2387         u32 handle = 0xFFFF;
2388         int i;
2389
2390         mutex_lock(&ioc->sas_discovery_mutex);
2391         mptsas_probe_hba_phys(ioc);
2392         while (!mptsas_probe_expander_phys(ioc, &handle))
2393                 ;
2394         /*
2395           Reporting RAID volumes.
2396         */
2397         if (!ioc->ir_firmware)
2398                 goto out;
2399         if (!ioc->raid_data.pIocPg2)
2400                 goto out;
2401         if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
2402                 goto out;
2403         for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
2404                 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
2405                     ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
2406         }
2407  out:
2408         mutex_unlock(&ioc->sas_discovery_mutex);
2409 }
2410
2411 /*
2412  * Work queue thread to handle Runtime discovery
2413  * Mere purpose is the hot add/delete of expanders
2414  *(Mutex UNLOCKED)
2415  */
2416 static void
2417 __mptsas_discovery_work(MPT_ADAPTER *ioc)
2418 {
2419         u32 handle = 0xFFFF;
2420
2421         ioc->sas_discovery_runtime=1;
2422         mptsas_delete_expander_phys(ioc);
2423         mptsas_probe_hba_phys(ioc);
2424         while (!mptsas_probe_expander_phys(ioc, &handle))
2425                 ;
2426         ioc->sas_discovery_runtime=0;
2427 }
2428
2429 /*
2430  * Work queue thread to handle Runtime discovery
2431  * Mere purpose is the hot add/delete of expanders
2432  *(Mutex LOCKED)
2433  */
2434 static void
2435 mptsas_discovery_work(struct work_struct *work)
2436 {
2437         struct mptsas_discovery_event *ev =
2438                 container_of(work, struct mptsas_discovery_event, work);
2439         MPT_ADAPTER *ioc = ev->ioc;
2440
2441         mutex_lock(&ioc->sas_discovery_mutex);
2442         __mptsas_discovery_work(ioc);
2443         mutex_unlock(&ioc->sas_discovery_mutex);
2444         kfree(ev);
2445 }
2446
2447 static struct mptsas_phyinfo *
2448 mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
2449 {
2450         struct mptsas_portinfo *port_info;
2451         struct mptsas_phyinfo *phy_info = NULL;
2452         int i;
2453
2454         mutex_lock(&ioc->sas_topology_mutex);
2455         list_for_each_entry(port_info, &ioc->sas_topology, list) {
2456                 for (i = 0; i < port_info->num_phys; i++) {
2457                         if (!mptsas_is_end_device(
2458                                 &port_info->phy_info[i].attached))
2459                                 continue;
2460                         if (port_info->phy_info[i].attached.sas_address
2461                             != sas_address)
2462                                 continue;
2463                         phy_info = &port_info->phy_info[i];
2464                         break;
2465                 }
2466         }
2467         mutex_unlock(&ioc->sas_topology_mutex);
2468         return phy_info;
2469 }
2470
2471 static struct mptsas_phyinfo *
2472 mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
2473 {
2474         struct mptsas_portinfo *port_info;
2475         struct mptsas_phyinfo *phy_info = NULL;
2476         int i;
2477
2478         mutex_lock(&ioc->sas_topology_mutex);
2479         list_for_each_entry(port_info, &ioc->sas_topology, list) {
2480                 for (i = 0; i < port_info->num_phys; i++) {
2481                         if (!mptsas_is_end_device(
2482                                 &port_info->phy_info[i].attached))
2483                                 continue;
2484                         if (port_info->phy_info[i].attached.id != id)
2485                                 continue;
2486                         if (port_info->phy_info[i].attached.channel != channel)
2487                                 continue;
2488                         phy_info = &port_info->phy_info[i];
2489                         break;
2490                 }
2491         }
2492         mutex_unlock(&ioc->sas_topology_mutex);
2493         return phy_info;
2494 }
2495
2496 static struct mptsas_phyinfo *
2497 mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
2498 {
2499         struct mptsas_portinfo *port_info;
2500         struct mptsas_phyinfo *phy_info = NULL;
2501         int i;
2502
2503         mutex_lock(&ioc->sas_topology_mutex);
2504         list_for_each_entry(port_info, &ioc->sas_topology, list) {
2505                 for (i = 0; i < port_info->num_phys; i++) {
2506                         if (!mptsas_is_end_device(
2507                                 &port_info->phy_info[i].attached))
2508                                 continue;
2509                         if (port_info->phy_info[i].attached.phys_disk_num == ~0)
2510                                 continue;
2511                         if (port_info->phy_info[i].attached.phys_disk_num != id)
2512                                 continue;
2513                         if (port_info->phy_info[i].attached.channel != channel)
2514                                 continue;
2515                         phy_info = &port_info->phy_info[i];
2516                         break;
2517                 }
2518         }
2519         mutex_unlock(&ioc->sas_topology_mutex);
2520         return phy_info;
2521 }
2522
2523 /*
2524  * Work queue thread to clear the persitency table
2525  */
2526 static void
2527 mptsas_persist_clear_table(struct work_struct *work)
2528 {
2529         MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
2530
2531         mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
2532 }
2533
2534 static void
2535 mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
2536 {
2537         int rc;
2538
2539         sdev->no_uld_attach = data ? 1 : 0;
2540         rc = scsi_device_reprobe(sdev);
2541 }
2542
2543 static void
2544 mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
2545 {
2546         starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
2547                         mptsas_reprobe_lun);
2548 }
2549
2550 static void
2551 mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
2552 {
2553         CONFIGPARMS                     cfg;
2554         ConfigPageHeader_t              hdr;
2555         dma_addr_t                      dma_handle;
2556         pRaidVolumePage0_t              buffer = NULL;
2557         RaidPhysDiskPage0_t             phys_disk;
2558         int                             i;
2559         struct mptsas_hotplug_event     *ev;
2560
2561         memset(&cfg, 0 , sizeof(CONFIGPARMS));
2562         memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
2563         hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
2564         cfg.pageAddr = (channel << 8) + id;
2565         cfg.cfghdr.hdr = &hdr;
2566         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2567
2568         if (mpt_config(ioc, &cfg) != 0)
2569                 goto out;
2570
2571         if (!hdr.PageLength)
2572                 goto out;
2573
2574         buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
2575             &dma_handle);
2576
2577         if (!buffer)
2578                 goto out;
2579
2580         cfg.physAddr = dma_handle;
2581         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2582
2583         if (mpt_config(ioc, &cfg) != 0)
2584                 goto out;
2585
2586         if (!(buffer->VolumeStatus.Flags &
2587             MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
2588                 goto out;
2589
2590         if (!buffer->NumPhysDisks)
2591                 goto out;
2592
2593         for (i = 0; i < buffer->NumPhysDisks; i++) {
2594
2595                 if (mpt_raid_phys_disk_pg0(ioc,
2596                     buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
2597                         continue;
2598
2599                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2600                 if (!ev) {
2601                         printk(MYIOC_s_WARN_FMT "mptsas: lost hotplug event\n", ioc->name);
2602                         goto out;
2603                 }
2604
2605                 INIT_WORK(&ev->work, mptsas_hotplug_work);
2606                 ev->ioc = ioc;
2607                 ev->id = phys_disk.PhysDiskID;
2608                 ev->channel = phys_disk.PhysDiskBus;
2609                 ev->phys_disk_num_valid = 1;
2610                 ev->phys_disk_num = phys_disk.PhysDiskNum;
2611                 ev->event_type = MPTSAS_ADD_DEVICE;
2612                 schedule_work(&ev->work);
2613         }
2614
2615  out:
2616         if (buffer)
2617                 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
2618                     dma_handle);
2619 }
2620 /*
2621  * Work queue thread to handle SAS hotplug events
2622  */
2623 static void
2624 mptsas_hotplug_work(struct work_struct *work)
2625 {
2626         struct mptsas_hotplug_event *ev =
2627                 container_of(work, struct mptsas_hotplug_event, work);
2628
2629         MPT_ADAPTER *ioc = ev->ioc;
2630         struct mptsas_phyinfo *phy_info;
2631         struct sas_rphy *rphy;
2632         struct sas_port *port;
2633         struct scsi_device *sdev;
2634         struct scsi_target * starget;
2635         struct sas_identify identify;
2636         char *ds = NULL;
2637         struct mptsas_devinfo sas_device;
2638         VirtTarget *vtarget;
2639         VirtDevice *vdevice;
2640
2641         mutex_lock(&ioc->sas_discovery_mutex);
2642         switch (ev->event_type) {
2643         case MPTSAS_DEL_DEVICE:
2644
2645                 phy_info = NULL;
2646                 if (ev->phys_disk_num_valid) {
2647                         if (ev->hidden_raid_component){
2648                                 if (mptsas_sas_device_pg0(ioc, &sas_device,
2649                                     (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2650                                      MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2651                                     (ev->channel << 8) + ev->id)) {
2652                                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2653                                         "%s: exit at line=%d\n", ioc->name,
2654                                                 __func__, __LINE__));
2655                                         break;
2656                                 }
2657                                 phy_info = mptsas_find_phyinfo_by_sas_address(
2658                                     ioc, sas_device.sas_address);
2659                         }else
2660                                 phy_info = mptsas_find_phyinfo_by_phys_disk_num(
2661                                     ioc, ev->channel, ev->phys_disk_num);
2662                 }
2663
2664                 if (!phy_info)
2665                         phy_info = mptsas_find_phyinfo_by_target(ioc,
2666                             ev->channel, ev->id);
2667
2668                 /*
2669                  * Sanity checks, for non-existing phys and remote rphys.
2670                  */
2671                 if (!phy_info){
2672                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2673                                 "%s: exit at line=%d\n", ioc->name,
2674                                 __func__, __LINE__));
2675                         break;
2676                 }
2677                 if (!phy_info->port_details) {
2678                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2679                                 "%s: exit at line=%d\n", ioc->name,
2680                                 __func__, __LINE__));
2681                         break;
2682                 }
2683                 rphy = mptsas_get_rphy(phy_info);
2684                 if (!rphy) {
2685                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2686                                 "%s: exit at line=%d\n", ioc->name,
2687                                 __func__, __LINE__));
2688                         break;
2689                 }
2690
2691                 port = mptsas_get_port(phy_info);
2692                 if (!port) {
2693                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2694                                 "%s: exit at line=%d\n", ioc->name,
2695                                 __func__, __LINE__));
2696                         break;
2697                 }
2698
2699                 starget = mptsas_get_starget(phy_info);
2700                 if (starget) {
2701                         vtarget = starget->hostdata;
2702
2703                         if (!vtarget) {
2704                                 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2705                                         "%s: exit at line=%d\n", ioc->name,
2706                                         __func__, __LINE__));
2707                                 break;
2708                         }
2709
2710                         /*
2711                          * Handling  RAID components
2712                          */
2713                         if (ev->phys_disk_num_valid &&
2714                             ev->hidden_raid_component) {
2715                                 printk(MYIOC_s_INFO_FMT
2716                                     "RAID Hidding: channel=%d, id=%d, "
2717                                     "physdsk %d \n", ioc->name, ev->channel,
2718                                     ev->id, ev->phys_disk_num);
2719                                 vtarget->id = ev->phys_disk_num;
2720                                 vtarget->tflags |=
2721                                     MPT_TARGET_FLAGS_RAID_COMPONENT;
2722                                 mptsas_reprobe_target(starget, 1);
2723                                 phy_info->attached.phys_disk_num =
2724                                     ev->phys_disk_num;
2725                         break;
2726                         }
2727                 }
2728
2729                 if (phy_info->attached.device_info &
2730                     MPI_SAS_DEVICE_INFO_SSP_TARGET)
2731                         ds = "ssp";
2732                 if (phy_info->attached.device_info &
2733                     MPI_SAS_DEVICE_INFO_STP_TARGET)
2734                         ds = "stp";
2735                 if (phy_info->attached.device_info &
2736                     MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2737                         ds = "sata";
2738
2739                 printk(MYIOC_s_INFO_FMT
2740                        "removing %s device, channel %d, id %d, phy %d\n",
2741                        ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
2742                 dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
2743                     "delete port (%d)\n", ioc->name, port->port_identifier);
2744                 sas_port_delete(port);
2745                 mptsas_port_delete(ioc, phy_info->port_details);
2746                 break;
2747         case MPTSAS_ADD_DEVICE:
2748
2749                 if (ev->phys_disk_num_valid)
2750                         mpt_findImVolumes(ioc);
2751
2752                 /*
2753                  * Refresh sas device pg0 data
2754                  */
2755                 if (mptsas_sas_device_pg0(ioc, &sas_device,
2756                     (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
2757                      MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
2758                         (ev->channel << 8) + ev->id)) {
2759                                 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2760                                         "%s: exit at line=%d\n", ioc->name,
2761                                         __func__, __LINE__));
2762                         break;
2763                 }
2764
2765                 __mptsas_discovery_work(ioc);
2766
2767                 phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
2768                                 sas_device.sas_address);
2769
2770                 if (!phy_info || !phy_info->port_details) {
2771                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2772                                 "%s: exit at line=%d\n", ioc->name,
2773                                 __func__, __LINE__));
2774                         break;
2775                 }
2776
2777                 starget = mptsas_get_starget(phy_info);
2778                 if (starget && (!ev->hidden_raid_component)){
2779
2780                         vtarget = starget->hostdata;
2781
2782                         if (!vtarget) {
2783                                 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2784                                     "%s: exit at line=%d\n", ioc->name,
2785                                     __func__, __LINE__));
2786                                 break;
2787                         }
2788                         /*
2789                          * Handling  RAID components
2790                          */
2791                         if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2792                                 printk(MYIOC_s_INFO_FMT
2793                                     "RAID Exposing: channel=%d, id=%d, "
2794                                     "physdsk %d \n", ioc->name, ev->channel,
2795                                     ev->id, ev->phys_disk_num);
2796                                 vtarget->tflags &=
2797                                     ~MPT_TARGET_FLAGS_RAID_COMPONENT;
2798                                 vtarget->id = ev->id;
2799                                 mptsas_reprobe_target(starget, 0);
2800                                 phy_info->attached.phys_disk_num = ~0;
2801                         }
2802                         break;
2803                 }
2804
2805                 if (mptsas_get_rphy(phy_info)) {
2806                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2807                                 "%s: exit at line=%d\n", ioc->name,
2808                                 __func__, __LINE__));
2809                         if (ev->channel) printk("%d\n", __LINE__);
2810                         break;
2811                 }
2812
2813                 port = mptsas_get_port(phy_info);
2814                 if (!port) {
2815                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2816                                 "%s: exit at line=%d\n", ioc->name,
2817                                 __func__, __LINE__));
2818                         break;
2819                 }
2820                 memcpy(&phy_info->attached, &sas_device,
2821                     sizeof(struct mptsas_devinfo));
2822
2823                 if (phy_info->attached.device_info &
2824                     MPI_SAS_DEVICE_INFO_SSP_TARGET)
2825                         ds = "ssp";
2826                 if (phy_info->attached.device_info &
2827                     MPI_SAS_DEVICE_INFO_STP_TARGET)
2828                         ds = "stp";
2829                 if (phy_info->attached.device_info &
2830                     MPI_SAS_DEVICE_INFO_SATA_DEVICE)
2831                         ds = "sata";
2832
2833                 printk(MYIOC_s_INFO_FMT
2834                        "attaching %s device, channel %d, id %d, phy %d\n",
2835                        ioc->name, ds, ev->channel, ev->id, ev->phy_id);
2836
2837                 mptsas_parse_device_info(&identify, &phy_info->attached);
2838                 rphy = sas_end_device_alloc(port);
2839                 if (!rphy) {
2840                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2841                                 "%s: exit at line=%d\n", ioc->name,
2842                                 __func__, __LINE__));
2843                         break; /* non-fatal: an rphy can be added later */
2844                 }
2845
2846                 rphy->identify = identify;
2847                 if (sas_rphy_add(rphy)) {
2848                         dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2849                                 "%s: exit at line=%d\n", ioc->name,
2850                                 __func__, __LINE__));
2851                         sas_rphy_free(rphy);
2852                         break;
2853                 }
2854                 mptsas_set_rphy(ioc, phy_info, rphy);
2855                 break;
2856         case MPTSAS_ADD_RAID:
2857                 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2858                     ev->id, 0);
2859                 if (sdev) {
2860                         scsi_device_put(sdev);
2861                         break;
2862                 }
2863                 printk(MYIOC_s_INFO_FMT
2864                        "attaching raid volume, channel %d, id %d\n",
2865                        ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2866                 scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
2867                 mpt_findImVolumes(ioc);
2868                 break;
2869         case MPTSAS_DEL_RAID:
2870                 sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
2871                     ev->id, 0);
2872                 if (!sdev)
2873                         break;
2874                 printk(MYIOC_s_INFO_FMT
2875                        "removing raid volume, channel %d, id %d\n",
2876                        ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
2877                 vdevice = sdev->hostdata;
2878                 scsi_remove_device(sdev);
2879                 scsi_device_put(sdev);
2880                 mpt_findImVolumes(ioc);
2881                 break;
2882         case MPTSAS_ADD_INACTIVE_VOLUME:
2883                 mptsas_adding_inactive_raid_components(ioc,
2884                     ev->channel, ev->id);
2885                 break;
2886         case MPTSAS_IGNORE_EVENT:
2887         default:
2888                 break;
2889         }
2890
2891         mutex_unlock(&ioc->sas_discovery_mutex);
2892         kfree(ev);
2893 }
2894
2895 static void
2896 mptsas_send_sas_event(MPT_ADAPTER *ioc,
2897                 EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
2898 {
2899         struct mptsas_hotplug_event *ev;
2900         u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
2901         __le64 sas_address;
2902
2903         if ((device_info &
2904              (MPI_SAS_DEVICE_INFO_SSP_TARGET |
2905               MPI_SAS_DEVICE_INFO_STP_TARGET |
2906               MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
2907                 return;
2908
2909         switch (sas_event_data->ReasonCode) {
2910         case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2911
2912                 mptsas_target_reset_queue(ioc, sas_event_data);
2913                 break;
2914
2915         case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2916                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2917                 if (!ev) {
2918                         printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
2919                         break;
2920                 }
2921
2922                 INIT_WORK(&ev->work, mptsas_hotplug_work);
2923                 ev->ioc = ioc;
2924                 ev->handle = le16_to_cpu(sas_event_data->DevHandle);
2925                 ev->parent_handle =
2926                     le16_to_cpu(sas_event_data->ParentDevHandle);
2927                 ev->channel = sas_event_data->Bus;
2928                 ev->id = sas_event_data->TargetID;
2929                 ev->phy_id = sas_event_data->PhyNum;
2930                 memcpy(&sas_address, &sas_event_data->SASAddress,
2931                     sizeof(__le64));
2932                 ev->sas_address = le64_to_cpu(sas_address);
2933                 ev->device_info = device_info;
2934
2935                 if (sas_event_data->ReasonCode &
2936                     MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
2937                         ev->event_type = MPTSAS_ADD_DEVICE;
2938                 else
2939                         ev->event_type = MPTSAS_DEL_DEVICE;
2940                 schedule_work(&ev->work);
2941                 break;
2942         case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
2943         /*
2944          * Persistent table is full.
2945          */
2946                 INIT_WORK(&ioc->sas_persist_task,
2947                     mptsas_persist_clear_table);
2948                 schedule_work(&ioc->sas_persist_task);
2949                 break;
2950         /*
2951          * TODO, handle other events
2952          */
2953         case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
2954         case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
2955         case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2956         case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
2957         case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
2958         case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
2959         case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
2960         default:
2961                 break;
2962         }
2963 }
2964 static void
2965 mptsas_send_raid_event(MPT_ADAPTER *ioc,
2966                 EVENT_DATA_RAID *raid_event_data)
2967 {
2968         struct mptsas_hotplug_event *ev;
2969         int status = le32_to_cpu(raid_event_data->SettingsStatus);
2970         int state = (status >> 8) & 0xff;
2971
2972         if (ioc->bus_type != SAS)
2973                 return;
2974
2975         ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2976         if (!ev) {
2977                 printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
2978                 return;
2979         }
2980
2981         INIT_WORK(&ev->work, mptsas_hotplug_work);
2982         ev->ioc = ioc;
2983         ev->id = raid_event_data->VolumeID;
2984         ev->channel = raid_event_data->VolumeBus;
2985         ev->event_type = MPTSAS_IGNORE_EVENT;
2986
2987         switch (raid_event_data->ReasonCode) {
2988         case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
2989                 ev->phys_disk_num_valid = 1;
2990                 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2991                 ev->event_type = MPTSAS_ADD_DEVICE;
2992                 break;
2993         case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
2994                 ev->phys_disk_num_valid = 1;
2995                 ev->phys_disk_num = raid_event_data->PhysDiskNum;
2996                 ev->hidden_raid_component = 1;
2997                 ev->event_type = MPTSAS_DEL_DEVICE;
2998                 break;
2999         case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
3000                 switch (state) {
3001                 case MPI_PD_STATE_ONLINE:
3002                 case MPI_PD_STATE_NOT_COMPATIBLE:
3003                         ev->phys_disk_num_valid = 1;
3004                         ev->phys_disk_num = raid_event_data->PhysDiskNum;
3005                         ev->hidden_raid_component = 1;
3006                         ev->event_type = MPTSAS_ADD_DEVICE;
3007                         break;
3008                 case MPI_PD_STATE_MISSING:
3009                 case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
3010                 case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
3011                 case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
3012                         ev->phys_disk_num_valid = 1;
3013                         ev->phys_disk_num = raid_event_data->PhysDiskNum;
3014                         ev->event_type = MPTSAS_DEL_DEVICE;
3015                         break;
3016                 default:
3017                         break;
3018                 }
3019                 break;
3020         case MPI_EVENT_RAID_RC_VOLUME_DELETED:
3021                 ev->event_type = MPTSAS_DEL_RAID;
3022                 break;
3023         case MPI_EVENT_RAID_RC_VOLUME_CREATED:
3024                 ev->event_type = MPTSAS_ADD_RAID;
3025                 break;
3026         case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
3027                 switch (state) {
3028                 case MPI_RAIDVOL0_STATUS_STATE_FAILED:
3029                 case MPI_RAIDVOL0_STATUS_STATE_MISSING:
3030                         ev->event_type = MPTSAS_DEL_RAID;
3031                         break;
3032                 case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
3033                 case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
3034                         ev->event_type = MPTSAS_ADD_RAID;
3035                         break;
3036                 default:
3037                         break;
3038                 }
3039                 break;
3040         default:
3041                 break;
3042         }
3043         schedule_work(&ev->work);
3044 }
3045
3046 static void
3047 mptsas_send_discovery_event(MPT_ADAPTER *ioc,
3048         EVENT_DATA_SAS_DISCOVERY *discovery_data)
3049 {
3050         struct mptsas_discovery_event *ev;
3051         u32 discovery_status;
3052
3053         /*
3054          * DiscoveryStatus
3055          *
3056          * This flag will be non-zero when firmware
3057          * kicks off discovery, and return to zero
3058          * once its completed.
3059          */
3060         discovery_status = le32_to_cpu(discovery_data->DiscoveryStatus);
3061         ioc->sas_discovery_quiesce_io = discovery_status ? 1 : 0;
3062         if (discovery_status)
3063                 return;
3064
3065         ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3066         if (!ev)
3067                 return;
3068         INIT_WORK(&ev->work, mptsas_discovery_work);
3069         ev->ioc = ioc;
3070         schedule_work(&ev->work);
3071 };
3072
3073 /*
3074  * mptsas_send_ir2_event - handle exposing hidden disk when
3075  * an inactive raid volume is added
3076  *
3077  * @ioc: Pointer to MPT_ADAPTER structure
3078  * @ir2_data
3079  *
3080  */
3081 static void
3082 mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
3083 {
3084         struct mptsas_hotplug_event *ev;
3085
3086         if (ir2_data->ReasonCode !=
3087             MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
3088                 return;
3089
3090         ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3091         if (!ev)
3092                 return;
3093
3094         INIT_WORK(&ev->work, mptsas_hotplug_work);
3095         ev->ioc = ioc;
3096         ev->id = ir2_data->TargetID;
3097         ev->channel = ir2_data->Bus;
3098         ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
3099
3100         schedule_work(&ev->work);
3101 };
3102
3103 static int
3104 mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
3105 {
3106         int rc=1;
3107         u8 event = le32_to_cpu(reply->Event) & 0xFF;
3108
3109         if (!ioc->sh)
3110                 goto out;
3111
3112         /*
3113          * sas_discovery_ignore_events
3114          *
3115          * This flag is to prevent anymore processing of
3116          * sas events once mptsas_remove function is called.
3117          */
3118         if (ioc->sas_discovery_ignore_events) {
3119                 rc = mptscsih_event_process(ioc, reply);
3120                 goto out;
3121         }
3122
3123         switch (event) {
3124         case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
3125                 mptsas_send_sas_event(ioc,
3126                         (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
3127                 break;
3128         case MPI_EVENT_INTEGRATED_RAID:
3129                 mptsas_send_raid_event(ioc,
3130                         (EVENT_DATA_RAID *)reply->Data);
3131                 break;
3132         case MPI_EVENT_PERSISTENT_TABLE_FULL:
3133                 INIT_WORK(&ioc->sas_persist_task,
3134                     mptsas_persist_clear_table);
3135                 schedule_work(&ioc->sas_persist_task);
3136                 break;
3137          case MPI_EVENT_SAS_DISCOVERY:
3138                 mptsas_send_discovery_event(ioc,
3139                         (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
3140                 break;
3141         case MPI_EVENT_IR2:
3142                 mptsas_send_ir2_event(ioc,
3143                     (PTR_MPI_EVENT_DATA_IR2)reply->Data);
3144                 break;
3145         default:
3146                 rc = mptscsih_event_process(ioc, reply);
3147                 break;
3148         }
3149  out:
3150
3151         return rc;
3152 }
3153
3154 static int
3155 mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3156 {
3157         struct Scsi_Host        *sh;
3158         MPT_SCSI_HOST           *hd;
3159         MPT_ADAPTER             *ioc;
3160         unsigned long            flags;
3161         int                      ii;
3162         int                      numSGE = 0;
3163         int                      scale;
3164         int                      ioc_cap;
3165         int                     error=0;
3166         int                     r;
3167
3168         r = mpt_attach(pdev,id);
3169         if (r)
3170                 return r;
3171
3172         ioc = pci_get_drvdata(pdev);
3173         ioc->DoneCtx = mptsasDoneCtx;
3174         ioc->TaskCtx = mptsasTaskCtx;
3175         ioc->InternalCtx = mptsasInternalCtx;
3176
3177         /*  Added sanity check on readiness of the MPT adapter.
3178          */
3179         if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
3180                 printk(MYIOC_s_WARN_FMT
3181                   "Skipping because it's not operational!\n",
3182                   ioc->name);
3183                 error = -ENODEV;
3184                 goto out_mptsas_probe;
3185         }
3186
3187         if (!ioc->active) {
3188                 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
3189                   ioc->name);
3190                 error = -ENODEV;
3191                 goto out_mptsas_probe;
3192         }
3193
3194         /*  Sanity check - ensure at least 1 port is INITIATOR capable
3195          */
3196         ioc_cap = 0;
3197         for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
3198                 if (ioc->pfacts[ii].ProtocolFlags &
3199                                 MPI_PORTFACTS_PROTOCOL_INITIATOR)
3200                         ioc_cap++;
3201         }
3202
3203         if (!ioc_cap) {
3204                 printk(MYIOC_s_WARN_FMT
3205                         "Skipping ioc=%p because SCSI Initiator mode "
3206                         "is NOT enabled!\n", ioc->name, ioc);
3207                 return 0;
3208         }
3209
3210         sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
3211         if (!sh) {
3212                 printk(MYIOC_s_WARN_FMT
3213                         "Unable to register controller with SCSI subsystem\n",
3214                         ioc->name);
3215                 error = -1;
3216                 goto out_mptsas_probe;
3217         }
3218
3219         spin_lock_irqsave(&ioc->FreeQlock, flags);
3220
3221         /* Attach the SCSI Host to the IOC structure
3222          */
3223         ioc->sh = sh;
3224
3225         sh->io_port = 0;
3226         sh->n_io_port = 0;
3227         sh->irq = 0;
3228
3229         /* set 16 byte cdb's */
3230         sh->max_cmd_len = 16;
3231
3232         sh->max_id = ioc->pfacts[0].PortSCSIID;
3233         sh->max_lun = max_lun;
3234
3235         sh->transportt = mptsas_transport_template;
3236
3237         /* Required entry.
3238          */
3239         sh->unique_id = ioc->id;
3240
3241         INIT_LIST_HEAD(&ioc->sas_topology);
3242         mutex_init(&ioc->sas_topology_mutex);
3243         mutex_init(&ioc->sas_discovery_mutex);
3244         mutex_init(&ioc->sas_mgmt.mutex);
3245         init_completion(&ioc->sas_mgmt.done);
3246
3247         /* Verify that we won't exceed the maximum
3248          * number of chain buffers
3249          * We can optimize:  ZZ = req_sz/sizeof(SGE)
3250          * For 32bit SGE's:
3251          *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
3252          *               + (req_sz - 64)/sizeof(SGE)
3253          * A slightly different algorithm is required for
3254          * 64bit SGEs.
3255          */
3256         scale = ioc->req_sz/ioc->SGE_size;
3257         if (ioc->sg_addr_size == sizeof(u64)) {
3258                 numSGE = (scale - 1) *
3259                   (ioc->facts.MaxChainDepth-1) + scale +
3260                   (ioc->req_sz - 60) / ioc->SGE_size;
3261         } else {
3262                 numSGE = 1 + (scale - 1) *
3263                   (ioc->facts.MaxChainDepth-1) + scale +
3264                   (ioc->req_sz - 64) / ioc->SGE_size;
3265         }
3266
3267         if (numSGE < sh->sg_tablesize) {
3268                 /* Reset this value */
3269                 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
3270                   "Resetting sg_tablesize to %d from %d\n",
3271                   ioc->name, numSGE, sh->sg_tablesize));
3272                 sh->sg_tablesize = numSGE;
3273         }
3274
3275         hd = shost_priv(sh);
3276         hd->ioc = ioc;
3277
3278         /* SCSI needs scsi_cmnd lookup table!
3279          * (with size equal to req_depth*PtrSz!)
3280          */
3281         ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
3282         if (!ioc->ScsiLookup) {
3283                 error = -ENOMEM;
3284                 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3285                 goto out_mptsas_probe;
3286         }
3287         spin_lock_init(&ioc->scsi_lookup_lock);
3288
3289         dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
3290                  ioc->name, ioc->ScsiLookup));
3291
3292         /* Clear the TM flags
3293          */
3294         hd->tmPending = 0;
3295         hd->tmState = TM_STATE_NONE;
3296         hd->abortSCpnt = NULL;
3297
3298         /* Clear the pointer used to store
3299          * single-threaded commands, i.e., those
3300          * issued during a bus scan, dv and
3301          * configuration pages.
3302          */
3303         hd->cmdPtr = NULL;
3304
3305         /* Initialize this SCSI Hosts' timers
3306          * To use, set the timer expires field
3307          * and add_timer
3308          */
3309         init_timer(&hd->timer);
3310         hd->timer.data = (unsigned long) hd;
3311         hd->timer.function = mptscsih_timer_expired;
3312
3313         ioc->sas_data.ptClear = mpt_pt_clear;
3314
3315         hd->last_queue_full = 0;
3316         INIT_LIST_HEAD(&hd->target_reset_list);
3317         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
3318
3319         if (ioc->sas_data.ptClear==1) {
3320                 mptbase_sas_persist_operation(
3321                     ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
3322         }
3323
3324         error = scsi_add_host(sh, &ioc->pcidev->dev);
3325         if (error) {
3326                 dprintk(ioc, printk(MYIOC_s_ERR_FMT
3327                   "scsi_add_host failed\n", ioc->name));
3328                 goto out_mptsas_probe;
3329         }
3330
3331         mptsas_scan_sas_topology(ioc);
3332
3333         return 0;
3334
3335  out_mptsas_probe:
3336
3337         mptscsih_remove(pdev);
3338         return error;
3339 }
3340
3341 void
3342 mptsas_shutdown(struct pci_dev *pdev)
3343 {
3344         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
3345
3346         ioc->sas_discovery_quiesce_io = 0;
3347 }
3348
3349 static void __devexit mptsas_remove(struct pci_dev *pdev)
3350 {
3351         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
3352         struct mptsas_portinfo *p, *n;
3353         int i;
3354
3355         mptsas_shutdown(pdev);
3356
3357         ioc->sas_discovery_ignore_events = 1;
3358         sas_remove_host(ioc->sh);
3359
3360         mutex_lock(&ioc->sas_topology_mutex);
3361         list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
3362                 list_del(&p->list);
3363                 for (i = 0 ; i < p->num_phys ; i++)
3364                         mptsas_port_delete(ioc, p->phy_info[i].port_details);
3365                 kfree(p->phy_info);
3366                 kfree(p);
3367         }
3368         mutex_unlock(&ioc->sas_topology_mutex);
3369
3370         mptscsih_remove(pdev);
3371 }
3372
3373 static struct pci_device_id mptsas_pci_table[] = {
3374         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
3375                 PCI_ANY_ID, PCI_ANY_ID },
3376         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
3377                 PCI_ANY_ID, PCI_ANY_ID },
3378         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
3379                 PCI_ANY_ID, PCI_ANY_ID },
3380         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
3381                 PCI_ANY_ID, PCI_ANY_ID },
3382         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
3383                 PCI_ANY_ID, PCI_ANY_ID },
3384         {0}     /* Terminating entry */
3385 };
3386 MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
3387
3388
3389 static struct pci_driver mptsas_driver = {
3390         .name           = "mptsas",
3391         .id_table       = mptsas_pci_table,
3392         .probe          = mptsas_probe,
3393         .remove         = __devexit_p(mptsas_remove),
3394         .shutdown       = mptsas_shutdown,
3395 #ifdef CONFIG_PM
3396         .suspend        = mptscsih_suspend,
3397         .resume         = mptscsih_resume,
3398 #endif
3399 };
3400
3401 static int __init
3402 mptsas_init(void)
3403 {
3404         int error;
3405
3406         show_mptmod_ver(my_NAME, my_VERSION);
3407
3408         mptsas_transport_template =
3409             sas_attach_transport(&mptsas_transport_functions);
3410         if (!mptsas_transport_template)
3411                 return -ENODEV;
3412
3413         mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
3414         mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER);
3415         mptsasInternalCtx =
3416                 mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
3417         mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
3418         mptsasDeviceResetCtx =
3419                 mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
3420
3421         mpt_event_register(mptsasDoneCtx, mptsas_event_process);
3422         mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
3423
3424         error = pci_register_driver(&mptsas_driver);
3425         if (error)
3426                 sas_release_transport(mptsas_transport_template);
3427
3428         return error;
3429 }
3430
3431 static void __exit
3432 mptsas_exit(void)
3433 {
3434         pci_unregister_driver(&mptsas_driver);
3435         sas_release_transport(mptsas_transport_template);
3436
3437         mpt_reset_deregister(mptsasDoneCtx);
3438         mpt_event_deregister(mptsasDoneCtx);
3439
3440         mpt_deregister(mptsasMgmtCtx);
3441         mpt_deregister(mptsasInternalCtx);
3442         mpt_deregister(mptsasTaskCtx);
3443         mpt_deregister(mptsasDoneCtx);
3444         mpt_deregister(mptsasDeviceResetCtx);
3445 }
3446
3447 module_init(mptsas_init);
3448 module_exit(mptsas_exit);