]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/mpt2sas/mpt2sas_scsih.c
[SCSI] mpt2sas : Rescan topology from Interrupt context instead of work thread
[karo-tx-linux.git] / drivers / scsi / mpt2sas / mpt2sas_scsih.c
1 /*
2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3  *
4  * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
5  * Copyright (C) 2007-2008  LSI Corporation
6  *  (mailto:DL-MPTFusionLinux@lsi.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * NO WARRANTY
19  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23  * solely responsible for determining the appropriateness of using and
24  * distributing the Program and assumes all risks associated with its
25  * exercise of rights under this Agreement, including but not limited to
26  * the risks and costs of program errors, damage to or loss of data,
27  * programs or equipment, and unavailability or interruption of operations.
28
29  * DISCLAIMER OF LIABILITY
30  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
41  * USA.
42  */
43
44 #include <linux/version.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55
56 #include "mpt2sas_base.h"
57
58 MODULE_AUTHOR(MPT2SAS_AUTHOR);
59 MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63 #define RAID_CHANNEL 1
64
65 /* forward proto's */
66 static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67     struct _sas_node *sas_expander);
68 static void _firmware_event_work(struct work_struct *work);
69
70 /* global parameters */
71 LIST_HEAD(mpt2sas_ioc_list);
72
73 /* local parameters */
74 static u8 scsi_io_cb_idx = -1;
75 static u8 tm_cb_idx = -1;
76 static u8 ctl_cb_idx = -1;
77 static u8 base_cb_idx = -1;
78 static u8 transport_cb_idx = -1;
79 static u8 config_cb_idx = -1;
80 static int mpt_ids;
81
82 /* command line options */
83 static u32 logging_level;
84 MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
85     "(default=0)");
86
87 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88 #define MPT2SAS_MAX_LUN (16895)
89 static int max_lun = MPT2SAS_MAX_LUN;
90 module_param(max_lun, int, 0);
91 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92
93 /**
94  * struct sense_info - common structure for obtaining sense keys
95  * @skey: sense key
96  * @asc: additional sense code
97  * @ascq: additional sense code qualifier
98  */
99 struct sense_info {
100         u8 skey;
101         u8 asc;
102         u8 ascq;
103 };
104
105
106 /**
107  * struct fw_event_work - firmware event struct
108  * @list: link list framework
109  * @work: work object (ioc->fault_reset_work_q)
110  * @ioc: per adapter object
111  * @VF_ID: virtual function id
112  * @host_reset_handling: handling events during host reset
113  * @ignore: flag meaning this event has been marked to ignore
114  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
115  * @event_data: reply event data payload follows
116  *
117  * This object stored on ioc->fw_event_list.
118  */
119 struct fw_event_work {
120         struct list_head        list;
121         struct work_struct      work;
122         struct MPT2SAS_ADAPTER *ioc;
123         u8                      VF_ID;
124         u8                      host_reset_handling;
125         u8                      ignore;
126         u16                     event;
127         void                    *event_data;
128 };
129
130 /**
131  * struct _scsi_io_transfer - scsi io transfer
132  * @handle: sas device handle (assigned by firmware)
133  * @is_raid: flag set for hidden raid components
134  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
135  * @data_length: data transfer length
136  * @data_dma: dma pointer to data
137  * @sense: sense data
138  * @lun: lun number
139  * @cdb_length: cdb length
140  * @cdb: cdb contents
141  * @valid_reply: flag set for reply message
142  * @timeout: timeout for this command
143  * @sense_length: sense length
144  * @ioc_status: ioc status
145  * @scsi_state: scsi state
146  * @scsi_status: scsi staus
147  * @log_info: log information
148  * @transfer_length: data length transfer when there is a reply message
149  *
150  * Used for sending internal scsi commands to devices within this module.
151  * Refer to _scsi_send_scsi_io().
152  */
153 struct _scsi_io_transfer {
154         u16     handle;
155         u8      is_raid;
156         enum dma_data_direction dir;
157         u32     data_length;
158         dma_addr_t data_dma;
159         u8      sense[SCSI_SENSE_BUFFERSIZE];
160         u32     lun;
161         u8      cdb_length;
162         u8      cdb[32];
163         u8      timeout;
164         u8      valid_reply;
165   /* the following bits are only valid when 'valid_reply = 1' */
166         u32     sense_length;
167         u16     ioc_status;
168         u8      scsi_state;
169         u8      scsi_status;
170         u32     log_info;
171         u32     transfer_length;
172 };
173
174 /*
175  * The pci device ids are defined in mpi/mpi2_cnfg.h.
176  */
177 static struct pci_device_id scsih_pci_table[] = {
178         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
179                 PCI_ANY_ID, PCI_ANY_ID },
180         /* Falcon ~ 2008*/
181         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
182                 PCI_ANY_ID, PCI_ANY_ID },
183         /* Liberator ~ 2108 */
184         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
185                 PCI_ANY_ID, PCI_ANY_ID },
186         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
187                 PCI_ANY_ID, PCI_ANY_ID },
188         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
189                 PCI_ANY_ID, PCI_ANY_ID },
190         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
191                 PCI_ANY_ID, PCI_ANY_ID },
192         { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
193                 PCI_ANY_ID, PCI_ANY_ID },
194         {0}     /* Terminating entry */
195 };
196 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
197
198 /**
199  * _scsih_set_debug_level - global setting of ioc->logging_level.
200  *
201  * Note: The logging levels are defined in mpt2sas_debug.h.
202  */
203 static int
204 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
205 {
206         int ret = param_set_int(val, kp);
207         struct MPT2SAS_ADAPTER *ioc;
208
209         if (ret)
210                 return ret;
211
212         printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
213         list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
214                 ioc->logging_level = logging_level;
215         return 0;
216 }
217 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
218     &logging_level, 0644);
219
220 /**
221  * _scsih_srch_boot_sas_address - search based on sas_address
222  * @sas_address: sas address
223  * @boot_device: boot device object from bios page 2
224  *
225  * Returns 1 when there's a match, 0 means no match.
226  */
227 static inline int
228 _scsih_srch_boot_sas_address(u64 sas_address,
229     Mpi2BootDeviceSasWwid_t *boot_device)
230 {
231         return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
232 }
233
234 /**
235  * _scsih_srch_boot_device_name - search based on device name
236  * @device_name: device name specified in INDENTIFY fram
237  * @boot_device: boot device object from bios page 2
238  *
239  * Returns 1 when there's a match, 0 means no match.
240  */
241 static inline int
242 _scsih_srch_boot_device_name(u64 device_name,
243     Mpi2BootDeviceDeviceName_t *boot_device)
244 {
245         return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
246 }
247
248 /**
249  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
250  * @enclosure_logical_id: enclosure logical id
251  * @slot_number: slot number
252  * @boot_device: boot device object from bios page 2
253  *
254  * Returns 1 when there's a match, 0 means no match.
255  */
256 static inline int
257 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
258     Mpi2BootDeviceEnclosureSlot_t *boot_device)
259 {
260         return (enclosure_logical_id == le64_to_cpu(boot_device->
261             EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
262             SlotNumber)) ? 1 : 0;
263 }
264
265 /**
266  * _scsih_is_boot_device - search for matching boot device.
267  * @sas_address: sas address
268  * @device_name: device name specified in INDENTIFY fram
269  * @enclosure_logical_id: enclosure logical id
270  * @slot_number: slot number
271  * @form: specifies boot device form
272  * @boot_device: boot device object from bios page 2
273  *
274  * Returns 1 when there's a match, 0 means no match.
275  */
276 static int
277 _scsih_is_boot_device(u64 sas_address, u64 device_name,
278     u64 enclosure_logical_id, u16 slot, u8 form,
279     Mpi2BiosPage2BootDevice_t *boot_device)
280 {
281         int rc = 0;
282
283         switch (form) {
284         case MPI2_BIOSPAGE2_FORM_SAS_WWID:
285                 if (!sas_address)
286                         break;
287                 rc = _scsih_srch_boot_sas_address(
288                     sas_address, &boot_device->SasWwid);
289                 break;
290         case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
291                 if (!enclosure_logical_id)
292                         break;
293                 rc = _scsih_srch_boot_encl_slot(
294                     enclosure_logical_id,
295                     slot, &boot_device->EnclosureSlot);
296                 break;
297         case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
298                 if (!device_name)
299                         break;
300                 rc = _scsih_srch_boot_device_name(
301                     device_name, &boot_device->DeviceName);
302                 break;
303         case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
304                 break;
305         }
306
307         return rc;
308 }
309
310 /**
311  * _scsih_determine_boot_device - determine boot device.
312  * @ioc: per adapter object
313  * @device: either sas_device or raid_device object
314  * @is_raid: [flag] 1 = raid object, 0 = sas object
315  *
316  * Determines whether this device should be first reported device to
317  * to scsi-ml or sas transport, this purpose is for persistant boot device.
318  * There are primary, alternate, and current entries in bios page 2. The order
319  * priority is primary, alternate, then current.  This routine saves
320  * the corresponding device object and is_raid flag in the ioc object.
321  * The saved data to be used later in _scsih_probe_boot_devices().
322  */
323 static void
324 _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
325     void *device, u8 is_raid)
326 {
327         struct _sas_device *sas_device;
328         struct _raid_device *raid_device;
329         u64 sas_address;
330         u64 device_name;
331         u64 enclosure_logical_id;
332         u16 slot;
333
334          /* only process this function when driver loads */
335         if (!ioc->wait_for_port_enable_to_complete)
336                 return;
337
338         if (!is_raid) {
339                 sas_device = device;
340                 sas_address = sas_device->sas_address;
341                 device_name = sas_device->device_name;
342                 enclosure_logical_id = sas_device->enclosure_logical_id;
343                 slot = sas_device->slot;
344         } else {
345                 raid_device = device;
346                 sas_address = raid_device->wwid;
347                 device_name = 0;
348                 enclosure_logical_id = 0;
349                 slot = 0;
350         }
351
352         if (!ioc->req_boot_device.device) {
353                 if (_scsih_is_boot_device(sas_address, device_name,
354                     enclosure_logical_id, slot,
355                     (ioc->bios_pg2.ReqBootDeviceForm &
356                     MPI2_BIOSPAGE2_FORM_MASK),
357                     &ioc->bios_pg2.RequestedBootDevice)) {
358                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
359                            "%s: req_boot_device(0x%016llx)\n",
360                             ioc->name, __func__,
361                             (unsigned long long)sas_address));
362                         ioc->req_boot_device.device = device;
363                         ioc->req_boot_device.is_raid = is_raid;
364                 }
365         }
366
367         if (!ioc->req_alt_boot_device.device) {
368                 if (_scsih_is_boot_device(sas_address, device_name,
369                     enclosure_logical_id, slot,
370                     (ioc->bios_pg2.ReqAltBootDeviceForm &
371                     MPI2_BIOSPAGE2_FORM_MASK),
372                     &ioc->bios_pg2.RequestedAltBootDevice)) {
373                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
374                            "%s: req_alt_boot_device(0x%016llx)\n",
375                             ioc->name, __func__,
376                             (unsigned long long)sas_address));
377                         ioc->req_alt_boot_device.device = device;
378                         ioc->req_alt_boot_device.is_raid = is_raid;
379                 }
380         }
381
382         if (!ioc->current_boot_device.device) {
383                 if (_scsih_is_boot_device(sas_address, device_name,
384                     enclosure_logical_id, slot,
385                     (ioc->bios_pg2.CurrentBootDeviceForm &
386                     MPI2_BIOSPAGE2_FORM_MASK),
387                     &ioc->bios_pg2.CurrentBootDevice)) {
388                         dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
389                            "%s: current_boot_device(0x%016llx)\n",
390                             ioc->name, __func__,
391                             (unsigned long long)sas_address));
392                         ioc->current_boot_device.device = device;
393                         ioc->current_boot_device.is_raid = is_raid;
394                 }
395         }
396 }
397
398 /**
399  * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
400  * @ioc: per adapter object
401  * @sas_address: sas address
402  * Context: Calling function should acquire ioc->sas_device_lock
403  *
404  * This searches for sas_device based on sas_address, then return sas_device
405  * object.
406  */
407 struct _sas_device *
408 mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
409     u64 sas_address)
410 {
411         struct _sas_device *sas_device, *r;
412
413         r = NULL;
414         /* check the sas_device_init_list */
415         list_for_each_entry(sas_device, &ioc->sas_device_init_list,
416             list) {
417                 if (sas_device->sas_address != sas_address)
418                         continue;
419                 r = sas_device;
420                 goto out;
421         }
422
423         /* then check the sas_device_list */
424         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
425                 if (sas_device->sas_address != sas_address)
426                         continue;
427                 r = sas_device;
428                 goto out;
429         }
430  out:
431         return r;
432 }
433
434 /**
435  * _scsih_sas_device_find_by_handle - sas device search
436  * @ioc: per adapter object
437  * @handle: sas device handle (assigned by firmware)
438  * Context: Calling function should acquire ioc->sas_device_lock
439  *
440  * This searches for sas_device based on sas_address, then return sas_device
441  * object.
442  */
443 static struct _sas_device *
444 _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
445 {
446         struct _sas_device *sas_device, *r;
447
448         r = NULL;
449         if (ioc->wait_for_port_enable_to_complete) {
450                 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
451                     list) {
452                         if (sas_device->handle != handle)
453                                 continue;
454                         r = sas_device;
455                         goto out;
456                 }
457         } else {
458                 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
459                         if (sas_device->handle != handle)
460                                 continue;
461                         r = sas_device;
462                         goto out;
463                 }
464         }
465
466  out:
467         return r;
468 }
469
470 /**
471  * _scsih_sas_device_remove - remove sas_device from list.
472  * @ioc: per adapter object
473  * @sas_device: the sas_device object
474  * Context: This function will acquire ioc->sas_device_lock.
475  *
476  * Removing object and freeing associated memory from the ioc->sas_device_list.
477  */
478 static void
479 _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
480     struct _sas_device *sas_device)
481 {
482         unsigned long flags;
483
484         spin_lock_irqsave(&ioc->sas_device_lock, flags);
485         list_del(&sas_device->list);
486         memset(sas_device, 0, sizeof(struct _sas_device));
487         kfree(sas_device);
488         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
489 }
490
491 /**
492  * _scsih_sas_device_add - insert sas_device to the list.
493  * @ioc: per adapter object
494  * @sas_device: the sas_device object
495  * Context: This function will acquire ioc->sas_device_lock.
496  *
497  * Adding new object to the ioc->sas_device_list.
498  */
499 static void
500 _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
501     struct _sas_device *sas_device)
502 {
503         unsigned long flags;
504         u16 handle, parent_handle;
505         u64 sas_address;
506
507         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
508             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
509             sas_device->handle, (unsigned long long)sas_device->sas_address));
510
511         spin_lock_irqsave(&ioc->sas_device_lock, flags);
512         list_add_tail(&sas_device->list, &ioc->sas_device_list);
513         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
514
515         handle = sas_device->handle;
516         parent_handle = sas_device->parent_handle;
517         sas_address = sas_device->sas_address;
518         if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
519                 _scsih_sas_device_remove(ioc, sas_device);
520 }
521
522 /**
523  * _scsih_sas_device_init_add - insert sas_device to the list.
524  * @ioc: per adapter object
525  * @sas_device: the sas_device object
526  * Context: This function will acquire ioc->sas_device_lock.
527  *
528  * Adding new object at driver load time to the ioc->sas_device_init_list.
529  */
530 static void
531 _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
532     struct _sas_device *sas_device)
533 {
534         unsigned long flags;
535
536         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
537             "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
538             sas_device->handle, (unsigned long long)sas_device->sas_address));
539
540         spin_lock_irqsave(&ioc->sas_device_lock, flags);
541         list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
542         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
543         _scsih_determine_boot_device(ioc, sas_device, 0);
544 }
545
546 /**
547  * mpt2sas_scsih_expander_find_by_handle - expander device search
548  * @ioc: per adapter object
549  * @handle: expander handle (assigned by firmware)
550  * Context: Calling function should acquire ioc->sas_device_lock
551  *
552  * This searches for expander device based on handle, then returns the
553  * sas_node object.
554  */
555 struct _sas_node *
556 mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
557 {
558         struct _sas_node *sas_expander, *r;
559
560         r = NULL;
561         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
562                 if (sas_expander->handle != handle)
563                         continue;
564                 r = sas_expander;
565                 goto out;
566         }
567  out:
568         return r;
569 }
570
571 /**
572  * _scsih_raid_device_find_by_id - raid device search
573  * @ioc: per adapter object
574  * @id: sas device target id
575  * @channel: sas device channel
576  * Context: Calling function should acquire ioc->raid_device_lock
577  *
578  * This searches for raid_device based on target id, then return raid_device
579  * object.
580  */
581 static struct _raid_device *
582 _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
583 {
584         struct _raid_device *raid_device, *r;
585
586         r = NULL;
587         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
588                 if (raid_device->id == id && raid_device->channel == channel) {
589                         r = raid_device;
590                         goto out;
591                 }
592         }
593
594  out:
595         return r;
596 }
597
598 /**
599  * _scsih_raid_device_find_by_handle - raid device search
600  * @ioc: per adapter object
601  * @handle: sas device handle (assigned by firmware)
602  * Context: Calling function should acquire ioc->raid_device_lock
603  *
604  * This searches for raid_device based on handle, then return raid_device
605  * object.
606  */
607 static struct _raid_device *
608 _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
609 {
610         struct _raid_device *raid_device, *r;
611
612         r = NULL;
613         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
614                 if (raid_device->handle != handle)
615                         continue;
616                 r = raid_device;
617                 goto out;
618         }
619
620  out:
621         return r;
622 }
623
624 /**
625  * _scsih_raid_device_find_by_wwid - raid device search
626  * @ioc: per adapter object
627  * @handle: sas device handle (assigned by firmware)
628  * Context: Calling function should acquire ioc->raid_device_lock
629  *
630  * This searches for raid_device based on wwid, then return raid_device
631  * object.
632  */
633 static struct _raid_device *
634 _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
635 {
636         struct _raid_device *raid_device, *r;
637
638         r = NULL;
639         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
640                 if (raid_device->wwid != wwid)
641                         continue;
642                 r = raid_device;
643                 goto out;
644         }
645
646  out:
647         return r;
648 }
649
650 /**
651  * _scsih_raid_device_add - add raid_device object
652  * @ioc: per adapter object
653  * @raid_device: raid_device object
654  *
655  * This is added to the raid_device_list link list.
656  */
657 static void
658 _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
659     struct _raid_device *raid_device)
660 {
661         unsigned long flags;
662
663         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
664             "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
665             raid_device->handle, (unsigned long long)raid_device->wwid));
666
667         spin_lock_irqsave(&ioc->raid_device_lock, flags);
668         list_add_tail(&raid_device->list, &ioc->raid_device_list);
669         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
670 }
671
672 /**
673  * _scsih_raid_device_remove - delete raid_device object
674  * @ioc: per adapter object
675  * @raid_device: raid_device object
676  *
677  * This is removed from the raid_device_list link list.
678  */
679 static void
680 _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
681     struct _raid_device *raid_device)
682 {
683         unsigned long flags;
684
685         spin_lock_irqsave(&ioc->raid_device_lock, flags);
686         list_del(&raid_device->list);
687         memset(raid_device, 0, sizeof(struct _raid_device));
688         kfree(raid_device);
689         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
690 }
691
692 /**
693  * mpt2sas_scsih_expander_find_by_sas_address - expander device search
694  * @ioc: per adapter object
695  * @sas_address: sas address
696  * Context: Calling function should acquire ioc->sas_node_lock.
697  *
698  * This searches for expander device based on sas_address, then returns the
699  * sas_node object.
700  */
701 struct _sas_node *
702 mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
703     u64 sas_address)
704 {
705         struct _sas_node *sas_expander, *r;
706
707         r = NULL;
708         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
709                 if (sas_expander->sas_address != sas_address)
710                         continue;
711                 r = sas_expander;
712                 goto out;
713         }
714  out:
715         return r;
716 }
717
718 /**
719  * _scsih_expander_node_add - insert expander device to the list.
720  * @ioc: per adapter object
721  * @sas_expander: the sas_device object
722  * Context: This function will acquire ioc->sas_node_lock.
723  *
724  * Adding new object to the ioc->sas_expander_list.
725  *
726  * Return nothing.
727  */
728 static void
729 _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
730     struct _sas_node *sas_expander)
731 {
732         unsigned long flags;
733
734         spin_lock_irqsave(&ioc->sas_node_lock, flags);
735         list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
736         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
737 }
738
739 /**
740  * _scsih_is_end_device - determines if device is an end device
741  * @device_info: bitfield providing information about the device.
742  * Context: none
743  *
744  * Returns 1 if end device.
745  */
746 static int
747 _scsih_is_end_device(u32 device_info)
748 {
749         if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
750                 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
751                 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
752                 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
753                 return 1;
754         else
755                 return 0;
756 }
757
758 /**
759  * _scsih_scsi_lookup_get - returns scmd entry
760  * @ioc: per adapter object
761  * @smid: system request message index
762  * Context: This function will acquire ioc->scsi_lookup_lock.
763  *
764  * Returns the smid stored scmd pointer.
765  */
766 static struct scsi_cmnd *
767 _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
768 {
769         unsigned long   flags;
770         struct scsi_cmnd *scmd;
771
772         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
773         scmd = ioc->scsi_lookup[smid - 1].scmd;
774         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
775         return scmd;
776 }
777
778 /**
779  * mptscsih_getclear_scsi_lookup - returns scmd entry
780  * @ioc: per adapter object
781  * @smid: system request message index
782  * Context: This function will acquire ioc->scsi_lookup_lock.
783  *
784  * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
785  */
786 static struct scsi_cmnd *
787 _scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
788 {
789         unsigned long   flags;
790         struct scsi_cmnd *scmd;
791
792         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
793         scmd = ioc->scsi_lookup[smid - 1].scmd;
794         ioc->scsi_lookup[smid - 1].scmd = NULL;
795         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
796         return scmd;
797 }
798
799 /**
800  * _scsih_scsi_lookup_set - updates scmd entry in lookup
801  * @ioc: per adapter object
802  * @smid: system request message index
803  * @scmd: pointer to scsi command object
804  * Context: This function will acquire ioc->scsi_lookup_lock.
805  *
806  * This will save scmd pointer in the scsi_lookup array.
807  *
808  * Return nothing.
809  */
810 static void
811 _scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
812     struct scsi_cmnd *scmd)
813 {
814         unsigned long   flags;
815
816         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
817         ioc->scsi_lookup[smid - 1].scmd = scmd;
818         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
819 }
820
821 /**
822  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
823  * @ioc: per adapter object
824  * @smid: system request message index
825  * @scmd: pointer to scsi command object
826  * Context: This function will acquire ioc->scsi_lookup_lock.
827  *
828  * This will search for a scmd pointer in the scsi_lookup array,
829  * returning the revelent smid.  A returned value of zero means invalid.
830  */
831 static u16
832 _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
833     *scmd)
834 {
835         u16 smid;
836         unsigned long   flags;
837         int i;
838
839         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
840         smid = 0;
841         for (i = 0; i < ioc->request_depth; i++) {
842                 if (ioc->scsi_lookup[i].scmd == scmd) {
843                         smid = i + 1;
844                         goto out;
845                 }
846         }
847  out:
848         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
849         return smid;
850 }
851
852 /**
853  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
854  * @ioc: per adapter object
855  * @id: target id
856  * @channel: channel
857  * Context: This function will acquire ioc->scsi_lookup_lock.
858  *
859  * This will search for a matching channel:id in the scsi_lookup array,
860  * returning 1 if found.
861  */
862 static u8
863 _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
864     int channel)
865 {
866         u8 found;
867         unsigned long   flags;
868         int i;
869
870         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
871         found = 0;
872         for (i = 0 ; i < ioc->request_depth; i++) {
873                 if (ioc->scsi_lookup[i].scmd &&
874                     (ioc->scsi_lookup[i].scmd->device->id == id &&
875                     ioc->scsi_lookup[i].scmd->device->channel == channel)) {
876                         found = 1;
877                         goto out;
878                 }
879         }
880  out:
881         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
882         return found;
883 }
884
885 /**
886  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
887  * @ioc: per adapter object
888  * @id: target id
889  * @lun: lun number
890  * @channel: channel
891  * Context: This function will acquire ioc->scsi_lookup_lock.
892  *
893  * This will search for a matching channel:id:lun in the scsi_lookup array,
894  * returning 1 if found.
895  */
896 static u8
897 _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
898     unsigned int lun, int channel)
899 {
900         u8 found;
901         unsigned long   flags;
902         int i;
903
904         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
905         found = 0;
906         for (i = 0 ; i < ioc->request_depth; i++) {
907                 if (ioc->scsi_lookup[i].scmd &&
908                     (ioc->scsi_lookup[i].scmd->device->id == id &&
909                     ioc->scsi_lookup[i].scmd->device->channel == channel &&
910                     ioc->scsi_lookup[i].scmd->device->lun == lun)) {
911                         found = 1;
912                         goto out;
913                 }
914         }
915  out:
916         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
917         return found;
918 }
919
920 /**
921  * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
922  * @ioc: per adapter object
923  * @smid: system request message index
924  *
925  * Returns phys pointer to chain buffer.
926  */
927 static dma_addr_t
928 _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
929 {
930         return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
931             ioc->chains_needed_per_io));
932 }
933
934 /**
935  * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
936  * @ioc: per adapter object
937  * @smid: system request message index
938  *
939  * Returns virt pointer to chain buffer.
940  */
941 static void *
942 _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943 {
944         return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
945             ioc->chains_needed_per_io)));
946 }
947
948 /**
949  * _scsih_build_scatter_gather - main sg creation routine
950  * @ioc: per adapter object
951  * @scmd: scsi command
952  * @smid: system request message index
953  * Context: none.
954  *
955  * The main routine that builds scatter gather table from a given
956  * scsi request sent via the .queuecommand main handler.
957  *
958  * Returns 0 success, anything else error
959  */
960 static int
961 _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
962     struct scsi_cmnd *scmd, u16 smid)
963 {
964         Mpi2SCSIIORequest_t *mpi_request;
965         dma_addr_t chain_dma;
966         struct scatterlist *sg_scmd;
967         void *sg_local, *chain;
968         u32 chain_offset;
969         u32 chain_length;
970         u32 chain_flags;
971         u32 sges_left;
972         u32 sges_in_segment;
973         u32 sgl_flags;
974         u32 sgl_flags_last_element;
975         u32 sgl_flags_end_buffer;
976
977         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
978
979         /* init scatter gather flags */
980         sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
981         if (scmd->sc_data_direction == DMA_TO_DEVICE)
982                 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
983         sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
984             << MPI2_SGE_FLAGS_SHIFT;
985         sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
986             MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
987             << MPI2_SGE_FLAGS_SHIFT;
988         sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
989
990         sg_scmd = scsi_sglist(scmd);
991         sges_left = scsi_dma_map(scmd);
992         if (!sges_left) {
993                 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
994                 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
995                 return -ENOMEM;
996         }
997
998         sg_local = &mpi_request->SGL;
999         sges_in_segment = ioc->max_sges_in_main_message;
1000         if (sges_left <= sges_in_segment)
1001                 goto fill_in_last_segment;
1002
1003         mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1004             (sges_in_segment * ioc->sge_size))/4;
1005
1006         /* fill in main message segment when there is a chain following */
1007         while (sges_in_segment) {
1008                 if (sges_in_segment == 1)
1009                         ioc->base_add_sg_single(sg_local,
1010                             sgl_flags_last_element | sg_dma_len(sg_scmd),
1011                             sg_dma_address(sg_scmd));
1012                 else
1013                         ioc->base_add_sg_single(sg_local, sgl_flags |
1014                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1015                 sg_scmd = sg_next(sg_scmd);
1016                 sg_local += ioc->sge_size;
1017                 sges_left--;
1018                 sges_in_segment--;
1019         }
1020
1021         /* initializing the chain flags and pointers */
1022         chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1023         chain = _scsih_get_chain_buffer(ioc, smid);
1024         chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1025         do {
1026                 sges_in_segment = (sges_left <=
1027                     ioc->max_sges_in_chain_message) ? sges_left :
1028                     ioc->max_sges_in_chain_message;
1029                 chain_offset = (sges_left == sges_in_segment) ?
1030                     0 : (sges_in_segment * ioc->sge_size)/4;
1031                 chain_length = sges_in_segment * ioc->sge_size;
1032                 if (chain_offset) {
1033                         chain_offset = chain_offset <<
1034                             MPI2_SGE_CHAIN_OFFSET_SHIFT;
1035                         chain_length += ioc->sge_size;
1036                 }
1037                 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1038                     chain_length, chain_dma);
1039                 sg_local = chain;
1040                 if (!chain_offset)
1041                         goto fill_in_last_segment;
1042
1043                 /* fill in chain segments */
1044                 while (sges_in_segment) {
1045                         if (sges_in_segment == 1)
1046                                 ioc->base_add_sg_single(sg_local,
1047                                     sgl_flags_last_element |
1048                                     sg_dma_len(sg_scmd),
1049                                     sg_dma_address(sg_scmd));
1050                         else
1051                                 ioc->base_add_sg_single(sg_local, sgl_flags |
1052                                     sg_dma_len(sg_scmd),
1053                                     sg_dma_address(sg_scmd));
1054                         sg_scmd = sg_next(sg_scmd);
1055                         sg_local += ioc->sge_size;
1056                         sges_left--;
1057                         sges_in_segment--;
1058                 }
1059
1060                 chain_dma += ioc->request_sz;
1061                 chain += ioc->request_sz;
1062         } while (1);
1063
1064
1065  fill_in_last_segment:
1066
1067         /* fill the last segment */
1068         while (sges_left) {
1069                 if (sges_left == 1)
1070                         ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1071                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1072                 else
1073                         ioc->base_add_sg_single(sg_local, sgl_flags |
1074                             sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1075                 sg_scmd = sg_next(sg_scmd);
1076                 sg_local += ioc->sge_size;
1077                 sges_left--;
1078         }
1079
1080         return 0;
1081 }
1082
1083 /**
1084  * _scsih_change_queue_depth - setting device queue depth
1085  * @sdev: scsi device struct
1086  * @qdepth: requested queue depth
1087  *
1088  * Returns queue depth.
1089  */
1090 static int
1091 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1092 {
1093         struct Scsi_Host *shost = sdev->host;
1094         int max_depth;
1095         int tag_type;
1096
1097         max_depth = shost->can_queue;
1098         if (!sdev->tagged_supported)
1099                 max_depth = 1;
1100         if (qdepth > max_depth)
1101                 qdepth = max_depth;
1102         tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1103         scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1104
1105         if (sdev->inquiry_len > 7)
1106                 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1107                 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1108                 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1109                 sdev->ordered_tags, sdev->scsi_level,
1110                 (sdev->inquiry[7] & 2) >> 1);
1111
1112         return sdev->queue_depth;
1113 }
1114
1115 /**
1116  * _scsih_change_queue_depth - changing device queue tag type
1117  * @sdev: scsi device struct
1118  * @tag_type: requested tag type
1119  *
1120  * Returns queue tag type.
1121  */
1122 static int
1123 _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1124 {
1125         if (sdev->tagged_supported) {
1126                 scsi_set_tag_type(sdev, tag_type);
1127                 if (tag_type)
1128                         scsi_activate_tcq(sdev, sdev->queue_depth);
1129                 else
1130                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1131         } else
1132                 tag_type = 0;
1133
1134         return tag_type;
1135 }
1136
1137 /**
1138  * _scsih_target_alloc - target add routine
1139  * @starget: scsi target struct
1140  *
1141  * Returns 0 if ok. Any other return is assumed to be an error and
1142  * the device is ignored.
1143  */
1144 static int
1145 _scsih_target_alloc(struct scsi_target *starget)
1146 {
1147         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1148         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1149         struct MPT2SAS_TARGET *sas_target_priv_data;
1150         struct _sas_device *sas_device;
1151         struct _raid_device *raid_device;
1152         unsigned long flags;
1153         struct sas_rphy *rphy;
1154
1155         sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1156         if (!sas_target_priv_data)
1157                 return -ENOMEM;
1158
1159         starget->hostdata = sas_target_priv_data;
1160         sas_target_priv_data->starget = starget;
1161         sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1162
1163         /* RAID volumes */
1164         if (starget->channel == RAID_CHANNEL) {
1165                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1166                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1167                     starget->channel);
1168                 if (raid_device) {
1169                         sas_target_priv_data->handle = raid_device->handle;
1170                         sas_target_priv_data->sas_address = raid_device->wwid;
1171                         sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1172                         raid_device->starget = starget;
1173                 }
1174                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1175                 return 0;
1176         }
1177
1178         /* sas/sata devices */
1179         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1180         rphy = dev_to_rphy(starget->dev.parent);
1181         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1182            rphy->identify.sas_address);
1183
1184         if (sas_device) {
1185                 sas_target_priv_data->handle = sas_device->handle;
1186                 sas_target_priv_data->sas_address = sas_device->sas_address;
1187                 sas_device->starget = starget;
1188                 sas_device->id = starget->id;
1189                 sas_device->channel = starget->channel;
1190                 if (sas_device->hidden_raid_component)
1191                         sas_target_priv_data->flags |=
1192                             MPT_TARGET_FLAGS_RAID_COMPONENT;
1193         }
1194         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1195
1196         return 0;
1197 }
1198
1199 /**
1200  * _scsih_target_destroy - target destroy routine
1201  * @starget: scsi target struct
1202  *
1203  * Returns nothing.
1204  */
1205 static void
1206 _scsih_target_destroy(struct scsi_target *starget)
1207 {
1208         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1209         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1210         struct MPT2SAS_TARGET *sas_target_priv_data;
1211         struct _sas_device *sas_device;
1212         struct _raid_device *raid_device;
1213         unsigned long flags;
1214         struct sas_rphy *rphy;
1215
1216         sas_target_priv_data = starget->hostdata;
1217         if (!sas_target_priv_data)
1218                 return;
1219
1220         if (starget->channel == RAID_CHANNEL) {
1221                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1222                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1223                     starget->channel);
1224                 if (raid_device) {
1225                         raid_device->starget = NULL;
1226                         raid_device->sdev = NULL;
1227                 }
1228                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1229                 goto out;
1230         }
1231
1232         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1233         rphy = dev_to_rphy(starget->dev.parent);
1234         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1235            rphy->identify.sas_address);
1236         if (sas_device && (sas_device->starget == starget) &&
1237             (sas_device->id == starget->id) &&
1238             (sas_device->channel == starget->channel))
1239                 sas_device->starget = NULL;
1240
1241         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1242
1243  out:
1244         kfree(sas_target_priv_data);
1245         starget->hostdata = NULL;
1246 }
1247
1248 /**
1249  * _scsih_slave_alloc - device add routine
1250  * @sdev: scsi device struct
1251  *
1252  * Returns 0 if ok. Any other return is assumed to be an error and
1253  * the device is ignored.
1254  */
1255 static int
1256 _scsih_slave_alloc(struct scsi_device *sdev)
1257 {
1258         struct Scsi_Host *shost;
1259         struct MPT2SAS_ADAPTER *ioc;
1260         struct MPT2SAS_TARGET *sas_target_priv_data;
1261         struct MPT2SAS_DEVICE *sas_device_priv_data;
1262         struct scsi_target *starget;
1263         struct _raid_device *raid_device;
1264         struct _sas_device *sas_device;
1265         unsigned long flags;
1266
1267         sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1268         if (!sas_device_priv_data)
1269                 return -ENOMEM;
1270
1271         sas_device_priv_data->lun = sdev->lun;
1272         sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1273
1274         starget = scsi_target(sdev);
1275         sas_target_priv_data = starget->hostdata;
1276         sas_target_priv_data->num_luns++;
1277         sas_device_priv_data->sas_target = sas_target_priv_data;
1278         sdev->hostdata = sas_device_priv_data;
1279         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1280                 sdev->no_uld_attach = 1;
1281
1282         shost = dev_to_shost(&starget->dev);
1283         ioc = shost_priv(shost);
1284         if (starget->channel == RAID_CHANNEL) {
1285                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1286                 raid_device = _scsih_raid_device_find_by_id(ioc,
1287                     starget->id, starget->channel);
1288                 if (raid_device)
1289                         raid_device->sdev = sdev; /* raid is single lun */
1290                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1291         } else {
1292                 /* set TLR bit for SSP devices */
1293                 if (!(ioc->facts.IOCCapabilities &
1294                      MPI2_IOCFACTS_CAPABILITY_TLR))
1295                         goto out;
1296                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1297                 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1298                    sas_device_priv_data->sas_target->sas_address);
1299                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1300                 if (sas_device && sas_device->device_info &
1301                     MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1302                         sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1303         }
1304
1305  out:
1306         return 0;
1307 }
1308
1309 /**
1310  * _scsih_slave_destroy - device destroy routine
1311  * @sdev: scsi device struct
1312  *
1313  * Returns nothing.
1314  */
1315 static void
1316 _scsih_slave_destroy(struct scsi_device *sdev)
1317 {
1318         struct MPT2SAS_TARGET *sas_target_priv_data;
1319         struct scsi_target *starget;
1320
1321         if (!sdev->hostdata)
1322                 return;
1323
1324         starget = scsi_target(sdev);
1325         sas_target_priv_data = starget->hostdata;
1326         sas_target_priv_data->num_luns--;
1327         kfree(sdev->hostdata);
1328         sdev->hostdata = NULL;
1329 }
1330
1331 /**
1332  * _scsih_display_sata_capabilities - sata capabilities
1333  * @ioc: per adapter object
1334  * @sas_device: the sas_device object
1335  * @sdev: scsi device struct
1336  */
1337 static void
1338 _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1339     struct _sas_device *sas_device, struct scsi_device *sdev)
1340 {
1341         Mpi2ConfigReply_t mpi_reply;
1342         Mpi2SasDevicePage0_t sas_device_pg0;
1343         u32 ioc_status;
1344         u16 flags;
1345         u32 device_info;
1346
1347         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1348             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1349                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1350                     ioc->name, __FILE__, __LINE__, __func__);
1351                 return;
1352         }
1353
1354         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1355             MPI2_IOCSTATUS_MASK;
1356         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1357                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1358                     ioc->name, __FILE__, __LINE__, __func__);
1359                 return;
1360         }
1361
1362         flags = le16_to_cpu(sas_device_pg0.Flags);
1363         device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1364
1365         sdev_printk(KERN_INFO, sdev,
1366             "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1367             "sw_preserve(%s)\n",
1368             (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1369             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1370             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1371             "n",
1372             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1373             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1374             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1375 }
1376
1377 /**
1378  * _scsih_get_volume_capabilities - volume capabilities
1379  * @ioc: per adapter object
1380  * @sas_device: the raid_device object
1381  */
1382 static void
1383 _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1384     struct _raid_device *raid_device)
1385 {
1386         Mpi2RaidVolPage0_t *vol_pg0;
1387         Mpi2RaidPhysDiskPage0_t pd_pg0;
1388         Mpi2SasDevicePage0_t sas_device_pg0;
1389         Mpi2ConfigReply_t mpi_reply;
1390         u16 sz;
1391         u8 num_pds;
1392
1393         if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1394             &num_pds)) || !num_pds) {
1395                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1396                     ioc->name, __FILE__, __LINE__, __func__);
1397                 return;
1398         }
1399
1400         raid_device->num_pds = num_pds;
1401         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1402             sizeof(Mpi2RaidVol0PhysDisk_t));
1403         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1404         if (!vol_pg0) {
1405                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1406                     ioc->name, __FILE__, __LINE__, __func__);
1407                 return;
1408         }
1409
1410         if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1411              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1412                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1413                     ioc->name, __FILE__, __LINE__, __func__);
1414                 kfree(vol_pg0);
1415                 return;
1416         }
1417
1418         raid_device->volume_type = vol_pg0->VolumeType;
1419
1420         /* figure out what the underlying devices are by
1421          * obtaining the device_info bits for the 1st device
1422          */
1423         if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1424             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1425             vol_pg0->PhysDisk[0].PhysDiskNum))) {
1426                 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1427                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1428                     le16_to_cpu(pd_pg0.DevHandle)))) {
1429                         raid_device->device_info =
1430                             le32_to_cpu(sas_device_pg0.DeviceInfo);
1431                 }
1432         }
1433
1434         kfree(vol_pg0);
1435 }
1436
1437 /**
1438  * _scsih_slave_configure - device configure routine.
1439  * @sdev: scsi device struct
1440  *
1441  * Returns 0 if ok. Any other return is assumed to be an error and
1442  * the device is ignored.
1443  */
1444 static int
1445 _scsih_slave_configure(struct scsi_device *sdev)
1446 {
1447         struct Scsi_Host *shost = sdev->host;
1448         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1449         struct MPT2SAS_DEVICE *sas_device_priv_data;
1450         struct MPT2SAS_TARGET *sas_target_priv_data;
1451         struct _sas_device *sas_device;
1452         struct _raid_device *raid_device;
1453         unsigned long flags;
1454         int qdepth;
1455         u8 ssp_target = 0;
1456         char *ds = "";
1457         char *r_level = "";
1458
1459         qdepth = 1;
1460         sas_device_priv_data = sdev->hostdata;
1461         sas_device_priv_data->configured_lun = 1;
1462         sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1463         sas_target_priv_data = sas_device_priv_data->sas_target;
1464
1465         /* raid volume handling */
1466         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1467
1468                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1469                 raid_device = _scsih_raid_device_find_by_handle(ioc,
1470                      sas_target_priv_data->handle);
1471                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1472                 if (!raid_device) {
1473                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1474                             ioc->name, __FILE__, __LINE__, __func__);
1475                         return 0;
1476                 }
1477
1478                 _scsih_get_volume_capabilities(ioc, raid_device);
1479
1480                 /* RAID Queue Depth Support
1481                  * IS volume = underlying qdepth of drive type, either
1482                  *    MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1483                  * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1484                  */
1485                 if (raid_device->device_info &
1486                     MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1487                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1488                         ds = "SSP";
1489                 } else {
1490                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1491                          if (raid_device->device_info &
1492                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1493                                 ds = "SATA";
1494                         else
1495                                 ds = "STP";
1496                 }
1497
1498                 switch (raid_device->volume_type) {
1499                 case MPI2_RAID_VOL_TYPE_RAID0:
1500                         r_level = "RAID0";
1501                         break;
1502                 case MPI2_RAID_VOL_TYPE_RAID1E:
1503                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1504                         r_level = "RAID1E";
1505                         break;
1506                 case MPI2_RAID_VOL_TYPE_RAID1:
1507                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1508                         r_level = "RAID1";
1509                         break;
1510                 case MPI2_RAID_VOL_TYPE_RAID10:
1511                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1512                         r_level = "RAID10";
1513                         break;
1514                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1515                 default:
1516                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1517                         r_level = "RAIDX";
1518                         break;
1519                 }
1520
1521                 sdev_printk(KERN_INFO, sdev, "%s: "
1522                     "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1523                     r_level, raid_device->handle,
1524                     (unsigned long long)raid_device->wwid,
1525                     raid_device->num_pds, ds);
1526                 _scsih_change_queue_depth(sdev, qdepth);
1527                 return 0;
1528         }
1529
1530         /* non-raid handling */
1531         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1532         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1533            sas_device_priv_data->sas_target->sas_address);
1534         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1535         if (sas_device) {
1536                 if (sas_target_priv_data->flags &
1537                     MPT_TARGET_FLAGS_RAID_COMPONENT) {
1538                         mpt2sas_config_get_volume_handle(ioc,
1539                             sas_device->handle, &sas_device->volume_handle);
1540                         mpt2sas_config_get_volume_wwid(ioc,
1541                             sas_device->volume_handle,
1542                             &sas_device->volume_wwid);
1543                 }
1544                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1545                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1546                         ssp_target = 1;
1547                         ds = "SSP";
1548                 } else {
1549                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1550                         if (sas_device->device_info &
1551                             MPI2_SAS_DEVICE_INFO_STP_TARGET)
1552                                 ds = "STP";
1553                         else if (sas_device->device_info &
1554                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1555                                 ds = "SATA";
1556                 }
1557
1558                 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1559                     "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1560                     ds, sas_device->handle,
1561                     (unsigned long long)sas_device->sas_address,
1562                     (unsigned long long)sas_device->device_name);
1563                 sdev_printk(KERN_INFO, sdev, "%s: "
1564                     "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1565                     (unsigned long long) sas_device->enclosure_logical_id,
1566                     sas_device->slot);
1567
1568                 if (!ssp_target)
1569                         _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1570         }
1571
1572         _scsih_change_queue_depth(sdev, qdepth);
1573
1574         if (ssp_target)
1575                 sas_read_port_mode_page(sdev);
1576         return 0;
1577 }
1578
1579 /**
1580  * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1581  * @sdev: scsi device struct
1582  * @bdev: pointer to block device context
1583  * @capacity: device size (in 512 byte sectors)
1584  * @params: three element array to place output:
1585  *              params[0] number of heads (max 255)
1586  *              params[1] number of sectors (max 63)
1587  *              params[2] number of cylinders
1588  *
1589  * Return nothing.
1590  */
1591 static int
1592 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1593     sector_t capacity, int params[])
1594 {
1595         int             heads;
1596         int             sectors;
1597         sector_t        cylinders;
1598         ulong           dummy;
1599
1600         heads = 64;
1601         sectors = 32;
1602
1603         dummy = heads * sectors;
1604         cylinders = capacity;
1605         sector_div(cylinders, dummy);
1606
1607         /*
1608          * Handle extended translation size for logical drives
1609          * > 1Gb
1610          */
1611         if ((ulong)capacity >= 0x200000) {
1612                 heads = 255;
1613                 sectors = 63;
1614                 dummy = heads * sectors;
1615                 cylinders = capacity;
1616                 sector_div(cylinders, dummy);
1617         }
1618
1619         /* return result */
1620         params[0] = heads;
1621         params[1] = sectors;
1622         params[2] = cylinders;
1623
1624         return 0;
1625 }
1626
1627 /**
1628  * _scsih_response_code - translation of device response code
1629  * @ioc: per adapter object
1630  * @response_code: response code returned by the device
1631  *
1632  * Return nothing.
1633  */
1634 static void
1635 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1636 {
1637         char *desc;
1638
1639         switch (response_code) {
1640         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1641                 desc = "task management request completed";
1642                 break;
1643         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1644                 desc = "invalid frame";
1645                 break;
1646         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1647                 desc = "task management request not supported";
1648                 break;
1649         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1650                 desc = "task management request failed";
1651                 break;
1652         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1653                 desc = "task management request succeeded";
1654                 break;
1655         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1656                 desc = "invalid lun";
1657                 break;
1658         case 0xA:
1659                 desc = "overlapped tag attempted";
1660                 break;
1661         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1662                 desc = "task queued, however not sent to target";
1663                 break;
1664         default:
1665                 desc = "unknown";
1666                 break;
1667         }
1668         printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1669                 ioc->name, response_code, desc);
1670 }
1671
1672 /**
1673  * _scsih_tm_done - tm completion routine
1674  * @ioc: per adapter object
1675  * @smid: system request message index
1676  * @VF_ID: virtual function id
1677  * @reply: reply message frame(lower 32bit addr)
1678  * Context: none.
1679  *
1680  * The callback handler when using scsih_issue_tm.
1681  *
1682  * Return nothing.
1683  */
1684 static void
1685 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
1686 {
1687         MPI2DefaultReply_t *mpi_reply;
1688
1689         if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1690                 return;
1691         if (ioc->tm_cmds.smid != smid)
1692                 return;
1693         ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1694         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
1695         if (mpi_reply) {
1696                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1697                 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1698         }
1699         ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1700         complete(&ioc->tm_cmds.done);
1701 }
1702
1703 /**
1704  * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1705  * @ioc: per adapter object
1706  * @handle: device handle
1707  *
1708  * During taskmangement request, we need to freeze the device queue.
1709  */
1710 void
1711 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1712 {
1713         struct MPT2SAS_DEVICE *sas_device_priv_data;
1714         struct scsi_device *sdev;
1715         u8 skip = 0;
1716
1717         shost_for_each_device(sdev, ioc->shost) {
1718                 if (skip)
1719                         continue;
1720                 sas_device_priv_data = sdev->hostdata;
1721                 if (!sas_device_priv_data)
1722                         continue;
1723                 if (sas_device_priv_data->sas_target->handle == handle) {
1724                         sas_device_priv_data->sas_target->tm_busy = 1;
1725                         skip = 1;
1726                         ioc->ignore_loginfos = 1;
1727                 }
1728         }
1729 }
1730
1731 /**
1732  * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1733  * @ioc: per adapter object
1734  * @handle: device handle
1735  *
1736  * During taskmangement request, we need to freeze the device queue.
1737  */
1738 void
1739 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1740 {
1741         struct MPT2SAS_DEVICE *sas_device_priv_data;
1742         struct scsi_device *sdev;
1743         u8 skip = 0;
1744
1745         shost_for_each_device(sdev, ioc->shost) {
1746                 if (skip)
1747                         continue;
1748                 sas_device_priv_data = sdev->hostdata;
1749                 if (!sas_device_priv_data)
1750                         continue;
1751                 if (sas_device_priv_data->sas_target->handle == handle) {
1752                         sas_device_priv_data->sas_target->tm_busy = 0;
1753                         skip = 1;
1754                         ioc->ignore_loginfos = 0;
1755                 }
1756         }
1757 }
1758
1759 /**
1760  * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1761  * @ioc: per adapter struct
1762  * @device_handle: device handle
1763  * @lun: lun number
1764  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1765  * @smid_task: smid assigned to the task
1766  * @timeout: timeout in seconds
1767  * Context: The calling function needs to acquire the tm_cmds.mutex
1768  *
1769  * A generic API for sending task management requests to firmware.
1770  *
1771  * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1772  * this API.
1773  *
1774  * The callback index is set inside `ioc->tm_cb_idx`.
1775  *
1776  * Return nothing.
1777  */
1778 void
1779 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1780     u8 type, u16 smid_task, ulong timeout)
1781 {
1782         Mpi2SCSITaskManagementRequest_t *mpi_request;
1783         Mpi2SCSITaskManagementReply_t *mpi_reply;
1784         u16 smid = 0;
1785         u32 ioc_state;
1786         unsigned long timeleft;
1787         u8 VF_ID = 0;
1788         unsigned long flags;
1789
1790         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
1791         if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED ||
1792             ioc->shost_recovery) {
1793                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1794                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1795                     __func__, ioc->name);
1796                 return;
1797         }
1798         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1799
1800         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1801         if (ioc_state & MPI2_DOORBELL_USED) {
1802                 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1803                     "active!\n", ioc->name));
1804                 goto issue_host_reset;
1805         }
1806
1807         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1808                 mpt2sas_base_fault_info(ioc, ioc_state &
1809                     MPI2_DOORBELL_DATA_MASK);
1810                 goto issue_host_reset;
1811         }
1812
1813         smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1814         if (!smid) {
1815                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1816                     ioc->name, __func__);
1817                 return;
1818         }
1819
1820         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1821             " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1822         ioc->tm_cmds.status = MPT2_CMD_PENDING;
1823         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1824         ioc->tm_cmds.smid = smid;
1825         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1826         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1827         mpi_request->DevHandle = cpu_to_le16(handle);
1828         mpi_request->TaskType = type;
1829         mpi_request->TaskMID = cpu_to_le16(smid_task);
1830         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1831         mpt2sas_scsih_set_tm_flag(ioc, handle);
1832         mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1833         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1834         mpt2sas_scsih_clear_tm_flag(ioc, handle);
1835         if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1836                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1837                     ioc->name, __func__);
1838                 _debug_dump_mf(mpi_request,
1839                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1840                 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1841                         goto issue_host_reset;
1842         }
1843
1844         if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1845                 mpi_reply = ioc->tm_cmds.reply;
1846                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1847                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1848                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1849                     le32_to_cpu(mpi_reply->IOCLogInfo),
1850                     le32_to_cpu(mpi_reply->TerminationCount)));
1851                 if (ioc->logging_level & MPT_DEBUG_TM)
1852                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
1853         }
1854         return;
1855  issue_host_reset:
1856         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1857 }
1858
1859 /**
1860  * _scsih_abort - eh threads main abort routine
1861  * @sdev: scsi device struct
1862  *
1863  * Returns SUCCESS if command aborted else FAILED
1864  */
1865 static int
1866 _scsih_abort(struct scsi_cmnd *scmd)
1867 {
1868         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1869         struct MPT2SAS_DEVICE *sas_device_priv_data;
1870         u16 smid;
1871         u16 handle;
1872         int r;
1873         struct scsi_cmnd *scmd_lookup;
1874
1875         printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1876             ioc->name, scmd);
1877         scsi_print_command(scmd);
1878
1879         sas_device_priv_data = scmd->device->hostdata;
1880         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1881                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1882                     ioc->name, scmd);
1883                 scmd->result = DID_NO_CONNECT << 16;
1884                 scmd->scsi_done(scmd);
1885                 r = SUCCESS;
1886                 goto out;
1887         }
1888
1889         /* search for the command */
1890         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1891         if (!smid) {
1892                 scmd->result = DID_RESET << 16;
1893                 r = SUCCESS;
1894                 goto out;
1895         }
1896
1897         /* for hidden raid components and volumes this is not supported */
1898         if (sas_device_priv_data->sas_target->flags &
1899             MPT_TARGET_FLAGS_RAID_COMPONENT ||
1900             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1901                 scmd->result = DID_RESET << 16;
1902                 r = FAILED;
1903                 goto out;
1904         }
1905
1906         mutex_lock(&ioc->tm_cmds.mutex);
1907         handle = sas_device_priv_data->sas_target->handle;
1908         mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1909             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1910
1911         /* sanity check - see whether command actually completed */
1912         scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1913         if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1914                 r = FAILED;
1915         else
1916                 r = SUCCESS;
1917         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1918         mutex_unlock(&ioc->tm_cmds.mutex);
1919
1920  out:
1921         printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1922             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1923         return r;
1924 }
1925
1926 /**
1927  * _scsih_dev_reset - eh threads main device reset routine
1928  * @sdev: scsi device struct
1929  *
1930  * Returns SUCCESS if command aborted else FAILED
1931  */
1932 static int
1933 _scsih_dev_reset(struct scsi_cmnd *scmd)
1934 {
1935         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1936         struct MPT2SAS_DEVICE *sas_device_priv_data;
1937         struct _sas_device *sas_device;
1938         unsigned long flags;
1939         u16     handle;
1940         int r;
1941
1942         printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
1943             ioc->name, scmd);
1944         scsi_print_command(scmd);
1945
1946         sas_device_priv_data = scmd->device->hostdata;
1947         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1948                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1949                     ioc->name, scmd);
1950                 scmd->result = DID_NO_CONNECT << 16;
1951                 scmd->scsi_done(scmd);
1952                 r = SUCCESS;
1953                 goto out;
1954         }
1955
1956         /* for hidden raid components obtain the volume_handle */
1957         handle = 0;
1958         if (sas_device_priv_data->sas_target->flags &
1959             MPT_TARGET_FLAGS_RAID_COMPONENT) {
1960                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1961                 sas_device = _scsih_sas_device_find_by_handle(ioc,
1962                    sas_device_priv_data->sas_target->handle);
1963                 if (sas_device)
1964                         handle = sas_device->volume_handle;
1965                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1966         } else
1967                 handle = sas_device_priv_data->sas_target->handle;
1968
1969         if (!handle) {
1970                 scmd->result = DID_RESET << 16;
1971                 r = FAILED;
1972                 goto out;
1973         }
1974
1975         mutex_lock(&ioc->tm_cmds.mutex);
1976         mpt2sas_scsih_issue_tm(ioc, handle, 0,
1977             MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
1978             30);
1979
1980         /*
1981          *  sanity check see whether all commands to this device been
1982          *  completed
1983          */
1984         if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
1985             scmd->device->lun, scmd->device->channel))
1986                 r = FAILED;
1987         else
1988                 r = SUCCESS;
1989         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1990         mutex_unlock(&ioc->tm_cmds.mutex);
1991
1992  out:
1993         printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
1994             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1995         return r;
1996 }
1997
1998 /**
1999  * _scsih_target_reset - eh threads main target reset routine
2000  * @sdev: scsi device struct
2001  *
2002  * Returns SUCCESS if command aborted else FAILED
2003  */
2004 static int
2005 _scsih_target_reset(struct scsi_cmnd *scmd)
2006 {
2007         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2008         struct MPT2SAS_DEVICE *sas_device_priv_data;
2009         struct _sas_device *sas_device;
2010         unsigned long flags;
2011         u16     handle;
2012         int r;
2013
2014         printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2015             ioc->name, scmd);
2016         scsi_print_command(scmd);
2017
2018         sas_device_priv_data = scmd->device->hostdata;
2019         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2020                 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2021                     ioc->name, scmd);
2022                 scmd->result = DID_NO_CONNECT << 16;
2023                 scmd->scsi_done(scmd);
2024                 r = SUCCESS;
2025                 goto out;
2026         }
2027
2028         /* for hidden raid components obtain the volume_handle */
2029         handle = 0;
2030         if (sas_device_priv_data->sas_target->flags &
2031             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2032                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2033                 sas_device = _scsih_sas_device_find_by_handle(ioc,
2034                    sas_device_priv_data->sas_target->handle);
2035                 if (sas_device)
2036                         handle = sas_device->volume_handle;
2037                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2038         } else
2039                 handle = sas_device_priv_data->sas_target->handle;
2040
2041         if (!handle) {
2042                 scmd->result = DID_RESET << 16;
2043                 r = FAILED;
2044                 goto out;
2045         }
2046
2047         mutex_lock(&ioc->tm_cmds.mutex);
2048         mpt2sas_scsih_issue_tm(ioc, handle, 0,
2049             MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2050
2051         /*
2052          *  sanity check see whether all commands to this target been
2053          *  completed
2054          */
2055         if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2056             scmd->device->channel))
2057                 r = FAILED;
2058         else
2059                 r = SUCCESS;
2060         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2061         mutex_unlock(&ioc->tm_cmds.mutex);
2062
2063  out:
2064         printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2065             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2066         return r;
2067 }
2068
2069 /**
2070  * _scsih_abort - eh threads main host reset routine
2071  * @sdev: scsi device struct
2072  *
2073  * Returns SUCCESS if command aborted else FAILED
2074  */
2075 static int
2076 _scsih_host_reset(struct scsi_cmnd *scmd)
2077 {
2078         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2079         int r, retval;
2080
2081         printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2082             ioc->name, scmd);
2083         scsi_print_command(scmd);
2084
2085         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2086             FORCE_BIG_HAMMER);
2087         r = (retval < 0) ? FAILED : SUCCESS;
2088         printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2089             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2090
2091         return r;
2092 }
2093
2094 /**
2095  * _scsih_fw_event_add - insert and queue up fw_event
2096  * @ioc: per adapter object
2097  * @fw_event: object describing the event
2098  * Context: This function will acquire ioc->fw_event_lock.
2099  *
2100  * This adds the firmware event object into link list, then queues it up to
2101  * be processed from user context.
2102  *
2103  * Return nothing.
2104  */
2105 static void
2106 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2107 {
2108         unsigned long flags;
2109
2110         if (ioc->firmware_event_thread == NULL)
2111                 return;
2112
2113         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2114         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2115         INIT_WORK(&fw_event->work, _firmware_event_work);
2116         queue_work(ioc->firmware_event_thread, &fw_event->work);
2117         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2118 }
2119
2120 /**
2121  * _scsih_fw_event_free - delete fw_event
2122  * @ioc: per adapter object
2123  * @fw_event: object describing the event
2124  * Context: This function will acquire ioc->fw_event_lock.
2125  *
2126  * This removes firmware event object from link list, frees associated memory.
2127  *
2128  * Return nothing.
2129  */
2130 static void
2131 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2132     *fw_event)
2133 {
2134         unsigned long flags;
2135
2136         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2137         list_del(&fw_event->list);
2138         kfree(fw_event->event_data);
2139         kfree(fw_event);
2140         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2141 }
2142
2143 /**
2144  * _scsih_fw_event_add - requeue an event
2145  * @ioc: per adapter object
2146  * @fw_event: object describing the event
2147  * Context: This function will acquire ioc->fw_event_lock.
2148  *
2149  * Return nothing.
2150  */
2151 static void
2152 _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2153     *fw_event, unsigned long delay)
2154 {
2155         unsigned long flags;
2156         if (ioc->firmware_event_thread == NULL)
2157                 return;
2158
2159         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2160         queue_work(ioc->firmware_event_thread, &fw_event->work);
2161         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2162 }
2163
2164 /**
2165  * _scsih_fw_event_off - turn flag off preventing event handling
2166  * @ioc: per adapter object
2167  *
2168  * Used to prevent handling of firmware events during adapter reset
2169  * driver unload.
2170  *
2171  * Return nothing.
2172  */
2173 static void
2174 _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2175 {
2176         unsigned long flags;
2177
2178         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2179         ioc->fw_events_off = 1;
2180         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2181
2182 }
2183
2184 /**
2185  * _scsih_fw_event_on - turn flag on allowing firmware event handling
2186  * @ioc: per adapter object
2187  *
2188  * Returns nothing.
2189  */
2190 static void
2191 _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2192 {
2193         unsigned long flags;
2194
2195         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2196         ioc->fw_events_off = 0;
2197         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2198 }
2199
2200 /**
2201  * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2202  * @ioc: per adapter object
2203  * @handle: device handle
2204  *
2205  * During device pull we need to appropiately set the sdev state.
2206  */
2207 static void
2208 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2209 {
2210         struct MPT2SAS_DEVICE *sas_device_priv_data;
2211         struct scsi_device *sdev;
2212
2213         shost_for_each_device(sdev, ioc->shost) {
2214                 sas_device_priv_data = sdev->hostdata;
2215                 if (!sas_device_priv_data)
2216                         continue;
2217                 if (!sas_device_priv_data->block)
2218                         continue;
2219                 if (sas_device_priv_data->sas_target->handle == handle) {
2220                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2221                             MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2222                             "handle(0x%04x)\n", ioc->name, handle));
2223                         sas_device_priv_data->block = 0;
2224                         scsi_device_set_state(sdev, SDEV_RUNNING);
2225                 }
2226         }
2227 }
2228
2229 /**
2230  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2231  * @ioc: per adapter object
2232  * @handle: device handle
2233  *
2234  * During device pull we need to appropiately set the sdev state.
2235  */
2236 static void
2237 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2238 {
2239         struct MPT2SAS_DEVICE *sas_device_priv_data;
2240         struct scsi_device *sdev;
2241
2242         shost_for_each_device(sdev, ioc->shost) {
2243                 sas_device_priv_data = sdev->hostdata;
2244                 if (!sas_device_priv_data)
2245                         continue;
2246                 if (sas_device_priv_data->block)
2247                         continue;
2248                 if (sas_device_priv_data->sas_target->handle == handle) {
2249                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2250                             MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2251                             "handle(0x%04x)\n", ioc->name, handle));
2252                         sas_device_priv_data->block = 1;
2253                         scsi_device_set_state(sdev, SDEV_BLOCK);
2254                 }
2255         }
2256 }
2257
2258 /**
2259  * _scsih_block_io_to_children_attached_to_ex
2260  * @ioc: per adapter object
2261  * @sas_expander: the sas_device object
2262  *
2263  * This routine set sdev state to SDEV_BLOCK for all devices
2264  * attached to this expander. This function called when expander is
2265  * pulled.
2266  */
2267 static void
2268 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2269     struct _sas_node *sas_expander)
2270 {
2271         struct _sas_port *mpt2sas_port;
2272         struct _sas_device *sas_device;
2273         struct _sas_node *expander_sibling;
2274         unsigned long flags;
2275
2276         if (!sas_expander)
2277                 return;
2278
2279         list_for_each_entry(mpt2sas_port,
2280            &sas_expander->sas_port_list, port_list) {
2281                 if (mpt2sas_port->remote_identify.device_type ==
2282                     SAS_END_DEVICE) {
2283                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2284                         sas_device =
2285                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2286                            mpt2sas_port->remote_identify.sas_address);
2287                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2288                         if (!sas_device)
2289                                 continue;
2290                         _scsih_block_io_device(ioc, sas_device->handle);
2291                 }
2292         }
2293
2294         list_for_each_entry(mpt2sas_port,
2295            &sas_expander->sas_port_list, port_list) {
2296
2297                 if (mpt2sas_port->remote_identify.device_type ==
2298                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2299                     mpt2sas_port->remote_identify.device_type ==
2300                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2301
2302                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
2303                         expander_sibling =
2304                             mpt2sas_scsih_expander_find_by_sas_address(
2305                             ioc, mpt2sas_port->remote_identify.sas_address);
2306                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2307                         _scsih_block_io_to_children_attached_to_ex(ioc,
2308                             expander_sibling);
2309                 }
2310         }
2311 }
2312
2313 /**
2314  * _scsih_block_io_to_children_attached_directly
2315  * @ioc: per adapter object
2316  * @event_data: topology change event data
2317  *
2318  * This routine set sdev state to SDEV_BLOCK for all devices
2319  * direct attached during device pull.
2320  */
2321 static void
2322 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2323     Mpi2EventDataSasTopologyChangeList_t *event_data)
2324 {
2325         int i;
2326         u16 handle;
2327         u16 reason_code;
2328         u8 phy_number;
2329
2330         for (i = 0; i < event_data->NumEntries; i++) {
2331                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2332                 if (!handle)
2333                         continue;
2334                 phy_number = event_data->StartPhyNum + i;
2335                 reason_code = event_data->PHY[i].PhyStatus &
2336                     MPI2_EVENT_SAS_TOPO_RC_MASK;
2337                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2338                         _scsih_block_io_device(ioc, handle);
2339         }
2340 }
2341
2342 /**
2343  * _scsih_check_topo_delete_events - sanity check on topo events
2344  * @ioc: per adapter object
2345  * @event_data: the event data payload
2346  *
2347  * This routine added to better handle cable breaker.
2348  *
2349  * This handles the case where driver recieves multiple expander
2350  * add and delete events in a single shot.  When there is a delete event
2351  * the routine will void any pending add events waiting in the event queue.
2352  *
2353  * Return nothing.
2354  */
2355 static void
2356 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2357     Mpi2EventDataSasTopologyChangeList_t *event_data)
2358 {
2359         struct fw_event_work *fw_event;
2360         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2361         u16 expander_handle;
2362         struct _sas_node *sas_expander;
2363         unsigned long flags;
2364
2365         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2366         if (expander_handle < ioc->sas_hba.num_phys) {
2367                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2368                 return;
2369         }
2370
2371         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2372          || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2373                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2374                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2375                     expander_handle);
2376                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2377                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2378         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2379                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2380
2381         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2382                 return;
2383
2384         /* mark ignore flag for pending events */
2385         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2386         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2387                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2388                     fw_event->ignore)
2389                         continue;
2390                 local_event_data = fw_event->event_data;
2391                 if (local_event_data->ExpStatus ==
2392                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2393                     local_event_data->ExpStatus ==
2394                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2395                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2396                             expander_handle) {
2397                                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2398                                     "setting ignoring flag\n", ioc->name));
2399                                 fw_event->ignore = 1;
2400                         }
2401                 }
2402         }
2403         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2404 }
2405
2406 /**
2407  * _scsih_flush_running_cmds - completing outstanding commands.
2408  * @ioc: per adapter object
2409  *
2410  * The flushing out of all pending scmd commands following host reset,
2411  * where all IO is dropped to the floor.
2412  *
2413  * Return nothing.
2414  */
2415 static void
2416 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2417 {
2418         struct scsi_cmnd *scmd;
2419         u16 smid;
2420         u16 count = 0;
2421
2422         for (smid = 1; smid <= ioc->request_depth; smid++) {
2423                 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2424                 if (!scmd)
2425                         continue;
2426                 count++;
2427                 mpt2sas_base_free_smid(ioc, smid);
2428                 scsi_dma_unmap(scmd);
2429                 scmd->result = DID_RESET << 16;
2430                 scmd->scsi_done(scmd);
2431         }
2432         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2433             ioc->name, count));
2434 }
2435
2436 /**
2437  * _scsih_setup_eedp - setup MPI request for EEDP transfer
2438  * @scmd: pointer to scsi command object
2439  * @mpi_request: pointer to the SCSI_IO reqest message frame
2440  *
2441  * Supporting protection 1 and 3.
2442  *
2443  * Returns nothing
2444  */
2445 static void
2446 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2447 {
2448         u16 eedp_flags;
2449         unsigned char prot_op = scsi_get_prot_op(scmd);
2450         unsigned char prot_type = scsi_get_prot_type(scmd);
2451
2452         if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2453            prot_type == SCSI_PROT_DIF_TYPE2 ||
2454            prot_op == SCSI_PROT_NORMAL)
2455                 return;
2456
2457         if (prot_op ==  SCSI_PROT_READ_STRIP)
2458                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2459         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
2460                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2461         else
2462                 return;
2463
2464         mpi_request->EEDPBlockSize = scmd->device->sector_size;
2465
2466         switch (prot_type) {
2467         case SCSI_PROT_DIF_TYPE1:
2468
2469                 /*
2470                 * enable ref/guard checking
2471                 * auto increment ref tag
2472                 */
2473                 mpi_request->EEDPFlags = eedp_flags |
2474                     MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2475                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2476                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2477                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2478                     cpu_to_be32(scsi_get_lba(scmd));
2479
2480                 break;
2481
2482         case SCSI_PROT_DIF_TYPE3:
2483
2484                 /*
2485                 * enable guard checking
2486                 */
2487                 mpi_request->EEDPFlags = eedp_flags |
2488                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2489
2490                 break;
2491         }
2492 }
2493
2494 /**
2495  * _scsih_eedp_error_handling - return sense code for EEDP errors
2496  * @scmd: pointer to scsi command object
2497  * @ioc_status: ioc status
2498  *
2499  * Returns nothing
2500  */
2501 static void
2502 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2503 {
2504         u8 ascq;
2505         u8 sk;
2506         u8 host_byte;
2507
2508         switch (ioc_status) {
2509         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2510                 ascq = 0x01;
2511                 break;
2512         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2513                 ascq = 0x02;
2514                 break;
2515         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2516                 ascq = 0x03;
2517                 break;
2518         default:
2519                 ascq = 0x00;
2520                 break;
2521         }
2522
2523         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2524                 sk = ILLEGAL_REQUEST;
2525                 host_byte = DID_ABORT;
2526         } else {
2527                 sk = ABORTED_COMMAND;
2528                 host_byte = DID_OK;
2529         }
2530
2531         scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2532         scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2533             SAM_STAT_CHECK_CONDITION;
2534 }
2535
2536 /**
2537  * _scsih_qcmd - main scsi request entry point
2538  * @scmd: pointer to scsi command object
2539  * @done: function pointer to be invoked on completion
2540  *
2541  * The callback index is set inside `ioc->scsi_io_cb_idx`.
2542  *
2543  * Returns 0 on success.  If there's a failure, return either:
2544  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2545  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2546  */
2547 static int
2548 _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
2549 {
2550         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2551         struct MPT2SAS_DEVICE *sas_device_priv_data;
2552         struct MPT2SAS_TARGET *sas_target_priv_data;
2553         Mpi2SCSIIORequest_t *mpi_request;
2554         u32 mpi_control;
2555         u16 smid;
2556         unsigned long flags;
2557
2558         scmd->scsi_done = done;
2559         sas_device_priv_data = scmd->device->hostdata;
2560         if (!sas_device_priv_data) {
2561                 scmd->result = DID_NO_CONNECT << 16;
2562                 scmd->scsi_done(scmd);
2563                 return 0;
2564         }
2565
2566         sas_target_priv_data = sas_device_priv_data->sas_target;
2567         if (!sas_target_priv_data || sas_target_priv_data->handle ==
2568             MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2569                 scmd->result = DID_NO_CONNECT << 16;
2570                 scmd->scsi_done(scmd);
2571                 return 0;
2572         }
2573
2574         /* see if we are busy with task managment stuff */
2575         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
2576         if (sas_target_priv_data->tm_busy ||
2577             ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
2578                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2579                 return SCSI_MLQUEUE_HOST_BUSY;
2580         }
2581         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2582
2583         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2584                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2585         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2586                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2587         else
2588                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2589
2590         /* set tags */
2591         if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2592                 if (scmd->device->tagged_supported) {
2593                         if (scmd->device->ordered_tags)
2594                                 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2595                         else
2596                                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2597                 } else
2598 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2599 /*                      mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2600  */
2601                         mpi_control |= (0x500);
2602
2603         } else
2604                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2605
2606         if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2607                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2608
2609         smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2610         if (!smid) {
2611                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2612                     ioc->name, __func__);
2613                 goto out;
2614         }
2615         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2616         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
2617         _scsih_setup_eedp(scmd, mpi_request);
2618         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2619         if (sas_device_priv_data->sas_target->flags &
2620             MPT_TARGET_FLAGS_RAID_COMPONENT)
2621                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2622         else
2623                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2624         mpi_request->DevHandle =
2625             cpu_to_le16(sas_device_priv_data->sas_target->handle);
2626         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2627         mpi_request->Control = cpu_to_le32(mpi_control);
2628         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2629         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2630         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2631         mpi_request->SenseBufferLowAddress =
2632             (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2633         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2634         mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2635             MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2636
2637         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2638             mpi_request->LUN);
2639         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2640
2641         if (!mpi_request->DataLength) {
2642                 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2643         } else {
2644                 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2645                         mpt2sas_base_free_smid(ioc, smid);
2646                         goto out;
2647                 }
2648         }
2649
2650         _scsih_scsi_lookup_set(ioc, smid, scmd);
2651         mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2652             sas_device_priv_data->sas_target->handle);
2653         return 0;
2654
2655  out:
2656         return SCSI_MLQUEUE_HOST_BUSY;
2657 }
2658
2659 /**
2660  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2661  * @sense_buffer: sense data returned by target
2662  * @data: normalized skey/asc/ascq
2663  *
2664  * Return nothing.
2665  */
2666 static void
2667 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2668 {
2669         if ((sense_buffer[0] & 0x7F) >= 0x72) {
2670                 /* descriptor format */
2671                 data->skey = sense_buffer[1] & 0x0F;
2672                 data->asc = sense_buffer[2];
2673                 data->ascq = sense_buffer[3];
2674         } else {
2675                 /* fixed format */
2676                 data->skey = sense_buffer[2] & 0x0F;
2677                 data->asc = sense_buffer[12];
2678                 data->ascq = sense_buffer[13];
2679         }
2680 }
2681
2682 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2683 /**
2684  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2685  * @ioc: per adapter object
2686  * @scmd: pointer to scsi command object
2687  * @mpi_reply: reply mf payload returned from firmware
2688  *
2689  * scsi_status - SCSI Status code returned from target device
2690  * scsi_state - state info associated with SCSI_IO determined by ioc
2691  * ioc_status - ioc supplied status info
2692  *
2693  * Return nothing.
2694  */
2695 static void
2696 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2697     Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2698 {
2699         u32 response_info;
2700         u8 *response_bytes;
2701         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2702             MPI2_IOCSTATUS_MASK;
2703         u8 scsi_state = mpi_reply->SCSIState;
2704         u8 scsi_status = mpi_reply->SCSIStatus;
2705         char *desc_ioc_state = NULL;
2706         char *desc_scsi_status = NULL;
2707         char *desc_scsi_state = ioc->tmp_string;
2708         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2709
2710         if (log_info == 0x31170000)
2711                 return;
2712
2713         switch (ioc_status) {
2714         case MPI2_IOCSTATUS_SUCCESS:
2715                 desc_ioc_state = "success";
2716                 break;
2717         case MPI2_IOCSTATUS_INVALID_FUNCTION:
2718                 desc_ioc_state = "invalid function";
2719                 break;
2720         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2721                 desc_ioc_state = "scsi recovered error";
2722                 break;
2723         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2724                 desc_ioc_state = "scsi invalid dev handle";
2725                 break;
2726         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2727                 desc_ioc_state = "scsi device not there";
2728                 break;
2729         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2730                 desc_ioc_state = "scsi data overrun";
2731                 break;
2732         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2733                 desc_ioc_state = "scsi data underrun";
2734                 break;
2735         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2736                 desc_ioc_state = "scsi io data error";
2737                 break;
2738         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2739                 desc_ioc_state = "scsi protocol error";
2740                 break;
2741         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2742                 desc_ioc_state = "scsi task terminated";
2743                 break;
2744         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2745                 desc_ioc_state = "scsi residual mismatch";
2746                 break;
2747         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2748                 desc_ioc_state = "scsi task mgmt failed";
2749                 break;
2750         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2751                 desc_ioc_state = "scsi ioc terminated";
2752                 break;
2753         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2754                 desc_ioc_state = "scsi ext terminated";
2755                 break;
2756         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2757                 desc_ioc_state = "eedp guard error";
2758                 break;
2759         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2760                 desc_ioc_state = "eedp ref tag error";
2761                 break;
2762         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2763                 desc_ioc_state = "eedp app tag error";
2764                 break;
2765         default:
2766                 desc_ioc_state = "unknown";
2767                 break;
2768         }
2769
2770         switch (scsi_status) {
2771         case MPI2_SCSI_STATUS_GOOD:
2772                 desc_scsi_status = "good";
2773                 break;
2774         case MPI2_SCSI_STATUS_CHECK_CONDITION:
2775                 desc_scsi_status = "check condition";
2776                 break;
2777         case MPI2_SCSI_STATUS_CONDITION_MET:
2778                 desc_scsi_status = "condition met";
2779                 break;
2780         case MPI2_SCSI_STATUS_BUSY:
2781                 desc_scsi_status = "busy";
2782                 break;
2783         case MPI2_SCSI_STATUS_INTERMEDIATE:
2784                 desc_scsi_status = "intermediate";
2785                 break;
2786         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2787                 desc_scsi_status = "intermediate condmet";
2788                 break;
2789         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2790                 desc_scsi_status = "reservation conflict";
2791                 break;
2792         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2793                 desc_scsi_status = "command terminated";
2794                 break;
2795         case MPI2_SCSI_STATUS_TASK_SET_FULL:
2796                 desc_scsi_status = "task set full";
2797                 break;
2798         case MPI2_SCSI_STATUS_ACA_ACTIVE:
2799                 desc_scsi_status = "aca active";
2800                 break;
2801         case MPI2_SCSI_STATUS_TASK_ABORTED:
2802                 desc_scsi_status = "task aborted";
2803                 break;
2804         default:
2805                 desc_scsi_status = "unknown";
2806                 break;
2807         }
2808
2809         desc_scsi_state[0] = '\0';
2810         if (!scsi_state)
2811                 desc_scsi_state = " ";
2812         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2813                 strcat(desc_scsi_state, "response info ");
2814         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2815                 strcat(desc_scsi_state, "state terminated ");
2816         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2817                 strcat(desc_scsi_state, "no status ");
2818         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2819                 strcat(desc_scsi_state, "autosense failed ");
2820         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2821                 strcat(desc_scsi_state, "autosense valid ");
2822
2823         scsi_print_command(scmd);
2824         printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2825             "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2826             le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2827                 ioc_status, smid);
2828         printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2829             "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2830             scsi_get_resid(scmd));
2831         printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2832             "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2833             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2834         printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2835             "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2836             scsi_status, desc_scsi_state, scsi_state);
2837
2838         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2839                 struct sense_info data;
2840                 _scsih_normalize_sense(scmd->sense_buffer, &data);
2841                 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2842                     "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2843                     data.asc, data.ascq);
2844         }
2845
2846         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2847                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2848                 response_bytes = (u8 *)&response_info;
2849                 _scsih_response_code(ioc, response_bytes[3]);
2850         }
2851 }
2852 #endif
2853
2854 /**
2855  * _scsih_smart_predicted_fault - illuminate Fault LED
2856  * @ioc: per adapter object
2857  * @handle: device handle
2858  *
2859  * Return nothing.
2860  */
2861 static void
2862 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2863 {
2864         Mpi2SepReply_t mpi_reply;
2865         Mpi2SepRequest_t mpi_request;
2866         struct scsi_target *starget;
2867         struct MPT2SAS_TARGET *sas_target_priv_data;
2868         Mpi2EventNotificationReply_t *event_reply;
2869         Mpi2EventDataSasDeviceStatusChange_t *event_data;
2870         struct _sas_device *sas_device;
2871         ssize_t sz;
2872         unsigned long flags;
2873
2874         /* only handle non-raid devices */
2875         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2876         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2877         if (!sas_device) {
2878                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2879                 return;
2880         }
2881         starget = sas_device->starget;
2882         sas_target_priv_data = starget->hostdata;
2883
2884         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2885            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2886                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2887                 return;
2888         }
2889         starget_printk(KERN_WARNING, starget, "predicted fault\n");
2890         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2891
2892         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2893                 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2894                 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2895                 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2896                 mpi_request.SlotStatus =
2897                     MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2898                 mpi_request.DevHandle = cpu_to_le16(handle);
2899                 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2900                 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2901                     &mpi_request)) != 0) {
2902                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2903                             ioc->name, __FILE__, __LINE__, __func__);
2904                         return;
2905                 }
2906
2907                 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2908                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2909                             "enclosure_processor: ioc_status (0x%04x), "
2910                             "loginfo(0x%08x)\n", ioc->name,
2911                             le16_to_cpu(mpi_reply.IOCStatus),
2912                             le32_to_cpu(mpi_reply.IOCLogInfo)));
2913                         return;
2914                 }
2915         }
2916
2917         /* insert into event log */
2918         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2919              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2920         event_reply = kzalloc(sz, GFP_KERNEL);
2921         if (!event_reply) {
2922                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2923                     ioc->name, __FILE__, __LINE__, __func__);
2924                 return;
2925         }
2926
2927         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2928         event_reply->Event =
2929             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2930         event_reply->MsgLength = sz/4;
2931         event_reply->EventDataLength =
2932             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2933         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2934             event_reply->EventData;
2935         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2936         event_data->ASC = 0x5D;
2937         event_data->DevHandle = cpu_to_le16(handle);
2938         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2939         mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2940         kfree(event_reply);
2941 }
2942
2943 /**
2944  * _scsih_io_done - scsi request callback
2945  * @ioc: per adapter object
2946  * @smid: system request message index
2947  * @VF_ID: virtual function id
2948  * @reply: reply message frame(lower 32bit addr)
2949  *
2950  * Callback handler when using scsih_qcmd.
2951  *
2952  * Return nothing.
2953  */
2954 static void
2955 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
2956 {
2957         Mpi2SCSIIORequest_t *mpi_request;
2958         Mpi2SCSIIOReply_t *mpi_reply;
2959         struct scsi_cmnd *scmd;
2960         u16 ioc_status;
2961         u32 xfer_cnt;
2962         u8 scsi_state;
2963         u8 scsi_status;
2964         u32 log_info;
2965         struct MPT2SAS_DEVICE *sas_device_priv_data;
2966         u32 response_code;
2967
2968         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2969         scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2970         if (scmd == NULL)
2971                 return;
2972
2973         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2974
2975         if (mpi_reply == NULL) {
2976                 scmd->result = DID_OK << 16;
2977                 goto out;
2978         }
2979
2980         sas_device_priv_data = scmd->device->hostdata;
2981         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2982              sas_device_priv_data->sas_target->deleted) {
2983                 scmd->result = DID_NO_CONNECT << 16;
2984                 goto out;
2985         }
2986
2987         /* turning off TLR */
2988         if (!sas_device_priv_data->tlr_snoop_check) {
2989                 sas_device_priv_data->tlr_snoop_check++;
2990                 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
2991                         response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
2992                             >> 24);
2993                         if (response_code ==
2994                             MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
2995                                 sas_device_priv_data->flags &=
2996                                     ~MPT_DEVICE_TLR_ON;
2997                 }
2998         }
2999
3000         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3001         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3002         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3003         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3004                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
3005         else
3006                 log_info = 0;
3007         ioc_status &= MPI2_IOCSTATUS_MASK;
3008         scsi_state = mpi_reply->SCSIState;
3009         scsi_status = mpi_reply->SCSIStatus;
3010
3011         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3012             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3013              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3014              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3015                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3016         }
3017
3018         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3019                 struct sense_info data;
3020                 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3021                     smid);
3022                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3023                     le32_to_cpu(mpi_reply->SenseCount));
3024                 memcpy(scmd->sense_buffer, sense_data, sz);
3025                 _scsih_normalize_sense(scmd->sense_buffer, &data);
3026                 /* failure prediction threshold exceeded */
3027                 if (data.asc == 0x5D)
3028                         _scsih_smart_predicted_fault(ioc,
3029                             le16_to_cpu(mpi_reply->DevHandle));
3030         }
3031
3032         switch (ioc_status) {
3033         case MPI2_IOCSTATUS_BUSY:
3034         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3035                 scmd->result = SAM_STAT_BUSY;
3036                 break;
3037
3038         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3039                 scmd->result = DID_NO_CONNECT << 16;
3040                 break;
3041
3042         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3043                 if (sas_device_priv_data->block) {
3044                         scmd->result = (DID_BUS_BUSY << 16);
3045                         break;
3046                 }
3047
3048         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3049         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3050                 scmd->result = DID_RESET << 16;
3051                 break;
3052
3053         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3054                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3055                         scmd->result = DID_SOFT_ERROR << 16;
3056                 else
3057                         scmd->result = (DID_OK << 16) | scsi_status;
3058                 break;
3059
3060         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3061                 scmd->result = (DID_OK << 16) | scsi_status;
3062
3063                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3064                         break;
3065
3066                 if (xfer_cnt < scmd->underflow) {
3067                         if (scsi_status == SAM_STAT_BUSY)
3068                                 scmd->result = SAM_STAT_BUSY;
3069                         else
3070                                 scmd->result = DID_SOFT_ERROR << 16;
3071                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3072                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3073                         scmd->result = DID_SOFT_ERROR << 16;
3074                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3075                         scmd->result = DID_RESET << 16;
3076                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3077                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3078                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3079                         scmd->result = (DRIVER_SENSE << 24) |
3080                             SAM_STAT_CHECK_CONDITION;
3081                         scmd->sense_buffer[0] = 0x70;
3082                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3083                         scmd->sense_buffer[12] = 0x20;
3084                         scmd->sense_buffer[13] = 0;
3085                 }
3086                 break;
3087
3088         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3089                 scsi_set_resid(scmd, 0);
3090         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3091         case MPI2_IOCSTATUS_SUCCESS:
3092                 scmd->result = (DID_OK << 16) | scsi_status;
3093                 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3094                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3095                         scmd->result = DID_SOFT_ERROR << 16;
3096                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3097                         scmd->result = DID_RESET << 16;
3098                 break;
3099
3100         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3101         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3102         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3103                 _scsih_eedp_error_handling(scmd, ioc_status);
3104                 break;
3105         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3106         case MPI2_IOCSTATUS_INVALID_FUNCTION:
3107         case MPI2_IOCSTATUS_INVALID_SGL:
3108         case MPI2_IOCSTATUS_INTERNAL_ERROR:
3109         case MPI2_IOCSTATUS_INVALID_FIELD:
3110         case MPI2_IOCSTATUS_INVALID_STATE:
3111         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3112         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3113         default:
3114                 scmd->result = DID_SOFT_ERROR << 16;
3115                 break;
3116
3117         }
3118
3119 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3120         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3121                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3122 #endif
3123
3124  out:
3125         scsi_dma_unmap(scmd);
3126         scmd->scsi_done(scmd);
3127 }
3128
3129 /**
3130  * _scsih_link_change - process phy link changes
3131  * @ioc: per adapter object
3132  * @handle: phy handle
3133  * @attached_handle: valid for devices attached to link
3134  * @phy_number: phy number
3135  * @link_rate: new link rate
3136  * Context: user.
3137  *
3138  * Return nothing.
3139  */
3140 static void
3141 _scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
3142    u8 phy_number, u8 link_rate)
3143 {
3144         mpt2sas_transport_update_phy_link_change(ioc, handle, attached_handle,
3145             phy_number, link_rate);
3146 }
3147
3148 /**
3149  * _scsih_sas_host_refresh - refreshing sas host object contents
3150  * @ioc: per adapter object
3151  * @update: update link information
3152  * Context: user
3153  *
3154  * During port enable, fw will send topology events for every device. Its
3155  * possible that the handles may change from the previous setting, so this
3156  * code keeping handles updating if changed.
3157  *
3158  * Return nothing.
3159  */
3160 static void
3161 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3162 {
3163         u16 sz;
3164         u16 ioc_status;
3165         int i;
3166         Mpi2ConfigReply_t mpi_reply;
3167         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3168
3169         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3170             "updating handles for sas_host(0x%016llx)\n",
3171             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3172
3173         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3174             * sizeof(Mpi2SasIOUnit0PhyData_t));
3175         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3176         if (!sas_iounit_pg0) {
3177                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3178                     ioc->name, __FILE__, __LINE__, __func__);
3179                 return;
3180         }
3181         if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3182             sas_iounit_pg0, sz))) {
3183                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3184                     MPI2_IOCSTATUS_MASK;
3185                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3186                         goto out;
3187                 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3188                         ioc->sas_hba.phy[i].handle =
3189                             le16_to_cpu(sas_iounit_pg0->PhyData[i].
3190                                 ControllerDevHandle);
3191                         if (update)
3192                                 _scsih_link_change(ioc,
3193                                     ioc->sas_hba.phy[i].handle,
3194                                     le16_to_cpu(sas_iounit_pg0->PhyData[i].
3195                                     AttachedDevHandle), i,
3196                                     sas_iounit_pg0->PhyData[i].
3197                                     NegotiatedLinkRate >> 4);
3198                 }
3199         }
3200
3201  out:
3202         kfree(sas_iounit_pg0);
3203 }
3204
3205 /**
3206  * _scsih_sas_host_add - create sas host object
3207  * @ioc: per adapter object
3208  *
3209  * Creating host side data object, stored in ioc->sas_hba
3210  *
3211  * Return nothing.
3212  */
3213 static void
3214 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3215 {
3216         int i;
3217         Mpi2ConfigReply_t mpi_reply;
3218         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3219         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3220         Mpi2SasPhyPage0_t phy_pg0;
3221         Mpi2SasDevicePage0_t sas_device_pg0;
3222         Mpi2SasEnclosurePage0_t enclosure_pg0;
3223         u16 ioc_status;
3224         u16 sz;
3225         u16 device_missing_delay;
3226
3227         mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3228         if (!ioc->sas_hba.num_phys) {
3229                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3230                     ioc->name, __FILE__, __LINE__, __func__);
3231                 return;
3232         }
3233
3234         /* sas_iounit page 0 */
3235         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3236             sizeof(Mpi2SasIOUnit0PhyData_t));
3237         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3238         if (!sas_iounit_pg0) {
3239                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3240                     ioc->name, __FILE__, __LINE__, __func__);
3241                 return;
3242         }
3243         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3244             sas_iounit_pg0, sz))) {
3245                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3246                     ioc->name, __FILE__, __LINE__, __func__);
3247                 goto out;
3248         }
3249         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3250             MPI2_IOCSTATUS_MASK;
3251         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3252                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3253                     ioc->name, __FILE__, __LINE__, __func__);
3254                 goto out;
3255         }
3256
3257         /* sas_iounit page 1 */
3258         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3259             sizeof(Mpi2SasIOUnit1PhyData_t));
3260         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3261         if (!sas_iounit_pg1) {
3262                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3263                     ioc->name, __FILE__, __LINE__, __func__);
3264                 goto out;
3265         }
3266         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3267             sas_iounit_pg1, sz))) {
3268                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3269                     ioc->name, __FILE__, __LINE__, __func__);
3270                 goto out;
3271         }
3272         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3273             MPI2_IOCSTATUS_MASK;
3274         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3275                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3276                     ioc->name, __FILE__, __LINE__, __func__);
3277                 goto out;
3278         }
3279
3280         ioc->io_missing_delay =
3281             le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3282         device_missing_delay =
3283             le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3284         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3285                 ioc->device_missing_delay = (device_missing_delay &
3286                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3287         else
3288                 ioc->device_missing_delay = device_missing_delay &
3289                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3290
3291         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3292         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3293             sizeof(struct _sas_phy), GFP_KERNEL);
3294         if (!ioc->sas_hba.phy) {
3295                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3296                     ioc->name, __FILE__, __LINE__, __func__);
3297                 goto out;
3298         }
3299         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3300                 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3301                     i))) {
3302                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3303                             ioc->name, __FILE__, __LINE__, __func__);
3304                         goto out;
3305                 }
3306                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3307                     MPI2_IOCSTATUS_MASK;
3308                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3309                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3310                             ioc->name, __FILE__, __LINE__, __func__);
3311                         goto out;
3312                 }
3313                 ioc->sas_hba.phy[i].handle =
3314                     le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3315                 ioc->sas_hba.phy[i].phy_id = i;
3316                 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3317                     phy_pg0, ioc->sas_hba.parent_dev);
3318         }
3319         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3320             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3321                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3322                     ioc->name, __FILE__, __LINE__, __func__);
3323                 goto out;
3324         }
3325         ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3326         ioc->sas_hba.enclosure_handle =
3327             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3328         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3329         printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3330             "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3331             (unsigned long long) ioc->sas_hba.sas_address,
3332             ioc->sas_hba.num_phys) ;
3333
3334         if (ioc->sas_hba.enclosure_handle) {
3335                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3336                     &enclosure_pg0,
3337                    MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3338                    ioc->sas_hba.enclosure_handle))) {
3339                         ioc->sas_hba.enclosure_logical_id =
3340                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3341                 }
3342         }
3343
3344  out:
3345         kfree(sas_iounit_pg1);
3346         kfree(sas_iounit_pg0);
3347 }
3348
3349 /**
3350  * _scsih_expander_add -  creating expander object
3351  * @ioc: per adapter object
3352  * @handle: expander handle
3353  *
3354  * Creating expander object, stored in ioc->sas_expander_list.
3355  *
3356  * Return 0 for success, else error.
3357  */
3358 static int
3359 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3360 {
3361         struct _sas_node *sas_expander;
3362         Mpi2ConfigReply_t mpi_reply;
3363         Mpi2ExpanderPage0_t expander_pg0;
3364         Mpi2ExpanderPage1_t expander_pg1;
3365         Mpi2SasEnclosurePage0_t enclosure_pg0;
3366         u32 ioc_status;
3367         u16 parent_handle;
3368         __le64 sas_address;
3369         int i;
3370         unsigned long flags;
3371         struct _sas_port *mpt2sas_port = NULL;
3372         int rc = 0;
3373
3374         if (!handle)
3375                 return -1;
3376
3377         if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3378             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3379                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3380                     ioc->name, __FILE__, __LINE__, __func__);
3381                 return -1;
3382         }
3383
3384         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3385             MPI2_IOCSTATUS_MASK;
3386         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3387                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3388                     ioc->name, __FILE__, __LINE__, __func__);
3389                 return -1;
3390         }
3391
3392         /* handle out of order topology events */
3393         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3394         if (parent_handle >= ioc->sas_hba.num_phys) {
3395                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3396                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3397                     parent_handle);
3398                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3399                 if (!sas_expander) {
3400                         rc = _scsih_expander_add(ioc, parent_handle);
3401                         if (rc != 0)
3402                                 return rc;
3403                 }
3404         }
3405
3406         sas_address = le64_to_cpu(expander_pg0.SASAddress);
3407
3408         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3409         sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3410             sas_address);
3411         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3412
3413         if (sas_expander)
3414                 return 0;
3415
3416         sas_expander = kzalloc(sizeof(struct _sas_node),
3417             GFP_KERNEL);
3418         if (!sas_expander) {
3419                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3420                     ioc->name, __FILE__, __LINE__, __func__);
3421                 return -1;
3422         }
3423
3424         sas_expander->handle = handle;
3425         sas_expander->num_phys = expander_pg0.NumPhys;
3426         sas_expander->parent_handle = parent_handle;
3427         sas_expander->enclosure_handle =
3428             le16_to_cpu(expander_pg0.EnclosureHandle);
3429         sas_expander->sas_address = sas_address;
3430
3431         printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3432             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3433             handle, sas_expander->parent_handle, (unsigned long long)
3434             sas_expander->sas_address, sas_expander->num_phys);
3435
3436         if (!sas_expander->num_phys)
3437                 goto out_fail;
3438         sas_expander->phy = kcalloc(sas_expander->num_phys,
3439             sizeof(struct _sas_phy), GFP_KERNEL);
3440         if (!sas_expander->phy) {
3441                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3442                     ioc->name, __FILE__, __LINE__, __func__);
3443                 rc = -1;
3444                 goto out_fail;
3445         }
3446
3447         INIT_LIST_HEAD(&sas_expander->sas_port_list);
3448         mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3449             sas_expander->parent_handle);
3450         if (!mpt2sas_port) {
3451                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3452                     ioc->name, __FILE__, __LINE__, __func__);
3453                 rc = -1;
3454                 goto out_fail;
3455         }
3456         sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3457
3458         for (i = 0 ; i < sas_expander->num_phys ; i++) {
3459                 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3460                     &expander_pg1, i, handle))) {
3461                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3462                             ioc->name, __FILE__, __LINE__, __func__);
3463                         rc = -1;
3464                         goto out_fail;
3465                 }
3466                 sas_expander->phy[i].handle = handle;
3467                 sas_expander->phy[i].phy_id = i;
3468
3469                 if ((mpt2sas_transport_add_expander_phy(ioc,
3470                     &sas_expander->phy[i], expander_pg1,
3471                     sas_expander->parent_dev))) {
3472                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3473                             ioc->name, __FILE__, __LINE__, __func__);
3474                         rc = -1;
3475                         goto out_fail;
3476                 }
3477         }
3478
3479         if (sas_expander->enclosure_handle) {
3480                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3481                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3482                    sas_expander->enclosure_handle))) {
3483                         sas_expander->enclosure_logical_id =
3484                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3485                 }
3486         }
3487
3488         _scsih_expander_node_add(ioc, sas_expander);
3489          return 0;
3490
3491  out_fail:
3492
3493         if (mpt2sas_port)
3494                 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3495                     sas_expander->parent_handle);
3496         kfree(sas_expander);
3497         return rc;
3498 }
3499
3500 /**
3501  * _scsih_expander_remove - removing expander object
3502  * @ioc: per adapter object
3503  * @handle: expander handle
3504  *
3505  * Return nothing.
3506  */
3507 static void
3508 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3509 {
3510         struct _sas_node *sas_expander;
3511         unsigned long flags;
3512
3513         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3514         sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3515         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3516         _scsih_expander_node_remove(ioc, sas_expander);
3517 }
3518
3519 /**
3520  * _scsih_add_device -  creating sas device object
3521  * @ioc: per adapter object
3522  * @handle: sas device handle
3523  * @phy_num: phy number end device attached to
3524  * @is_pd: is this hidden raid component
3525  *
3526  * Creating end device object, stored in ioc->sas_device_list.
3527  *
3528  * Returns 0 for success, non-zero for failure.
3529  */
3530 static int
3531 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3532 {
3533         Mpi2ConfigReply_t mpi_reply;
3534         Mpi2SasDevicePage0_t sas_device_pg0;
3535         Mpi2SasEnclosurePage0_t enclosure_pg0;
3536         struct _sas_device *sas_device;
3537         u32 ioc_status;
3538         __le64 sas_address;
3539         u32 device_info;
3540         unsigned long flags;
3541
3542         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3543             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3544                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3545                     ioc->name, __FILE__, __LINE__, __func__);
3546                 return -1;
3547         }
3548
3549         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3550             MPI2_IOCSTATUS_MASK;
3551         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3552                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3553                     ioc->name, __FILE__, __LINE__, __func__);
3554                 return -1;
3555         }
3556
3557         /* check if device is present */
3558         if (!(le16_to_cpu(sas_device_pg0.Flags) &
3559             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3560                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3561                     ioc->name, __FILE__, __LINE__, __func__);
3562                 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3563                     ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3564                 return -1;
3565         }
3566
3567         /* check if there were any issus with discovery */
3568         if (sas_device_pg0.AccessStatus ==
3569             MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3570                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3571                     ioc->name, __FILE__, __LINE__, __func__);
3572                 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3573                     ioc->name, sas_device_pg0.AccessStatus);
3574                 return -1;
3575         }
3576
3577         /* check if this is end device */
3578         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3579         if (!(_scsih_is_end_device(device_info))) {
3580                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3581                     ioc->name, __FILE__, __LINE__, __func__);
3582                 return -1;
3583         }
3584
3585         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3586
3587         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3588         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3589             sas_address);
3590         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3591
3592         if (sas_device) {
3593                 _scsih_ublock_io_device(ioc, handle);
3594                 return 0;
3595         }
3596
3597         sas_device = kzalloc(sizeof(struct _sas_device),
3598             GFP_KERNEL);
3599         if (!sas_device) {
3600                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3601                     ioc->name, __FILE__, __LINE__, __func__);
3602                 return -1;
3603         }
3604
3605         sas_device->handle = handle;
3606         sas_device->parent_handle =
3607             le16_to_cpu(sas_device_pg0.ParentDevHandle);
3608         sas_device->enclosure_handle =
3609             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3610         sas_device->slot =
3611             le16_to_cpu(sas_device_pg0.Slot);
3612         sas_device->device_info = device_info;
3613         sas_device->sas_address = sas_address;
3614         sas_device->hidden_raid_component = is_pd;
3615
3616         /* get enclosure_logical_id */
3617         if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3618            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3619            sas_device->enclosure_handle)))
3620                 sas_device->enclosure_logical_id =
3621                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3622
3623         /* get device name */
3624         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3625
3626         if (ioc->wait_for_port_enable_to_complete)
3627                 _scsih_sas_device_init_add(ioc, sas_device);
3628         else
3629                 _scsih_sas_device_add(ioc, sas_device);
3630
3631         return 0;
3632 }
3633
3634 /**
3635  * _scsih_remove_device -  removing sas device object
3636  * @ioc: per adapter object
3637  * @handle: sas device handle
3638  *
3639  * Return nothing.
3640  */
3641 static void
3642 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3643 {
3644         struct MPT2SAS_TARGET *sas_target_priv_data;
3645         struct _sas_device *sas_device;
3646         unsigned long flags;
3647         Mpi2SasIoUnitControlReply_t mpi_reply;
3648         Mpi2SasIoUnitControlRequest_t mpi_request;
3649         u16 device_handle;
3650
3651         /* lookup sas_device */
3652         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3653         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3654         if (!sas_device) {
3655                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3656                 return;
3657         }
3658
3659         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3660             "(0x%04x)\n", ioc->name, __func__, handle));
3661
3662         if (sas_device->starget && sas_device->starget->hostdata) {
3663                 sas_target_priv_data = sas_device->starget->hostdata;
3664                 sas_target_priv_data->deleted = 1;
3665         }
3666         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3667
3668         if (ioc->remove_host)
3669                 goto out;
3670
3671         /* Target Reset to flush out all the outstanding IO */
3672         device_handle = (sas_device->hidden_raid_component) ?
3673             sas_device->volume_handle : handle;
3674         if (device_handle) {
3675                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3676                     "handle(0x%04x)\n", ioc->name, device_handle));
3677                 mutex_lock(&ioc->tm_cmds.mutex);
3678                 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3679                     MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3680                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3681                 mutex_unlock(&ioc->tm_cmds.mutex);
3682                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3683                     "done: handle(0x%04x)\n", ioc->name, device_handle));
3684         }
3685
3686         /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3687         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3688             "(0x%04x)\n", ioc->name, handle));
3689         memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3690         mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3691         mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3692         mpi_request.DevHandle = handle;
3693         mpi_request.VF_ID = 0;
3694         if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3695             &mpi_request)) != 0) {
3696                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3697                     ioc->name, __FILE__, __LINE__, __func__);
3698         }
3699
3700         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3701             "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3702             le16_to_cpu(mpi_reply.IOCStatus),
3703             le32_to_cpu(mpi_reply.IOCLogInfo)));
3704
3705  out:
3706         mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3707             sas_device->parent_handle);
3708
3709         printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3710             "(0x%016llx)\n", ioc->name, sas_device->handle,
3711             (unsigned long long) sas_device->sas_address);
3712         _scsih_sas_device_remove(ioc, sas_device);
3713
3714         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3715             "(0x%04x)\n", ioc->name, __func__, handle));
3716 }
3717
3718 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3719 /**
3720  * _scsih_sas_topology_change_event_debug - debug for topology event
3721  * @ioc: per adapter object
3722  * @event_data: event data payload
3723  * Context: user.
3724  */
3725 static void
3726 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3727     Mpi2EventDataSasTopologyChangeList_t *event_data)
3728 {
3729         int i;
3730         u16 handle;
3731         u16 reason_code;
3732         u8 phy_number;
3733         char *status_str = NULL;
3734         char link_rate[25];
3735
3736         switch (event_data->ExpStatus) {
3737         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3738                 status_str = "add";
3739                 break;
3740         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3741                 status_str = "remove";
3742                 break;
3743         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3744                 status_str =  "responding";
3745                 break;
3746         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3747                 status_str = "remove delay";
3748                 break;
3749         default:
3750                 status_str = "unknown status";
3751                 break;
3752         }
3753         printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3754             ioc->name, status_str);
3755         printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3756             "start_phy(%02d), count(%d)\n",
3757             le16_to_cpu(event_data->ExpanderDevHandle),
3758             le16_to_cpu(event_data->EnclosureHandle),
3759             event_data->StartPhyNum, event_data->NumEntries);
3760         for (i = 0; i < event_data->NumEntries; i++) {
3761                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3762                 if (!handle)
3763                         continue;
3764                 phy_number = event_data->StartPhyNum + i;
3765                 reason_code = event_data->PHY[i].PhyStatus &
3766                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3767                 switch (reason_code) {
3768                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3769                         snprintf(link_rate, 25, ": add, link(0x%02x)",
3770                             (event_data->PHY[i].LinkRate >> 4));
3771                         status_str = link_rate;
3772                         break;
3773                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3774                         status_str = ": remove";
3775                         break;
3776                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3777                         status_str = ": remove_delay";
3778                         break;
3779                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3780                         snprintf(link_rate, 25, ": link(0x%02x)",
3781                             (event_data->PHY[i].LinkRate >> 4));
3782                         status_str = link_rate;
3783                         break;
3784                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3785                         status_str = ": responding";
3786                         break;
3787                 default:
3788                         status_str = ": unknown";
3789                         break;
3790                 }
3791                 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3792                     phy_number, handle, status_str);
3793         }
3794 }
3795 #endif
3796
3797 /**
3798  * _scsih_sas_topology_change_event - handle topology changes
3799  * @ioc: per adapter object
3800  * @VF_ID:
3801  * @event_data: event data payload
3802  * fw_event:
3803  * Context: user.
3804  *
3805  */
3806 static void
3807 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3808     Mpi2EventDataSasTopologyChangeList_t *event_data,
3809     struct fw_event_work *fw_event)
3810 {
3811         int i;
3812         u16 parent_handle, handle;
3813         u16 reason_code;
3814         u8 phy_number;
3815         struct _sas_node *sas_expander;
3816         unsigned long flags;
3817         u8 link_rate_;
3818
3819 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3820         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3821                 _scsih_sas_topology_change_event_debug(ioc, event_data);
3822 #endif
3823
3824         if (!ioc->sas_hba.num_phys)
3825                 _scsih_sas_host_add(ioc);
3826         else
3827                 _scsih_sas_host_refresh(ioc, 0);
3828
3829         if (fw_event->ignore) {
3830                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3831                     "event\n", ioc->name));
3832                 return;
3833         }
3834
3835         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3836
3837         /* handle expander add */
3838         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3839                 if (_scsih_expander_add(ioc, parent_handle) != 0)
3840                         return;
3841
3842         /* handle siblings events */
3843         for (i = 0; i < event_data->NumEntries; i++) {
3844                 if (fw_event->ignore) {
3845                         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3846                             "expander event\n", ioc->name));
3847                         return;
3848                 }
3849                 if (event_data->PHY[i].PhyStatus &
3850                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3851                         continue;
3852                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3853                 if (!handle)
3854                         continue;
3855                 phy_number = event_data->StartPhyNum + i;
3856                 reason_code = event_data->PHY[i].PhyStatus &
3857                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3858                 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3859                 switch (reason_code) {
3860                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3861                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3862                         if (!parent_handle) {
3863                                 if (phy_number < ioc->sas_hba.num_phys)
3864                                         _scsih_link_change(ioc,
3865                                            ioc->sas_hba.phy[phy_number].handle,
3866                                            handle, phy_number, link_rate_);
3867                         } else {
3868                                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3869                                 sas_expander =
3870                                     mpt2sas_scsih_expander_find_by_handle(ioc,
3871                                         parent_handle);
3872                                 spin_unlock_irqrestore(&ioc->sas_node_lock,
3873                                     flags);
3874                                 if (sas_expander) {
3875                                         if (phy_number < sas_expander->num_phys)
3876                                                 _scsih_link_change(ioc,
3877                                                    sas_expander->
3878                                                    phy[phy_number].handle,
3879                                                    handle, phy_number,
3880                                                    link_rate_);
3881                                 }
3882                         }
3883                         if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
3884                                 if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
3885                                         _scsih_ublock_io_device(ioc, handle);
3886                         }
3887                         if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3888                                 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3889                                         break;
3890                                 _scsih_add_device(ioc, handle, phy_number, 0);
3891                         }
3892                         break;
3893                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3894                         _scsih_remove_device(ioc, handle);
3895                         break;
3896                 }
3897         }
3898
3899         /* handle expander removal */
3900         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3901                 _scsih_expander_remove(ioc, parent_handle);
3902
3903 }
3904
3905 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3906 /**
3907  * _scsih_sas_device_status_change_event_debug - debug for device event
3908  * @event_data: event data payload
3909  * Context: user.
3910  *
3911  * Return nothing.
3912  */
3913 static void
3914 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3915     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3916 {
3917         char *reason_str = NULL;
3918
3919         switch (event_data->ReasonCode) {
3920         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3921                 reason_str = "smart data";
3922                 break;
3923         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3924                 reason_str = "unsupported device discovered";
3925                 break;
3926         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3927                 reason_str = "internal device reset";
3928                 break;
3929         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3930                 reason_str = "internal task abort";
3931                 break;
3932         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3933                 reason_str = "internal task abort set";
3934                 break;
3935         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3936                 reason_str = "internal clear task set";
3937                 break;
3938         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3939                 reason_str = "internal query task";
3940                 break;
3941         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3942                 reason_str = "sata init failure";
3943                 break;
3944         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3945                 reason_str = "internal device reset complete";
3946                 break;
3947         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3948                 reason_str = "internal task abort complete";
3949                 break;
3950         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3951                 reason_str = "internal async notification";
3952                 break;
3953         default:
3954                 reason_str = "unknown reason";
3955                 break;
3956         }
3957         printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3958             "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3959             reason_str, le16_to_cpu(event_data->DevHandle),
3960             (unsigned long long)le64_to_cpu(event_data->SASAddress));
3961         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3962                 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3963                     event_data->ASC, event_data->ASCQ);
3964         printk(KERN_INFO "\n");
3965 }
3966 #endif
3967
3968 /**
3969  * _scsih_sas_device_status_change_event - handle device status change
3970  * @ioc: per adapter object
3971  * @VF_ID:
3972  * @event_data: event data payload
3973  * Context: user.
3974  *
3975  * Return nothing.
3976  */
3977 static void
3978 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3979     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3980 {
3981 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3982         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3983                 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3984 #endif
3985 }
3986
3987 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3988 /**
3989  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3990  * @ioc: per adapter object
3991  * @event_data: event data payload
3992  * Context: user.
3993  *
3994  * Return nothing.
3995  */
3996 static void
3997 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3998     Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3999 {
4000         char *reason_str = NULL;
4001
4002         switch (event_data->ReasonCode) {
4003         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4004                 reason_str = "enclosure add";
4005                 break;
4006         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4007                 reason_str = "enclosure remove";
4008                 break;
4009         default:
4010                 reason_str = "unknown reason";
4011                 break;
4012         }
4013
4014         printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4015             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4016             " number slots(%d)\n", ioc->name, reason_str,
4017             le16_to_cpu(event_data->EnclosureHandle),
4018             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4019             le16_to_cpu(event_data->StartSlot));
4020 }
4021 #endif
4022
4023 /**
4024  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4025  * @ioc: per adapter object
4026  * @VF_ID:
4027  * @event_data: event data payload
4028  * Context: user.
4029  *
4030  * Return nothing.
4031  */
4032 static void
4033 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4034     u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4035 {
4036 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4037         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4038                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
4039                      event_data);
4040 #endif
4041 }
4042
4043 /**
4044  * _scsih_sas_broadcast_primative_event - handle broadcast events
4045  * @ioc: per adapter object
4046  * @event_data: event data payload
4047  * Context: user.
4048  *
4049  * Return nothing.
4050  */
4051 static void
4052 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4053     Mpi2EventDataSasBroadcastPrimitive_t *event_data)
4054 {
4055         struct scsi_cmnd *scmd;
4056         u16 smid, handle;
4057         u32 lun;
4058         struct MPT2SAS_DEVICE *sas_device_priv_data;
4059         u32 termination_count;
4060         u32 query_count;
4061         Mpi2SCSITaskManagementReply_t *mpi_reply;
4062
4063         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4064             "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4065             event_data->PortWidth));
4066
4067         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4068             __func__));
4069
4070         mutex_lock(&ioc->tm_cmds.mutex);
4071         termination_count = 0;
4072         query_count = 0;
4073         mpi_reply = ioc->tm_cmds.reply;
4074         for (smid = 1; smid <= ioc->request_depth; smid++) {
4075                 scmd = _scsih_scsi_lookup_get(ioc, smid);
4076                 if (!scmd)
4077                         continue;
4078                 sas_device_priv_data = scmd->device->hostdata;
4079                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4080                         continue;
4081                  /* skip hidden raid components */
4082                 if (sas_device_priv_data->sas_target->flags &
4083                     MPT_TARGET_FLAGS_RAID_COMPONENT)
4084                         continue;
4085                  /* skip volumes */
4086                 if (sas_device_priv_data->sas_target->flags &
4087                     MPT_TARGET_FLAGS_VOLUME)
4088                         continue;
4089
4090                 handle = sas_device_priv_data->sas_target->handle;
4091                 lun = sas_device_priv_data->lun;
4092                 query_count++;
4093
4094                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4095                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
4096                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4097
4098                 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4099                     (mpi_reply->ResponseCode ==
4100                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4101                      mpi_reply->ResponseCode ==
4102                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4103                         continue;
4104
4105                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4106                     MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4107                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4108                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4109         }
4110         ioc->broadcast_aen_busy = 0;
4111         mutex_unlock(&ioc->tm_cmds.mutex);
4112
4113         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4114             "%s - exit, query_count = %d termination_count = %d\n",
4115             ioc->name, __func__, query_count, termination_count));
4116 }
4117
4118 /**
4119  * _scsih_sas_discovery_event - handle discovery events
4120  * @ioc: per adapter object
4121  * @event_data: event data payload
4122  * Context: user.
4123  *
4124  * Return nothing.
4125  */
4126 static void
4127 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4128     Mpi2EventDataSasDiscovery_t *event_data)
4129 {
4130 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4131         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4132                 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4133                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4134                     "start" : "stop");
4135         if (event_data->DiscoveryStatus)
4136                 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
4137                     ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
4138         printk("\n");
4139         }
4140 #endif
4141
4142         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4143             !ioc->sas_hba.num_phys)
4144                 _scsih_sas_host_add(ioc);
4145 }
4146
4147 /**
4148  * _scsih_reprobe_lun - reprobing lun
4149  * @sdev: scsi device struct
4150  * @no_uld_attach: sdev->no_uld_attach flag setting
4151  *
4152  **/
4153 static void
4154 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4155 {
4156         int rc;
4157
4158         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4159         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4160             sdev->no_uld_attach ? "hidding" : "exposing");
4161         rc = scsi_device_reprobe(sdev);
4162 }
4163
4164 /**
4165  * _scsih_reprobe_target - reprobing target
4166  * @starget: scsi target struct
4167  * @no_uld_attach: sdev->no_uld_attach flag setting
4168  *
4169  * Note: no_uld_attach flag determines whether the disk device is attached
4170  * to block layer. A value of `1` means to not attach.
4171  **/
4172 static void
4173 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4174 {
4175         struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4176
4177         if (no_uld_attach)
4178                 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4179         else
4180                 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4181
4182         starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4183             _scsih_reprobe_lun);
4184 }
4185 /**
4186  * _scsih_sas_volume_add - add new volume
4187  * @ioc: per adapter object
4188  * @element: IR config element data
4189  * Context: user.
4190  *
4191  * Return nothing.
4192  */
4193 static void
4194 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4195     Mpi2EventIrConfigElement_t *element)
4196 {
4197         struct _raid_device *raid_device;
4198         unsigned long flags;
4199         u64 wwid;
4200         u16 handle = le16_to_cpu(element->VolDevHandle);
4201         int rc;
4202
4203         mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4204         if (!wwid) {
4205                 printk(MPT2SAS_ERR_FMT
4206                     "failure at %s:%d/%s()!\n", ioc->name,
4207                     __FILE__, __LINE__, __func__);
4208                 return;
4209         }
4210
4211         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4212         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4213         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4214
4215         if (raid_device)
4216                 return;
4217
4218         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4219         if (!raid_device) {
4220                 printk(MPT2SAS_ERR_FMT
4221                     "failure at %s:%d/%s()!\n", ioc->name,
4222                     __FILE__, __LINE__, __func__);
4223                 return;
4224         }
4225
4226         raid_device->id = ioc->sas_id++;
4227         raid_device->channel = RAID_CHANNEL;
4228         raid_device->handle = handle;
4229         raid_device->wwid = wwid;
4230         _scsih_raid_device_add(ioc, raid_device);
4231         if (!ioc->wait_for_port_enable_to_complete) {
4232                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4233                     raid_device->id, 0);
4234                 if (rc)
4235                         _scsih_raid_device_remove(ioc, raid_device);
4236         } else
4237                 _scsih_determine_boot_device(ioc, raid_device, 1);
4238 }
4239
4240 /**
4241  * _scsih_sas_volume_delete - delete volume
4242  * @ioc: per adapter object
4243  * @element: IR config element data
4244  * Context: user.
4245  *
4246  * Return nothing.
4247  */
4248 static void
4249 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4250     Mpi2EventIrConfigElement_t *element)
4251 {
4252         struct _raid_device *raid_device;
4253         u16 handle = le16_to_cpu(element->VolDevHandle);
4254         unsigned long flags;
4255         struct MPT2SAS_TARGET *sas_target_priv_data;
4256
4257         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4258         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4259         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4260         if (!raid_device)
4261                 return;
4262         if (raid_device->starget) {
4263                 sas_target_priv_data = raid_device->starget->hostdata;
4264                 sas_target_priv_data->deleted = 1;
4265                 scsi_remove_target(&raid_device->starget->dev);
4266         }
4267         _scsih_raid_device_remove(ioc, raid_device);
4268 }
4269
4270 /**
4271  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4272  * @ioc: per adapter object
4273  * @element: IR config element data
4274  * Context: user.
4275  *
4276  * Return nothing.
4277  */
4278 static void
4279 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4280     Mpi2EventIrConfigElement_t *element)
4281 {
4282         struct _sas_device *sas_device;
4283         unsigned long flags;
4284         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4285
4286         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4287         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4288         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4289         if (!sas_device)
4290                 return;
4291
4292         /* exposing raid component */
4293         sas_device->volume_handle = 0;
4294         sas_device->volume_wwid = 0;
4295         sas_device->hidden_raid_component = 0;
4296         _scsih_reprobe_target(sas_device->starget, 0);
4297 }
4298
4299 /**
4300  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4301  * @ioc: per adapter object
4302  * @element: IR config element data
4303  * Context: user.
4304  *
4305  * Return nothing.
4306  */
4307 static void
4308 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4309     Mpi2EventIrConfigElement_t *element)
4310 {
4311         struct _sas_device *sas_device;
4312         unsigned long flags;
4313         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4314
4315         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4316         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4317         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4318         if (!sas_device)
4319                 return;
4320
4321         /* hiding raid component */
4322         mpt2sas_config_get_volume_handle(ioc, handle,
4323             &sas_device->volume_handle);
4324         mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4325             &sas_device->volume_wwid);
4326         sas_device->hidden_raid_component = 1;
4327         _scsih_reprobe_target(sas_device->starget, 1);
4328 }
4329
4330 /**
4331  * _scsih_sas_pd_delete - delete pd component
4332  * @ioc: per adapter object
4333  * @element: IR config element data
4334  * Context: user.
4335  *
4336  * Return nothing.
4337  */
4338 static void
4339 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4340     Mpi2EventIrConfigElement_t *element)
4341 {
4342         struct _sas_device *sas_device;
4343         unsigned long flags;
4344         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4345
4346         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4347         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4348         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4349         if (!sas_device)
4350                 return;
4351         _scsih_remove_device(ioc, handle);
4352 }
4353
4354 /**
4355  * _scsih_sas_pd_add - remove pd component
4356  * @ioc: per adapter object
4357  * @element: IR config element data
4358  * Context: user.
4359  *
4360  * Return nothing.
4361  */
4362 static void
4363 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4364     Mpi2EventIrConfigElement_t *element)
4365 {
4366         struct _sas_device *sas_device;
4367         unsigned long flags;
4368         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4369         Mpi2ConfigReply_t mpi_reply;
4370         Mpi2SasDevicePage0_t sas_device_pg0;
4371         u32 ioc_status;
4372
4373         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4374         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4375         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4376         if (sas_device) {
4377                 sas_device->hidden_raid_component = 1;
4378                 return;
4379         }
4380
4381         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4382             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4383                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4384                     ioc->name, __FILE__, __LINE__, __func__);
4385                 return;
4386         }
4387
4388         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4389             MPI2_IOCSTATUS_MASK;
4390         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4391                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4392                     ioc->name, __FILE__, __LINE__, __func__);
4393                 return;
4394         }
4395
4396         _scsih_link_change(ioc,
4397             le16_to_cpu(sas_device_pg0.ParentDevHandle),
4398             handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4399
4400         _scsih_add_device(ioc, handle, 0, 1);
4401 }
4402
4403 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4404 /**
4405  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4406  * @ioc: per adapter object
4407  * @event_data: event data payload
4408  * Context: user.
4409  *
4410  * Return nothing.
4411  */
4412 static void
4413 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4414     Mpi2EventDataIrConfigChangeList_t *event_data)
4415 {
4416         Mpi2EventIrConfigElement_t *element;
4417         u8 element_type;
4418         int i;
4419         char *reason_str = NULL, *element_str = NULL;
4420
4421         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4422
4423         printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4424             ioc->name, (le32_to_cpu(event_data->Flags) &
4425             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4426             "foreign" : "native", event_data->NumElements);
4427         for (i = 0; i < event_data->NumElements; i++, element++) {
4428                 switch (element->ReasonCode) {
4429                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4430                         reason_str = "add";
4431                         break;
4432                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4433                         reason_str = "remove";
4434                         break;
4435                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4436                         reason_str = "no change";
4437                         break;
4438                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4439                         reason_str = "hide";
4440                         break;
4441                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4442                         reason_str = "unhide";
4443                         break;
4444                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4445                         reason_str = "volume_created";
4446                         break;
4447                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4448                         reason_str = "volume_deleted";
4449                         break;
4450                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4451                         reason_str = "pd_created";
4452                         break;
4453                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4454                         reason_str = "pd_deleted";
4455                         break;
4456                 default:
4457                         reason_str = "unknown reason";
4458                         break;
4459                 }
4460                 element_type = le16_to_cpu(element->ElementFlags) &
4461                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4462                 switch (element_type) {
4463                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4464                         element_str = "volume";
4465                         break;
4466                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4467                         element_str = "phys disk";
4468                         break;
4469                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4470                         element_str = "hot spare";
4471                         break;
4472                 default:
4473                         element_str = "unknown element";
4474                         break;
4475                 }
4476                 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4477                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4478                     reason_str, le16_to_cpu(element->VolDevHandle),
4479                     le16_to_cpu(element->PhysDiskDevHandle),
4480                     element->PhysDiskNum);
4481         }
4482 }
4483 #endif
4484
4485 /**
4486  * _scsih_sas_ir_config_change_event - handle ir configuration change events
4487  * @ioc: per adapter object
4488  * @VF_ID:
4489  * @event_data: event data payload
4490  * Context: user.
4491  *
4492  * Return nothing.
4493  */
4494 static void
4495 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4496     Mpi2EventDataIrConfigChangeList_t *event_data)
4497 {
4498         Mpi2EventIrConfigElement_t *element;
4499         int i;
4500         u8 foreign_config;
4501
4502 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4503         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4504                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4505
4506 #endif
4507         foreign_config = (le32_to_cpu(event_data->Flags) &
4508             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
4509
4510         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4511         for (i = 0; i < event_data->NumElements; i++, element++) {
4512
4513                 switch (element->ReasonCode) {
4514                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4515                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4516                         if (!foreign_config)
4517                                 _scsih_sas_volume_add(ioc, element);
4518                         break;
4519                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4520                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4521                         if (!foreign_config)
4522                                 _scsih_sas_volume_delete(ioc, element);
4523                         break;
4524                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4525                         _scsih_sas_pd_hide(ioc, element);
4526                         break;
4527                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4528                         _scsih_sas_pd_expose(ioc, element);
4529                         break;
4530                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4531                         _scsih_sas_pd_add(ioc, element);
4532                         break;
4533                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4534                         _scsih_sas_pd_delete(ioc, element);
4535                         break;
4536                 }
4537         }
4538 }
4539
4540 /**
4541  * _scsih_sas_ir_volume_event - IR volume event
4542  * @ioc: per adapter object
4543  * @event_data: event data payload
4544  * Context: user.
4545  *
4546  * Return nothing.
4547  */
4548 static void
4549 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4550     Mpi2EventDataIrVolume_t *event_data)
4551 {
4552         u64 wwid;
4553         unsigned long flags;
4554         struct _raid_device *raid_device;
4555         u16 handle;
4556         u32 state;
4557         int rc;
4558         struct MPT2SAS_TARGET *sas_target_priv_data;
4559
4560         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4561                 return;
4562
4563         handle = le16_to_cpu(event_data->VolDevHandle);
4564         state = le32_to_cpu(event_data->NewValue);
4565         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4566             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4567             le32_to_cpu(event_data->PreviousValue), state));
4568
4569         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4570         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4571         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4572
4573         switch (state) {
4574         case MPI2_RAID_VOL_STATE_MISSING:
4575         case MPI2_RAID_VOL_STATE_FAILED:
4576                 if (!raid_device)
4577                         break;
4578                 if (raid_device->starget) {
4579                         sas_target_priv_data = raid_device->starget->hostdata;
4580                         sas_target_priv_data->deleted = 1;
4581                         scsi_remove_target(&raid_device->starget->dev);
4582                 }
4583                 _scsih_raid_device_remove(ioc, raid_device);
4584                 break;
4585
4586         case MPI2_RAID_VOL_STATE_ONLINE:
4587         case MPI2_RAID_VOL_STATE_DEGRADED:
4588         case MPI2_RAID_VOL_STATE_OPTIMAL:
4589                 if (raid_device)
4590                         break;
4591
4592                 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4593                 if (!wwid) {
4594                         printk(MPT2SAS_ERR_FMT
4595                             "failure at %s:%d/%s()!\n", ioc->name,
4596                             __FILE__, __LINE__, __func__);
4597                         break;
4598                 }
4599
4600                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4601                 if (!raid_device) {
4602                         printk(MPT2SAS_ERR_FMT
4603                             "failure at %s:%d/%s()!\n", ioc->name,
4604                             __FILE__, __LINE__, __func__);
4605                         break;
4606                 }
4607
4608                 raid_device->id = ioc->sas_id++;
4609                 raid_device->channel = RAID_CHANNEL;
4610                 raid_device->handle = handle;
4611                 raid_device->wwid = wwid;
4612                 _scsih_raid_device_add(ioc, raid_device);
4613                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4614                     raid_device->id, 0);
4615                 if (rc)
4616                         _scsih_raid_device_remove(ioc, raid_device);
4617                 break;
4618
4619         case MPI2_RAID_VOL_STATE_INITIALIZING:
4620         default:
4621                 break;
4622         }
4623 }
4624
4625 /**
4626  * _scsih_sas_ir_physical_disk_event - PD event
4627  * @ioc: per adapter object
4628  * @event_data: event data payload
4629  * Context: user.
4630  *
4631  * Return nothing.
4632  */
4633 static void
4634 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4635    Mpi2EventDataIrPhysicalDisk_t *event_data)
4636 {
4637         u16 handle;
4638         u32 state;
4639         struct _sas_device *sas_device;
4640         unsigned long flags;
4641         Mpi2ConfigReply_t mpi_reply;
4642         Mpi2SasDevicePage0_t sas_device_pg0;
4643         u32 ioc_status;
4644
4645         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4646                 return;
4647
4648         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4649         state = le32_to_cpu(event_data->NewValue);
4650
4651         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4652             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4653             le32_to_cpu(event_data->PreviousValue), state));
4654
4655         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4656         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4657         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4658
4659         switch (state) {
4660         case MPI2_RAID_PD_STATE_ONLINE:
4661         case MPI2_RAID_PD_STATE_DEGRADED:
4662         case MPI2_RAID_PD_STATE_REBUILDING:
4663         case MPI2_RAID_PD_STATE_OPTIMAL:
4664                 if (sas_device) {
4665                         sas_device->hidden_raid_component = 1;
4666                         return;
4667                 }
4668
4669                 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4670                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4671                     handle))) {
4672                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4673                             ioc->name, __FILE__, __LINE__, __func__);
4674                         return;
4675                 }
4676
4677                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4678                     MPI2_IOCSTATUS_MASK;
4679                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4680                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4681                             ioc->name, __FILE__, __LINE__, __func__);
4682                         return;
4683                 }
4684
4685                 _scsih_link_change(ioc,
4686                     le16_to_cpu(sas_device_pg0.ParentDevHandle),
4687                     handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4688
4689                 _scsih_add_device(ioc, handle, 0, 1);
4690
4691                 break;
4692
4693         case MPI2_RAID_PD_STATE_OFFLINE:
4694         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4695         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4696         case MPI2_RAID_PD_STATE_HOT_SPARE:
4697         default:
4698                 break;
4699         }
4700 }
4701
4702 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4703 /**
4704  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4705  * @ioc: per adapter object
4706  * @event_data: event data payload
4707  * Context: user.
4708  *
4709  * Return nothing.
4710  */
4711 static void
4712 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4713     Mpi2EventDataIrOperationStatus_t *event_data)
4714 {
4715         char *reason_str = NULL;
4716
4717         switch (event_data->RAIDOperation) {
4718         case MPI2_EVENT_IR_RAIDOP_RESYNC:
4719                 reason_str = "resync";
4720                 break;
4721         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4722                 reason_str = "online capacity expansion";
4723                 break;
4724         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4725                 reason_str = "consistency check";
4726                 break;
4727         default:
4728                 reason_str = "unknown reason";
4729                 break;
4730         }
4731
4732         printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4733             "\thandle(0x%04x), percent complete(%d)\n",
4734             ioc->name, reason_str,
4735             le16_to_cpu(event_data->VolDevHandle),
4736             event_data->PercentComplete);
4737 }
4738 #endif
4739
4740 /**
4741  * _scsih_sas_ir_operation_status_event - handle RAID operation events
4742  * @ioc: per adapter object
4743  * @VF_ID:
4744  * @event_data: event data payload
4745  * Context: user.
4746  *
4747  * Return nothing.
4748  */
4749 static void
4750 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4751     Mpi2EventDataIrOperationStatus_t *event_data)
4752 {
4753 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4754         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4755                 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4756 #endif
4757 }
4758
4759 /**
4760  * _scsih_task_set_full - handle task set full
4761  * @ioc: per adapter object
4762  * @event_data: event data payload
4763  * Context: user.
4764  *
4765  * Throttle back qdepth.
4766  */
4767 static void
4768 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4769     Mpi2EventDataTaskSetFull_t *event_data)
4770 {
4771         unsigned long flags;
4772         struct _sas_device *sas_device;
4773         static struct _raid_device *raid_device;
4774         struct scsi_device *sdev;
4775         int depth;
4776         u16 current_depth;
4777         u16 handle;
4778         int id, channel;
4779         u64 sas_address;
4780
4781         current_depth = le16_to_cpu(event_data->CurrentDepth);
4782         handle = le16_to_cpu(event_data->DevHandle);
4783         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4784         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4785         if (!sas_device) {
4786                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4787                 return;
4788         }
4789         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4790         id = sas_device->id;
4791         channel = sas_device->channel;
4792         sas_address = sas_device->sas_address;
4793
4794         /* if hidden raid component, then change to volume characteristics */
4795         if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4796                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4797                 raid_device = _scsih_raid_device_find_by_handle(
4798                     ioc, sas_device->volume_handle);
4799                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4800                 if (raid_device) {
4801                         id = raid_device->id;
4802                         channel = raid_device->channel;
4803                         handle = raid_device->handle;
4804                         sas_address = raid_device->wwid;
4805                 }
4806         }
4807
4808         if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4809                 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4810                     "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4811                     handle, (unsigned long long)sas_address, current_depth);
4812
4813         shost_for_each_device(sdev, ioc->shost) {
4814                 if (sdev->id == id && sdev->channel == channel) {
4815                         if (current_depth > sdev->queue_depth) {
4816                                 if (ioc->logging_level &
4817                                     MPT_DEBUG_TASK_SET_FULL)
4818                                         sdev_printk(KERN_INFO, sdev, "strange "
4819                                             "observation, the queue depth is"
4820                                             " (%d) meanwhile fw queue depth "
4821                                             "is (%d)\n", sdev->queue_depth,
4822                                             current_depth);
4823                                 continue;
4824                         }
4825                         depth = scsi_track_queue_full(sdev,
4826                             current_depth - 1);
4827                         if (depth > 0)
4828                                 sdev_printk(KERN_INFO, sdev, "Queue depth "
4829                                     "reduced to (%d)\n", depth);
4830                         else if (depth < 0)
4831                                 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4832                                     "Queueing is being disabled\n");
4833                         else if (depth == 0)
4834                                 if (ioc->logging_level &
4835                                      MPT_DEBUG_TASK_SET_FULL)
4836                                         sdev_printk(KERN_INFO, sdev,
4837                                              "Queue depth not changed yet\n");
4838                 }
4839         }
4840 }
4841
4842 /**
4843  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4844  * @ioc: per adapter object
4845  * @sas_address: sas address
4846  * @slot: enclosure slot id
4847  * @handle: device handle
4848  *
4849  * After host reset, find out whether devices are still responding.
4850  * Used in _scsi_remove_unresponsive_sas_devices.
4851  *
4852  * Return nothing.
4853  */
4854 static void
4855 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4856     u16 slot, u16 handle)
4857 {
4858         struct MPT2SAS_TARGET *sas_target_priv_data;
4859         struct scsi_target *starget;
4860         struct _sas_device *sas_device;
4861         unsigned long flags;
4862
4863         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4864         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4865                 if (sas_device->sas_address == sas_address &&
4866                     sas_device->slot == slot && sas_device->starget) {
4867                         sas_device->responding = 1;
4868                         starget_printk(KERN_INFO, sas_device->starget,
4869                             "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4870                             "logical id(0x%016llx), slot(%d)\n", handle,
4871                             (unsigned long long)sas_device->sas_address,
4872                             (unsigned long long)
4873                             sas_device->enclosure_logical_id,
4874                             sas_device->slot);
4875                         if (sas_device->handle == handle)
4876                                 goto out;
4877                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4878                             sas_device->handle);
4879                         sas_device->handle = handle;
4880                         starget = sas_device->starget;
4881                         sas_target_priv_data = starget->hostdata;
4882                         sas_target_priv_data->handle = handle;
4883                         goto out;
4884                 }
4885         }
4886  out:
4887         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4888 }
4889
4890 /**
4891  * _scsih_search_responding_sas_devices -
4892  * @ioc: per adapter object
4893  *
4894  * After host reset, find out whether devices are still responding.
4895  * If not remove.
4896  *
4897  * Return nothing.
4898  */
4899 static void
4900 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4901 {
4902         Mpi2SasDevicePage0_t sas_device_pg0;
4903         Mpi2ConfigReply_t mpi_reply;
4904         u16 ioc_status;
4905         __le64 sas_address;
4906         u16 handle;
4907         u32 device_info;
4908         u16 slot;
4909
4910         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4911
4912         if (list_empty(&ioc->sas_device_list))
4913                 return;
4914
4915         handle = 0xFFFF;
4916         while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4917             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4918             handle))) {
4919                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4920                     MPI2_IOCSTATUS_MASK;
4921                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4922                         break;
4923                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4924                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4925                 if (!(_scsih_is_end_device(device_info)))
4926                         continue;
4927                 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4928                 slot = le16_to_cpu(sas_device_pg0.Slot);
4929                 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4930                     handle);
4931         }
4932 }
4933
4934 /**
4935  * _scsih_mark_responding_raid_device - mark a raid_device as responding
4936  * @ioc: per adapter object
4937  * @wwid: world wide identifier for raid volume
4938  * @handle: device handle
4939  *
4940  * After host reset, find out whether devices are still responding.
4941  * Used in _scsi_remove_unresponsive_raid_devices.
4942  *
4943  * Return nothing.
4944  */
4945 static void
4946 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4947     u16 handle)
4948 {
4949         struct MPT2SAS_TARGET *sas_target_priv_data;
4950         struct scsi_target *starget;
4951         struct _raid_device *raid_device;
4952         unsigned long flags;
4953
4954         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4955         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4956                 if (raid_device->wwid == wwid && raid_device->starget) {
4957                         raid_device->responding = 1;
4958                         starget_printk(KERN_INFO, raid_device->starget,
4959                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
4960                             (unsigned long long)raid_device->wwid);
4961                         if (raid_device->handle == handle)
4962                                 goto out;
4963                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4964                             raid_device->handle);
4965                         raid_device->handle = handle;
4966                         starget = raid_device->starget;
4967                         sas_target_priv_data = starget->hostdata;
4968                         sas_target_priv_data->handle = handle;
4969                         goto out;
4970                 }
4971         }
4972  out:
4973         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4974 }
4975
4976 /**
4977  * _scsih_search_responding_raid_devices -
4978  * @ioc: per adapter object
4979  *
4980  * After host reset, find out whether devices are still responding.
4981  * If not remove.
4982  *
4983  * Return nothing.
4984  */
4985 static void
4986 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4987 {
4988         Mpi2RaidVolPage1_t volume_pg1;
4989         Mpi2ConfigReply_t mpi_reply;
4990         u16 ioc_status;
4991         u16 handle;
4992
4993         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4994
4995         if (list_empty(&ioc->raid_device_list))
4996                 return;
4997
4998         handle = 0xFFFF;
4999         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5000             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5001                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5002                     MPI2_IOCSTATUS_MASK;
5003                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5004                         break;
5005                 handle = le16_to_cpu(volume_pg1.DevHandle);
5006                 _scsih_mark_responding_raid_device(ioc,
5007                     le64_to_cpu(volume_pg1.WWID), handle);
5008         }
5009 }
5010
5011 /**
5012  * _scsih_mark_responding_expander - mark a expander as responding
5013  * @ioc: per adapter object
5014  * @sas_address: sas address
5015  * @handle:
5016  *
5017  * After host reset, find out whether devices are still responding.
5018  * Used in _scsi_remove_unresponsive_expanders.
5019  *
5020  * Return nothing.
5021  */
5022 static void
5023 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5024      u16 handle)
5025 {
5026         struct _sas_node *sas_expander;
5027         unsigned long flags;
5028
5029         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5030         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5031                 if (sas_expander->sas_address == sas_address) {
5032                         sas_expander->responding = 1;
5033                         if (sas_expander->handle != handle) {
5034                                 printk(KERN_INFO "old handle(0x%04x)\n",
5035                                     sas_expander->handle);
5036                                 sas_expander->handle = handle;
5037                         }
5038                         goto out;
5039                 }
5040         }
5041  out:
5042         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5043 }
5044
5045 /**
5046  * _scsih_search_responding_expanders -
5047  * @ioc: per adapter object
5048  *
5049  * After host reset, find out whether devices are still responding.
5050  * If not remove.
5051  *
5052  * Return nothing.
5053  */
5054 static void
5055 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5056 {
5057         Mpi2ExpanderPage0_t expander_pg0;
5058         Mpi2ConfigReply_t mpi_reply;
5059         u16 ioc_status;
5060         __le64 sas_address;
5061         u16 handle;
5062
5063         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5064
5065         if (list_empty(&ioc->sas_expander_list))
5066                 return;
5067
5068         handle = 0xFFFF;
5069         while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5070             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5071
5072                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5073                     MPI2_IOCSTATUS_MASK;
5074                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5075                         break;
5076
5077                 handle = le16_to_cpu(expander_pg0.DevHandle);
5078                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5079                 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5080                     "sas_addr(0x%016llx)\n", handle,
5081                     (unsigned long long)sas_address);
5082                 _scsih_mark_responding_expander(ioc, sas_address, handle);
5083         }
5084
5085 }
5086
5087 /**
5088  * _scsih_remove_unresponding_devices - removing unresponding devices
5089  * @ioc: per adapter object
5090  *
5091  * Return nothing.
5092  */
5093 static void
5094 _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5095 {
5096         struct _sas_device *sas_device, *sas_device_next;
5097         struct _sas_node *sas_expander;
5098         struct _raid_device *raid_device, *raid_device_next;
5099
5100
5101         list_for_each_entry_safe(sas_device, sas_device_next,
5102             &ioc->sas_device_list, list) {
5103                 if (sas_device->responding) {
5104                         sas_device->responding = 0;
5105                         continue;
5106                 }
5107                 if (sas_device->starget)
5108                         starget_printk(KERN_INFO, sas_device->starget,
5109                             "removing: handle(0x%04x), sas_addr(0x%016llx), "
5110                             "enclosure logical id(0x%016llx), slot(%d)\n",
5111                             sas_device->handle,
5112                             (unsigned long long)sas_device->sas_address,
5113                             (unsigned long long)
5114                             sas_device->enclosure_logical_id,
5115                             sas_device->slot);
5116                 _scsih_remove_device(ioc, sas_device->handle);
5117         }
5118
5119         list_for_each_entry_safe(raid_device, raid_device_next,
5120             &ioc->raid_device_list, list) {
5121                 if (raid_device->responding) {
5122                         raid_device->responding = 0;
5123                         continue;
5124                 }
5125                 if (raid_device->starget) {
5126                         starget_printk(KERN_INFO, raid_device->starget,
5127                             "removing: handle(0x%04x), wwid(0x%016llx)\n",
5128                               raid_device->handle,
5129                             (unsigned long long)raid_device->wwid);
5130                         scsi_remove_target(&raid_device->starget->dev);
5131                 }
5132                 _scsih_raid_device_remove(ioc, raid_device);
5133         }
5134
5135  retry_expander_search:
5136         sas_expander = NULL;
5137         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5138                 if (sas_expander->responding) {
5139                         sas_expander->responding = 0;
5140                         continue;
5141                 }
5142                 _scsih_expander_remove(ioc, sas_expander->handle);
5143                 goto retry_expander_search;
5144         }
5145 }
5146
5147 /**
5148  * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5149  * @ioc: per adapter object
5150  * @reset_phase: phase
5151  *
5152  * The handler for doing any required cleanup or initialization.
5153  *
5154  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5155  * MPT2_IOC_DONE_RESET
5156  *
5157  * Return nothing.
5158  */
5159 void
5160 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5161 {
5162         switch (reset_phase) {
5163         case MPT2_IOC_PRE_RESET:
5164                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5165                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5166                 _scsih_fw_event_off(ioc);
5167                 break;
5168         case MPT2_IOC_AFTER_RESET:
5169                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5170                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5171                 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5172                         ioc->tm_cmds.status |= MPT2_CMD_RESET;
5173                         mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5174                         complete(&ioc->tm_cmds.done);
5175                 }
5176                 _scsih_fw_event_on(ioc);
5177                 _scsih_flush_running_cmds(ioc);
5178                 break;
5179         case MPT2_IOC_DONE_RESET:
5180                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5181                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5182                 _scsih_sas_host_refresh(ioc, 0);
5183                 _scsih_search_responding_sas_devices(ioc);
5184                 _scsih_search_responding_raid_devices(ioc);
5185                 _scsih_search_responding_expanders(ioc);
5186                 break;
5187         case MPT2_IOC_RUNNING:
5188                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5189                     "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5190                 _scsih_remove_unresponding_devices(ioc);
5191                 break;
5192         }
5193 }
5194
5195 /**
5196  * _firmware_event_work - delayed task for processing firmware events
5197  * @ioc: per adapter object
5198  * @work: equal to the fw_event_work object
5199  * Context: user.
5200  *
5201  * Return nothing.
5202  */
5203 static void
5204 _firmware_event_work(struct work_struct *work)
5205 {
5206         struct fw_event_work *fw_event = container_of(work,
5207             struct fw_event_work, work);
5208         unsigned long flags;
5209         struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5210
5211         /* the queue is being flushed so ignore this event */
5212         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5213         if (ioc->fw_events_off || ioc->remove_host) {
5214                 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5215                 _scsih_fw_event_free(ioc, fw_event);
5216                 return;
5217         }
5218         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5219
5220         spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5221         if (ioc->shost_recovery) {
5222                 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5223                 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5224                 return;
5225         }
5226         spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5227
5228         switch (fw_event->event) {
5229         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5230                 _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
5231                     fw_event->event_data, fw_event);
5232                 break;
5233         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5234                 _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
5235                     fw_event->event_data);
5236                 break;
5237         case MPI2_EVENT_SAS_DISCOVERY:
5238                 _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
5239                     fw_event->event_data);
5240                 break;
5241         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5242                 _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
5243                     fw_event->event_data);
5244                 break;
5245         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5246                 _scsih_sas_enclosure_dev_status_change_event(ioc,
5247                     fw_event->VF_ID, fw_event->event_data);
5248                 break;
5249         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5250                 _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
5251                     fw_event->event_data);
5252                 break;
5253         case MPI2_EVENT_IR_VOLUME:
5254                 _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
5255                     fw_event->event_data);
5256                 break;
5257         case MPI2_EVENT_IR_PHYSICAL_DISK:
5258                 _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
5259                     fw_event->event_data);
5260                 break;
5261         case MPI2_EVENT_IR_OPERATION_STATUS:
5262                 _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
5263                     fw_event->event_data);
5264                 break;
5265         case MPI2_EVENT_TASK_SET_FULL:
5266                 _scsih_task_set_full(ioc, fw_event->VF_ID,
5267                     fw_event->event_data);
5268                 break;
5269         }
5270         _scsih_fw_event_free(ioc, fw_event);
5271 }
5272
5273 /**
5274  * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5275  * @ioc: per adapter object
5276  * @VF_ID: virtual function id
5277  * @reply: reply message frame(lower 32bit addr)
5278  * Context: interrupt.
5279  *
5280  * This function merely adds a new work task into ioc->firmware_event_thread.
5281  * The tasks are worked from _firmware_event_work in user context.
5282  *
5283  * Return nothing.
5284  */
5285 void
5286 mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
5287 {
5288         struct fw_event_work *fw_event;
5289         Mpi2EventNotificationReply_t *mpi_reply;
5290         unsigned long flags;
5291         u16 event;
5292
5293         /* events turned off due to host reset or driver unloading */
5294         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5295         if (ioc->fw_events_off || ioc->remove_host) {
5296                 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5297                 return;
5298         }
5299         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5300
5301         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
5302         event = le16_to_cpu(mpi_reply->Event);
5303
5304         switch (event) {
5305         /* handle these */
5306         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5307         {
5308                 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5309                     (Mpi2EventDataSasBroadcastPrimitive_t *)
5310                     mpi_reply->EventData;
5311
5312                 if (baen_data->Primitive !=
5313                     MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5314                     ioc->broadcast_aen_busy)
5315                         return;
5316                 ioc->broadcast_aen_busy = 1;
5317                 break;
5318         }
5319
5320         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5321                 _scsih_check_topo_delete_events(ioc,
5322                     (Mpi2EventDataSasTopologyChangeList_t *)
5323                     mpi_reply->EventData);
5324                 break;
5325
5326         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5327         case MPI2_EVENT_IR_OPERATION_STATUS:
5328         case MPI2_EVENT_SAS_DISCOVERY:
5329         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5330         case MPI2_EVENT_IR_VOLUME:
5331         case MPI2_EVENT_IR_PHYSICAL_DISK:
5332         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5333         case MPI2_EVENT_TASK_SET_FULL:
5334                 break;
5335
5336         default: /* ignore the rest */
5337                 return;
5338         }
5339
5340         fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5341         if (!fw_event) {
5342                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5343                     ioc->name, __FILE__, __LINE__, __func__);
5344                 return;
5345         }
5346         fw_event->event_data =
5347             kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5348         if (!fw_event->event_data) {
5349                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5350                     ioc->name, __FILE__, __LINE__, __func__);
5351                 kfree(fw_event);
5352                 return;
5353         }
5354
5355         memcpy(fw_event->event_data, mpi_reply->EventData,
5356             mpi_reply->EventDataLength*4);
5357         fw_event->ioc = ioc;
5358         fw_event->VF_ID = VF_ID;
5359         fw_event->event = event;
5360         _scsih_fw_event_add(ioc, fw_event);
5361 }
5362
5363 /* shost template */
5364 static struct scsi_host_template scsih_driver_template = {
5365         .module                         = THIS_MODULE,
5366         .name                           = "Fusion MPT SAS Host",
5367         .proc_name                      = MPT2SAS_DRIVER_NAME,
5368         .queuecommand                   = _scsih_qcmd,
5369         .target_alloc                   = _scsih_target_alloc,
5370         .slave_alloc                    = _scsih_slave_alloc,
5371         .slave_configure                = _scsih_slave_configure,
5372         .target_destroy                 = _scsih_target_destroy,
5373         .slave_destroy                  = _scsih_slave_destroy,
5374         .change_queue_depth             = _scsih_change_queue_depth,
5375         .change_queue_type              = _scsih_change_queue_type,
5376         .eh_abort_handler               = _scsih_abort,
5377         .eh_device_reset_handler        = _scsih_dev_reset,
5378         .eh_target_reset_handler        = _scsih_target_reset,
5379         .eh_host_reset_handler          = _scsih_host_reset,
5380         .bios_param                     = _scsih_bios_param,
5381         .can_queue                      = 1,
5382         .this_id                        = -1,
5383         .sg_tablesize                   = MPT2SAS_SG_DEPTH,
5384         .max_sectors                    = 8192,
5385         .cmd_per_lun                    = 7,
5386         .use_clustering                 = ENABLE_CLUSTERING,
5387         .shost_attrs                    = mpt2sas_host_attrs,
5388         .sdev_attrs                     = mpt2sas_dev_attrs,
5389 };
5390
5391 /**
5392  * _scsih_expander_node_remove - removing expander device from list.
5393  * @ioc: per adapter object
5394  * @sas_expander: the sas_device object
5395  * Context: Calling function should acquire ioc->sas_node_lock.
5396  *
5397  * Removing object and freeing associated memory from the
5398  * ioc->sas_expander_list.
5399  *
5400  * Return nothing.
5401  */
5402 static void
5403 _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5404     struct _sas_node *sas_expander)
5405 {
5406         struct _sas_port *mpt2sas_port;
5407         struct _sas_device *sas_device;
5408         struct _sas_node *expander_sibling;
5409         unsigned long flags;
5410
5411         if (!sas_expander)
5412                 return;
5413
5414         /* remove sibling ports attached to this expander */
5415  retry_device_search:
5416         list_for_each_entry(mpt2sas_port,
5417            &sas_expander->sas_port_list, port_list) {
5418                 if (mpt2sas_port->remote_identify.device_type ==
5419                     SAS_END_DEVICE) {
5420                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5421                         sas_device =
5422                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5423                            mpt2sas_port->remote_identify.sas_address);
5424                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5425                         if (!sas_device)
5426                                 continue;
5427                         _scsih_remove_device(ioc, sas_device->handle);
5428                         goto retry_device_search;
5429                 }
5430         }
5431
5432  retry_expander_search:
5433         list_for_each_entry(mpt2sas_port,
5434            &sas_expander->sas_port_list, port_list) {
5435
5436                 if (mpt2sas_port->remote_identify.device_type ==
5437                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5438                     mpt2sas_port->remote_identify.device_type ==
5439                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5440
5441                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5442                         expander_sibling =
5443                             mpt2sas_scsih_expander_find_by_sas_address(
5444                             ioc, mpt2sas_port->remote_identify.sas_address);
5445                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5446                         if (!expander_sibling)
5447                                 continue;
5448                         _scsih_expander_remove(ioc, expander_sibling->handle);
5449                         goto retry_expander_search;
5450                 }
5451         }
5452
5453         mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5454             sas_expander->parent_handle);
5455
5456         printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5457            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5458             sas_expander->handle, (unsigned long long)
5459             sas_expander->sas_address);
5460
5461         list_del(&sas_expander->list);
5462         kfree(sas_expander->phy);
5463         kfree(sas_expander);
5464 }
5465
5466 /**
5467  * _scsih_remove - detach and remove add host
5468  * @pdev: PCI device struct
5469  *
5470  * Return nothing.
5471  */
5472 static void __devexit
5473 _scsih_remove(struct pci_dev *pdev)
5474 {
5475         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5476         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5477         struct _sas_port *mpt2sas_port;
5478         struct _sas_device *sas_device;
5479         struct _sas_node *expander_sibling;
5480         struct workqueue_struct *wq;
5481         unsigned long flags;
5482
5483         ioc->remove_host = 1;
5484         _scsih_fw_event_off(ioc);
5485
5486         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5487         wq = ioc->firmware_event_thread;
5488         ioc->firmware_event_thread = NULL;
5489         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5490         if (wq)
5491                 destroy_workqueue(wq);
5492
5493         /* free ports attached to the sas_host */
5494  retry_again:
5495         list_for_each_entry(mpt2sas_port,
5496            &ioc->sas_hba.sas_port_list, port_list) {
5497                 if (mpt2sas_port->remote_identify.device_type ==
5498                     SAS_END_DEVICE) {
5499                         sas_device =
5500                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5501                            mpt2sas_port->remote_identify.sas_address);
5502                         if (sas_device) {
5503                                 _scsih_remove_device(ioc, sas_device->handle);
5504                                 goto retry_again;
5505                         }
5506                 } else {
5507                         expander_sibling =
5508                             mpt2sas_scsih_expander_find_by_sas_address(ioc,
5509                             mpt2sas_port->remote_identify.sas_address);
5510                         if (expander_sibling) {
5511                                 _scsih_expander_remove(ioc,
5512                                     expander_sibling->handle);
5513                                 goto retry_again;
5514                         }
5515                 }
5516         }
5517
5518         /* free phys attached to the sas_host */
5519         if (ioc->sas_hba.num_phys) {
5520                 kfree(ioc->sas_hba.phy);
5521                 ioc->sas_hba.phy = NULL;
5522                 ioc->sas_hba.num_phys = 0;
5523         }
5524
5525         sas_remove_host(shost);
5526         mpt2sas_base_detach(ioc);
5527         list_del(&ioc->list);
5528         scsi_remove_host(shost);
5529         scsi_host_put(shost);
5530 }
5531
5532 /**
5533  * _scsih_probe_boot_devices - reports 1st device
5534  * @ioc: per adapter object
5535  *
5536  * If specified in bios page 2, this routine reports the 1st
5537  * device scsi-ml or sas transport for persistent boot device
5538  * purposes.  Please refer to function _scsih_determine_boot_device()
5539  */
5540 static void
5541 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5542 {
5543         u8 is_raid;
5544         void *device;
5545         struct _sas_device *sas_device;
5546         struct _raid_device *raid_device;
5547         u16 handle, parent_handle;
5548         u64 sas_address;
5549         unsigned long flags;
5550         int rc;
5551
5552         device = NULL;
5553         if (ioc->req_boot_device.device) {
5554                 device =  ioc->req_boot_device.device;
5555                 is_raid = ioc->req_boot_device.is_raid;
5556         } else if (ioc->req_alt_boot_device.device) {
5557                 device =  ioc->req_alt_boot_device.device;
5558                 is_raid = ioc->req_alt_boot_device.is_raid;
5559         } else if (ioc->current_boot_device.device) {
5560                 device =  ioc->current_boot_device.device;
5561                 is_raid = ioc->current_boot_device.is_raid;
5562         }
5563
5564         if (!device)
5565                 return;
5566
5567         if (is_raid) {
5568                 raid_device = device;
5569                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5570                     raid_device->id, 0);
5571                 if (rc)
5572                         _scsih_raid_device_remove(ioc, raid_device);
5573         } else {
5574                 sas_device = device;
5575                 handle = sas_device->handle;
5576                 parent_handle = sas_device->parent_handle;
5577                 sas_address = sas_device->sas_address;
5578                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5579                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5580                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5581                 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5582                     sas_device->parent_handle)) {
5583                         _scsih_sas_device_remove(ioc, sas_device);
5584                 } else if (!sas_device->starget) {
5585                         mpt2sas_transport_port_remove(ioc, sas_address,
5586                             parent_handle);
5587                         _scsih_sas_device_remove(ioc, sas_device);
5588                 }
5589         }
5590 }
5591
5592 /**
5593  * _scsih_probe_raid - reporting raid volumes to scsi-ml
5594  * @ioc: per adapter object
5595  *
5596  * Called during initial loading of the driver.
5597  */
5598 static void
5599 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5600 {
5601         struct _raid_device *raid_device, *raid_next;
5602         int rc;
5603
5604         list_for_each_entry_safe(raid_device, raid_next,
5605             &ioc->raid_device_list, list) {
5606                 if (raid_device->starget)
5607                         continue;
5608                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5609                     raid_device->id, 0);
5610                 if (rc)
5611                         _scsih_raid_device_remove(ioc, raid_device);
5612         }
5613 }
5614
5615 /**
5616  * _scsih_probe_sas - reporting raid volumes to sas transport
5617  * @ioc: per adapter object
5618  *
5619  * Called during initial loading of the driver.
5620  */
5621 static void
5622 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5623 {
5624         struct _sas_device *sas_device, *next;
5625         unsigned long flags;
5626         u16 handle, parent_handle;
5627         u64 sas_address;
5628
5629         /* SAS Device List */
5630         list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5631             list) {
5632                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5633                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5634                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5635
5636                 handle = sas_device->handle;
5637                 parent_handle = sas_device->parent_handle;
5638                 sas_address = sas_device->sas_address;
5639                 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5640                         _scsih_sas_device_remove(ioc, sas_device);
5641                 } else if (!sas_device->starget) {
5642                         mpt2sas_transport_port_remove(ioc, sas_address,
5643                             parent_handle);
5644                         _scsih_sas_device_remove(ioc, sas_device);
5645                 }
5646         }
5647 }
5648
5649 /**
5650  * _scsih_probe_devices - probing for devices
5651  * @ioc: per adapter object
5652  *
5653  * Called during initial loading of the driver.
5654  */
5655 static void
5656 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5657 {
5658         u16 volume_mapping_flags =
5659             le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5660             MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5661
5662         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5663                 return;  /* return when IOC doesn't support initiator mode */
5664
5665         _scsih_probe_boot_devices(ioc);
5666
5667         if (ioc->ir_firmware) {
5668                 if ((volume_mapping_flags &
5669                      MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5670                         _scsih_probe_sas(ioc);
5671                         _scsih_probe_raid(ioc);
5672                 } else {
5673                         _scsih_probe_raid(ioc);
5674                         _scsih_probe_sas(ioc);
5675                 }
5676         } else
5677                 _scsih_probe_sas(ioc);
5678 }
5679
5680 /**
5681  * _scsih_probe - attach and add scsi host
5682  * @pdev: PCI device struct
5683  * @id: pci device id
5684  *
5685  * Returns 0 success, anything else error.
5686  */
5687 static int
5688 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5689 {
5690         struct MPT2SAS_ADAPTER *ioc;
5691         struct Scsi_Host *shost;
5692
5693         shost = scsi_host_alloc(&scsih_driver_template,
5694             sizeof(struct MPT2SAS_ADAPTER));
5695         if (!shost)
5696                 return -ENODEV;
5697
5698         /* init local params */
5699         ioc = shost_priv(shost);
5700         memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5701         INIT_LIST_HEAD(&ioc->list);
5702         list_add_tail(&ioc->list, &mpt2sas_ioc_list);
5703         ioc->shost = shost;
5704         ioc->id = mpt_ids++;
5705         sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5706         ioc->pdev = pdev;
5707         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5708         ioc->tm_cb_idx = tm_cb_idx;
5709         ioc->ctl_cb_idx = ctl_cb_idx;
5710         ioc->base_cb_idx = base_cb_idx;
5711         ioc->transport_cb_idx = transport_cb_idx;
5712         ioc->config_cb_idx = config_cb_idx;
5713         ioc->logging_level = logging_level;
5714         /* misc semaphores and spin locks */
5715         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5716         spin_lock_init(&ioc->scsi_lookup_lock);
5717         spin_lock_init(&ioc->sas_device_lock);
5718         spin_lock_init(&ioc->sas_node_lock);
5719         spin_lock_init(&ioc->fw_event_lock);
5720         spin_lock_init(&ioc->raid_device_lock);
5721
5722         INIT_LIST_HEAD(&ioc->sas_device_list);
5723         INIT_LIST_HEAD(&ioc->sas_device_init_list);
5724         INIT_LIST_HEAD(&ioc->sas_expander_list);
5725         INIT_LIST_HEAD(&ioc->fw_event_list);
5726         INIT_LIST_HEAD(&ioc->raid_device_list);
5727         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5728
5729         /* init shost parameters */
5730         shost->max_cmd_len = 16;
5731         shost->max_lun = max_lun;
5732         shost->transportt = mpt2sas_transport_template;
5733         shost->unique_id = ioc->id;
5734
5735         if ((scsi_add_host(shost, &pdev->dev))) {
5736                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5737                     ioc->name, __FILE__, __LINE__, __func__);
5738                 list_del(&ioc->list);
5739                 goto out_add_shost_fail;
5740         }
5741
5742         scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5743             | SHOST_DIF_TYPE3_PROTECTION);
5744
5745         /* event thread */
5746         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5747             "fw_event%d", ioc->id);
5748         ioc->firmware_event_thread = create_singlethread_workqueue(
5749             ioc->firmware_event_name);
5750         if (!ioc->firmware_event_thread) {
5751                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5752                     ioc->name, __FILE__, __LINE__, __func__);
5753                 goto out_thread_fail;
5754         }
5755
5756         ioc->wait_for_port_enable_to_complete = 1;
5757         if ((mpt2sas_base_attach(ioc))) {
5758                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5759                     ioc->name, __FILE__, __LINE__, __func__);
5760                 goto out_attach_fail;
5761         }
5762
5763         ioc->wait_for_port_enable_to_complete = 0;
5764         _scsih_probe_devices(ioc);
5765         return 0;
5766
5767  out_attach_fail:
5768         destroy_workqueue(ioc->firmware_event_thread);
5769  out_thread_fail:
5770         list_del(&ioc->list);
5771         scsi_remove_host(shost);
5772  out_add_shost_fail:
5773         return -ENODEV;
5774 }
5775
5776 #ifdef CONFIG_PM
5777 /**
5778  * _scsih_suspend - power management suspend main entry point
5779  * @pdev: PCI device struct
5780  * @state: PM state change to (usually PCI_D3)
5781  *
5782  * Returns 0 success, anything else error.
5783  */
5784 static int
5785 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
5786 {
5787         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5788         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5789         u32 device_state;
5790
5791         mpt2sas_base_stop_watchdog(ioc);
5792         flush_scheduled_work();
5793         scsi_block_requests(shost);
5794         device_state = pci_choose_state(pdev, state);
5795         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5796             "operating state [D%d]\n", ioc->name, pdev,
5797             pci_name(pdev), device_state);
5798
5799         mpt2sas_base_free_resources(ioc);
5800         pci_save_state(pdev);
5801         pci_disable_device(pdev);
5802         pci_set_power_state(pdev, device_state);
5803         return 0;
5804 }
5805
5806 /**
5807  * _scsih_resume - power management resume main entry point
5808  * @pdev: PCI device struct
5809  *
5810  * Returns 0 success, anything else error.
5811  */
5812 static int
5813 _scsih_resume(struct pci_dev *pdev)
5814 {
5815         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5816         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5817         u32 device_state = pdev->current_state;
5818         int r;
5819
5820         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5821             "operating state [D%d]\n", ioc->name, pdev,
5822             pci_name(pdev), device_state);
5823
5824         pci_set_power_state(pdev, PCI_D0);
5825         pci_enable_wake(pdev, PCI_D0, 0);
5826         pci_restore_state(pdev);
5827         ioc->pdev = pdev;
5828         r = mpt2sas_base_map_resources(ioc);
5829         if (r)
5830                 return r;
5831
5832         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5833         scsi_unblock_requests(shost);
5834         mpt2sas_base_start_watchdog(ioc);
5835         return 0;
5836 }
5837 #endif /* CONFIG_PM */
5838
5839
5840 static struct pci_driver scsih_driver = {
5841         .name           = MPT2SAS_DRIVER_NAME,
5842         .id_table       = scsih_pci_table,
5843         .probe          = _scsih_probe,
5844         .remove         = __devexit_p(_scsih_remove),
5845 #ifdef CONFIG_PM
5846         .suspend        = _scsih_suspend,
5847         .resume         = _scsih_resume,
5848 #endif
5849 };
5850
5851
5852 /**
5853  * _scsih_init - main entry point for this driver.
5854  *
5855  * Returns 0 success, anything else error.
5856  */
5857 static int __init
5858 _scsih_init(void)
5859 {
5860         int error;
5861
5862         mpt_ids = 0;
5863         printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5864             MPT2SAS_DRIVER_VERSION);
5865
5866         mpt2sas_transport_template =
5867             sas_attach_transport(&mpt2sas_transport_functions);
5868         if (!mpt2sas_transport_template)
5869                 return -ENODEV;
5870
5871         mpt2sas_base_initialize_callback_handler();
5872
5873          /* queuecommand callback hander */
5874         scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
5875
5876         /* task managment callback handler */
5877         tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
5878
5879         /* base internal commands callback handler */
5880         base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5881
5882         /* transport internal commands callback handler */
5883         transport_cb_idx = mpt2sas_base_register_callback_handler(
5884             mpt2sas_transport_done);
5885
5886         /* configuration page API internal commands callback handler */
5887         config_cb_idx = mpt2sas_base_register_callback_handler(
5888             mpt2sas_config_done);
5889
5890         /* ctl module callback handler */
5891         ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5892
5893         mpt2sas_ctl_init();
5894
5895         error = pci_register_driver(&scsih_driver);
5896         if (error)
5897                 sas_release_transport(mpt2sas_transport_template);
5898
5899         return error;
5900 }
5901
5902 /**
5903  * _scsih_exit - exit point for this driver (when it is a module).
5904  *
5905  * Returns 0 success, anything else error.
5906  */
5907 static void __exit
5908 _scsih_exit(void)
5909 {
5910         printk(KERN_INFO "mpt2sas version %s unloading\n",
5911             MPT2SAS_DRIVER_VERSION);
5912
5913         pci_unregister_driver(&scsih_driver);
5914
5915         sas_release_transport(mpt2sas_transport_template);
5916         mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5917         mpt2sas_base_release_callback_handler(tm_cb_idx);
5918         mpt2sas_base_release_callback_handler(base_cb_idx);
5919         mpt2sas_base_release_callback_handler(transport_cb_idx);
5920         mpt2sas_base_release_callback_handler(config_cb_idx);
5921         mpt2sas_base_release_callback_handler(ctl_cb_idx);
5922
5923         mpt2sas_ctl_exit();
5924 }
5925
5926 module_init(_scsih_init);
5927 module_exit(_scsih_exit);