]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/mpt2sas/mpt2sas_scsih.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[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                         if (ioc->manu_pg10.OEMIdentifier &&
1505                             (ioc->manu_pg10.GenericFlags0 &
1506                             MFG10_GF0_R10_DISPLAY) &&
1507                             !(raid_device->num_pds % 2))
1508                                 r_level = "RAID10";
1509                         else
1510                                 r_level = "RAID1E";
1511                         break;
1512                 case MPI2_RAID_VOL_TYPE_RAID1:
1513                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1514                         r_level = "RAID1";
1515                         break;
1516                 case MPI2_RAID_VOL_TYPE_RAID10:
1517                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1518                         r_level = "RAID10";
1519                         break;
1520                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1521                 default:
1522                         qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1523                         r_level = "RAIDX";
1524                         break;
1525                 }
1526
1527                 sdev_printk(KERN_INFO, sdev, "%s: "
1528                     "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1529                     r_level, raid_device->handle,
1530                     (unsigned long long)raid_device->wwid,
1531                     raid_device->num_pds, ds);
1532                 _scsih_change_queue_depth(sdev, qdepth);
1533                 return 0;
1534         }
1535
1536         /* non-raid handling */
1537         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1538         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1539            sas_device_priv_data->sas_target->sas_address);
1540         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1541         if (sas_device) {
1542                 if (sas_target_priv_data->flags &
1543                     MPT_TARGET_FLAGS_RAID_COMPONENT) {
1544                         mpt2sas_config_get_volume_handle(ioc,
1545                             sas_device->handle, &sas_device->volume_handle);
1546                         mpt2sas_config_get_volume_wwid(ioc,
1547                             sas_device->volume_handle,
1548                             &sas_device->volume_wwid);
1549                 }
1550                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1551                         qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1552                         ssp_target = 1;
1553                         ds = "SSP";
1554                 } else {
1555                         qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1556                         if (sas_device->device_info &
1557                             MPI2_SAS_DEVICE_INFO_STP_TARGET)
1558                                 ds = "STP";
1559                         else if (sas_device->device_info &
1560                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1561                                 ds = "SATA";
1562                 }
1563
1564                 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1565                     "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1566                     ds, sas_device->handle,
1567                     (unsigned long long)sas_device->sas_address,
1568                     (unsigned long long)sas_device->device_name);
1569                 sdev_printk(KERN_INFO, sdev, "%s: "
1570                     "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1571                     (unsigned long long) sas_device->enclosure_logical_id,
1572                     sas_device->slot);
1573
1574                 if (!ssp_target)
1575                         _scsih_display_sata_capabilities(ioc, sas_device, sdev);
1576         }
1577
1578         _scsih_change_queue_depth(sdev, qdepth);
1579
1580         if (ssp_target)
1581                 sas_read_port_mode_page(sdev);
1582         return 0;
1583 }
1584
1585 /**
1586  * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1587  * @sdev: scsi device struct
1588  * @bdev: pointer to block device context
1589  * @capacity: device size (in 512 byte sectors)
1590  * @params: three element array to place output:
1591  *              params[0] number of heads (max 255)
1592  *              params[1] number of sectors (max 63)
1593  *              params[2] number of cylinders
1594  *
1595  * Return nothing.
1596  */
1597 static int
1598 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1599     sector_t capacity, int params[])
1600 {
1601         int             heads;
1602         int             sectors;
1603         sector_t        cylinders;
1604         ulong           dummy;
1605
1606         heads = 64;
1607         sectors = 32;
1608
1609         dummy = heads * sectors;
1610         cylinders = capacity;
1611         sector_div(cylinders, dummy);
1612
1613         /*
1614          * Handle extended translation size for logical drives
1615          * > 1Gb
1616          */
1617         if ((ulong)capacity >= 0x200000) {
1618                 heads = 255;
1619                 sectors = 63;
1620                 dummy = heads * sectors;
1621                 cylinders = capacity;
1622                 sector_div(cylinders, dummy);
1623         }
1624
1625         /* return result */
1626         params[0] = heads;
1627         params[1] = sectors;
1628         params[2] = cylinders;
1629
1630         return 0;
1631 }
1632
1633 /**
1634  * _scsih_response_code - translation of device response code
1635  * @ioc: per adapter object
1636  * @response_code: response code returned by the device
1637  *
1638  * Return nothing.
1639  */
1640 static void
1641 _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1642 {
1643         char *desc;
1644
1645         switch (response_code) {
1646         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1647                 desc = "task management request completed";
1648                 break;
1649         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1650                 desc = "invalid frame";
1651                 break;
1652         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1653                 desc = "task management request not supported";
1654                 break;
1655         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1656                 desc = "task management request failed";
1657                 break;
1658         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1659                 desc = "task management request succeeded";
1660                 break;
1661         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1662                 desc = "invalid lun";
1663                 break;
1664         case 0xA:
1665                 desc = "overlapped tag attempted";
1666                 break;
1667         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1668                 desc = "task queued, however not sent to target";
1669                 break;
1670         default:
1671                 desc = "unknown";
1672                 break;
1673         }
1674         printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1675                 ioc->name, response_code, desc);
1676 }
1677
1678 /**
1679  * _scsih_tm_done - tm completion routine
1680  * @ioc: per adapter object
1681  * @smid: system request message index
1682  * @VF_ID: virtual function id
1683  * @reply: reply message frame(lower 32bit addr)
1684  * Context: none.
1685  *
1686  * The callback handler when using scsih_issue_tm.
1687  *
1688  * Return nothing.
1689  */
1690 static void
1691 _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
1692 {
1693         MPI2DefaultReply_t *mpi_reply;
1694
1695         if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1696                 return;
1697         if (ioc->tm_cmds.smid != smid)
1698                 return;
1699         ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1700         mpi_reply =  mpt2sas_base_get_reply_virt_addr(ioc, reply);
1701         if (mpi_reply) {
1702                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1703                 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1704         }
1705         ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1706         complete(&ioc->tm_cmds.done);
1707 }
1708
1709 /**
1710  * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1711  * @ioc: per adapter object
1712  * @handle: device handle
1713  *
1714  * During taskmangement request, we need to freeze the device queue.
1715  */
1716 void
1717 mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1718 {
1719         struct MPT2SAS_DEVICE *sas_device_priv_data;
1720         struct scsi_device *sdev;
1721         u8 skip = 0;
1722
1723         shost_for_each_device(sdev, ioc->shost) {
1724                 if (skip)
1725                         continue;
1726                 sas_device_priv_data = sdev->hostdata;
1727                 if (!sas_device_priv_data)
1728                         continue;
1729                 if (sas_device_priv_data->sas_target->handle == handle) {
1730                         sas_device_priv_data->sas_target->tm_busy = 1;
1731                         skip = 1;
1732                         ioc->ignore_loginfos = 1;
1733                 }
1734         }
1735 }
1736
1737 /**
1738  * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1739  * @ioc: per adapter object
1740  * @handle: device handle
1741  *
1742  * During taskmangement request, we need to freeze the device queue.
1743  */
1744 void
1745 mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1746 {
1747         struct MPT2SAS_DEVICE *sas_device_priv_data;
1748         struct scsi_device *sdev;
1749         u8 skip = 0;
1750
1751         shost_for_each_device(sdev, ioc->shost) {
1752                 if (skip)
1753                         continue;
1754                 sas_device_priv_data = sdev->hostdata;
1755                 if (!sas_device_priv_data)
1756                         continue;
1757                 if (sas_device_priv_data->sas_target->handle == handle) {
1758                         sas_device_priv_data->sas_target->tm_busy = 0;
1759                         skip = 1;
1760                         ioc->ignore_loginfos = 0;
1761                 }
1762         }
1763 }
1764
1765 /**
1766  * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1767  * @ioc: per adapter struct
1768  * @device_handle: device handle
1769  * @lun: lun number
1770  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1771  * @smid_task: smid assigned to the task
1772  * @timeout: timeout in seconds
1773  * Context: The calling function needs to acquire the tm_cmds.mutex
1774  *
1775  * A generic API for sending task management requests to firmware.
1776  *
1777  * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1778  * this API.
1779  *
1780  * The callback index is set inside `ioc->tm_cb_idx`.
1781  *
1782  * Return nothing.
1783  */
1784 void
1785 mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1786     u8 type, u16 smid_task, ulong timeout)
1787 {
1788         Mpi2SCSITaskManagementRequest_t *mpi_request;
1789         Mpi2SCSITaskManagementReply_t *mpi_reply;
1790         u16 smid = 0;
1791         u32 ioc_state;
1792         unsigned long timeleft;
1793         u8 VF_ID = 0;
1794
1795         if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1796                 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1797                     __func__, ioc->name);
1798                 return;
1799         }
1800
1801         if (ioc->shost_recovery) {
1802                 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1803                     __func__, ioc->name);
1804                 return;
1805         }
1806
1807         ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1808         if (ioc_state & MPI2_DOORBELL_USED) {
1809                 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1810                     "active!\n", ioc->name));
1811                 goto issue_host_reset;
1812         }
1813
1814         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1815                 mpt2sas_base_fault_info(ioc, ioc_state &
1816                     MPI2_DOORBELL_DATA_MASK);
1817                 goto issue_host_reset;
1818         }
1819
1820         smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1821         if (!smid) {
1822                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1823                     ioc->name, __func__);
1824                 return;
1825         }
1826
1827         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1828             " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1829         ioc->tm_cmds.status = MPT2_CMD_PENDING;
1830         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1831         ioc->tm_cmds.smid = smid;
1832         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1833         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1834         mpi_request->DevHandle = cpu_to_le16(handle);
1835         mpi_request->TaskType = type;
1836         mpi_request->TaskMID = cpu_to_le16(smid_task);
1837         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1838         mpt2sas_scsih_set_tm_flag(ioc, handle);
1839         init_completion(&ioc->tm_cmds.done);
1840         mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1841         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1842         mpt2sas_scsih_clear_tm_flag(ioc, handle);
1843         if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1844                 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1845                     ioc->name, __func__);
1846                 _debug_dump_mf(mpi_request,
1847                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1848                 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1849                         goto issue_host_reset;
1850         }
1851
1852         if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1853                 mpi_reply = ioc->tm_cmds.reply;
1854                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1855                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1856                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1857                     le32_to_cpu(mpi_reply->IOCLogInfo),
1858                     le32_to_cpu(mpi_reply->TerminationCount)));
1859                 if (ioc->logging_level & MPT_DEBUG_TM)
1860                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
1861         }
1862         return;
1863  issue_host_reset:
1864         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1865 }
1866
1867 /**
1868  * _scsih_abort - eh threads main abort routine
1869  * @sdev: scsi device struct
1870  *
1871  * Returns SUCCESS if command aborted else FAILED
1872  */
1873 static int
1874 _scsih_abort(struct scsi_cmnd *scmd)
1875 {
1876         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1877         struct MPT2SAS_DEVICE *sas_device_priv_data;
1878         u16 smid;
1879         u16 handle;
1880         int r;
1881         struct scsi_cmnd *scmd_lookup;
1882
1883         printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1884             ioc->name, scmd);
1885         scsi_print_command(scmd);
1886
1887         sas_device_priv_data = scmd->device->hostdata;
1888         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1889                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1890                     ioc->name, scmd);
1891                 scmd->result = DID_NO_CONNECT << 16;
1892                 scmd->scsi_done(scmd);
1893                 r = SUCCESS;
1894                 goto out;
1895         }
1896
1897         /* search for the command */
1898         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1899         if (!smid) {
1900                 scmd->result = DID_RESET << 16;
1901                 r = SUCCESS;
1902                 goto out;
1903         }
1904
1905         /* for hidden raid components and volumes this is not supported */
1906         if (sas_device_priv_data->sas_target->flags &
1907             MPT_TARGET_FLAGS_RAID_COMPONENT ||
1908             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1909                 scmd->result = DID_RESET << 16;
1910                 r = FAILED;
1911                 goto out;
1912         }
1913
1914         mutex_lock(&ioc->tm_cmds.mutex);
1915         handle = sas_device_priv_data->sas_target->handle;
1916         mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1917             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1918
1919         /* sanity check - see whether command actually completed */
1920         scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1921         if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1922                 r = FAILED;
1923         else
1924                 r = SUCCESS;
1925         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1926         mutex_unlock(&ioc->tm_cmds.mutex);
1927
1928  out:
1929         printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1930             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1931         return r;
1932 }
1933
1934 /**
1935  * _scsih_dev_reset - eh threads main device reset routine
1936  * @sdev: scsi device struct
1937  *
1938  * Returns SUCCESS if command aborted else FAILED
1939  */
1940 static int
1941 _scsih_dev_reset(struct scsi_cmnd *scmd)
1942 {
1943         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1944         struct MPT2SAS_DEVICE *sas_device_priv_data;
1945         struct _sas_device *sas_device;
1946         unsigned long flags;
1947         u16     handle;
1948         int r;
1949
1950         printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
1951             ioc->name, scmd);
1952         scsi_print_command(scmd);
1953
1954         sas_device_priv_data = scmd->device->hostdata;
1955         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1956                 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1957                     ioc->name, scmd);
1958                 scmd->result = DID_NO_CONNECT << 16;
1959                 scmd->scsi_done(scmd);
1960                 r = SUCCESS;
1961                 goto out;
1962         }
1963
1964         /* for hidden raid components obtain the volume_handle */
1965         handle = 0;
1966         if (sas_device_priv_data->sas_target->flags &
1967             MPT_TARGET_FLAGS_RAID_COMPONENT) {
1968                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1969                 sas_device = _scsih_sas_device_find_by_handle(ioc,
1970                    sas_device_priv_data->sas_target->handle);
1971                 if (sas_device)
1972                         handle = sas_device->volume_handle;
1973                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1974         } else
1975                 handle = sas_device_priv_data->sas_target->handle;
1976
1977         if (!handle) {
1978                 scmd->result = DID_RESET << 16;
1979                 r = FAILED;
1980                 goto out;
1981         }
1982
1983         mutex_lock(&ioc->tm_cmds.mutex);
1984         mpt2sas_scsih_issue_tm(ioc, handle, 0,
1985             MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
1986             30);
1987
1988         /*
1989          *  sanity check see whether all commands to this device been
1990          *  completed
1991          */
1992         if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
1993             scmd->device->lun, scmd->device->channel))
1994                 r = FAILED;
1995         else
1996                 r = SUCCESS;
1997         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1998         mutex_unlock(&ioc->tm_cmds.mutex);
1999
2000  out:
2001         printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2002             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2003         return r;
2004 }
2005
2006 /**
2007  * _scsih_target_reset - eh threads main target reset routine
2008  * @sdev: scsi device struct
2009  *
2010  * Returns SUCCESS if command aborted else FAILED
2011  */
2012 static int
2013 _scsih_target_reset(struct scsi_cmnd *scmd)
2014 {
2015         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2016         struct MPT2SAS_DEVICE *sas_device_priv_data;
2017         struct _sas_device *sas_device;
2018         unsigned long flags;
2019         u16     handle;
2020         int r;
2021
2022         printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2023             ioc->name, scmd);
2024         scsi_print_command(scmd);
2025
2026         sas_device_priv_data = scmd->device->hostdata;
2027         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2028                 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2029                     ioc->name, scmd);
2030                 scmd->result = DID_NO_CONNECT << 16;
2031                 scmd->scsi_done(scmd);
2032                 r = SUCCESS;
2033                 goto out;
2034         }
2035
2036         /* for hidden raid components obtain the volume_handle */
2037         handle = 0;
2038         if (sas_device_priv_data->sas_target->flags &
2039             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2040                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2041                 sas_device = _scsih_sas_device_find_by_handle(ioc,
2042                    sas_device_priv_data->sas_target->handle);
2043                 if (sas_device)
2044                         handle = sas_device->volume_handle;
2045                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2046         } else
2047                 handle = sas_device_priv_data->sas_target->handle;
2048
2049         if (!handle) {
2050                 scmd->result = DID_RESET << 16;
2051                 r = FAILED;
2052                 goto out;
2053         }
2054
2055         mutex_lock(&ioc->tm_cmds.mutex);
2056         mpt2sas_scsih_issue_tm(ioc, handle, 0,
2057             MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2058
2059         /*
2060          *  sanity check see whether all commands to this target been
2061          *  completed
2062          */
2063         if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2064             scmd->device->channel))
2065                 r = FAILED;
2066         else
2067                 r = SUCCESS;
2068         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2069         mutex_unlock(&ioc->tm_cmds.mutex);
2070
2071  out:
2072         printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2073             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2074         return r;
2075 }
2076
2077 /**
2078  * _scsih_abort - eh threads main host reset routine
2079  * @sdev: scsi device struct
2080  *
2081  * Returns SUCCESS if command aborted else FAILED
2082  */
2083 static int
2084 _scsih_host_reset(struct scsi_cmnd *scmd)
2085 {
2086         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2087         int r, retval;
2088
2089         printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2090             ioc->name, scmd);
2091         scsi_print_command(scmd);
2092
2093         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2094             FORCE_BIG_HAMMER);
2095         r = (retval < 0) ? FAILED : SUCCESS;
2096         printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2097             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2098
2099         return r;
2100 }
2101
2102 /**
2103  * _scsih_fw_event_add - insert and queue up fw_event
2104  * @ioc: per adapter object
2105  * @fw_event: object describing the event
2106  * Context: This function will acquire ioc->fw_event_lock.
2107  *
2108  * This adds the firmware event object into link list, then queues it up to
2109  * be processed from user context.
2110  *
2111  * Return nothing.
2112  */
2113 static void
2114 _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2115 {
2116         unsigned long flags;
2117
2118         if (ioc->firmware_event_thread == NULL)
2119                 return;
2120
2121         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2122         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2123         INIT_WORK(&fw_event->work, _firmware_event_work);
2124         queue_work(ioc->firmware_event_thread, &fw_event->work);
2125         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2126 }
2127
2128 /**
2129  * _scsih_fw_event_free - delete fw_event
2130  * @ioc: per adapter object
2131  * @fw_event: object describing the event
2132  * Context: This function will acquire ioc->fw_event_lock.
2133  *
2134  * This removes firmware event object from link list, frees associated memory.
2135  *
2136  * Return nothing.
2137  */
2138 static void
2139 _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2140     *fw_event)
2141 {
2142         unsigned long flags;
2143
2144         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2145         list_del(&fw_event->list);
2146         kfree(fw_event->event_data);
2147         kfree(fw_event);
2148         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2149 }
2150
2151 /**
2152  * _scsih_fw_event_add - requeue an event
2153  * @ioc: per adapter object
2154  * @fw_event: object describing the event
2155  * Context: This function will acquire ioc->fw_event_lock.
2156  *
2157  * Return nothing.
2158  */
2159 static void
2160 _scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2161     *fw_event, unsigned long delay)
2162 {
2163         unsigned long flags;
2164         if (ioc->firmware_event_thread == NULL)
2165                 return;
2166
2167         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2168         queue_work(ioc->firmware_event_thread, &fw_event->work);
2169         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2170 }
2171
2172 /**
2173  * _scsih_fw_event_off - turn flag off preventing event handling
2174  * @ioc: per adapter object
2175  *
2176  * Used to prevent handling of firmware events during adapter reset
2177  * driver unload.
2178  *
2179  * Return nothing.
2180  */
2181 static void
2182 _scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2183 {
2184         unsigned long flags;
2185
2186         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2187         ioc->fw_events_off = 1;
2188         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2189
2190 }
2191
2192 /**
2193  * _scsih_fw_event_on - turn flag on allowing firmware event handling
2194  * @ioc: per adapter object
2195  *
2196  * Returns nothing.
2197  */
2198 static void
2199 _scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2200 {
2201         unsigned long flags;
2202
2203         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2204         ioc->fw_events_off = 0;
2205         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2206 }
2207
2208 /**
2209  * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2210  * @ioc: per adapter object
2211  * @handle: device handle
2212  *
2213  * During device pull we need to appropiately set the sdev state.
2214  */
2215 static void
2216 _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2217 {
2218         struct MPT2SAS_DEVICE *sas_device_priv_data;
2219         struct scsi_device *sdev;
2220
2221         shost_for_each_device(sdev, ioc->shost) {
2222                 sas_device_priv_data = sdev->hostdata;
2223                 if (!sas_device_priv_data)
2224                         continue;
2225                 if (!sas_device_priv_data->block)
2226                         continue;
2227                 if (sas_device_priv_data->sas_target->handle == handle) {
2228                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2229                             MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2230                             "handle(0x%04x)\n", ioc->name, handle));
2231                         sas_device_priv_data->block = 0;
2232                         scsi_internal_device_unblock(sdev);
2233                 }
2234         }
2235 }
2236
2237 /**
2238  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2239  * @ioc: per adapter object
2240  * @handle: device handle
2241  *
2242  * During device pull we need to appropiately set the sdev state.
2243  */
2244 static void
2245 _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2246 {
2247         struct MPT2SAS_DEVICE *sas_device_priv_data;
2248         struct scsi_device *sdev;
2249
2250         shost_for_each_device(sdev, ioc->shost) {
2251                 sas_device_priv_data = sdev->hostdata;
2252                 if (!sas_device_priv_data)
2253                         continue;
2254                 if (sas_device_priv_data->block)
2255                         continue;
2256                 if (sas_device_priv_data->sas_target->handle == handle) {
2257                         dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2258                             MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2259                             "handle(0x%04x)\n", ioc->name, handle));
2260                         sas_device_priv_data->block = 1;
2261                         scsi_internal_device_block(sdev);
2262                 }
2263         }
2264 }
2265
2266 /**
2267  * _scsih_block_io_to_children_attached_to_ex
2268  * @ioc: per adapter object
2269  * @sas_expander: the sas_device object
2270  *
2271  * This routine set sdev state to SDEV_BLOCK for all devices
2272  * attached to this expander. This function called when expander is
2273  * pulled.
2274  */
2275 static void
2276 _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2277     struct _sas_node *sas_expander)
2278 {
2279         struct _sas_port *mpt2sas_port;
2280         struct _sas_device *sas_device;
2281         struct _sas_node *expander_sibling;
2282         unsigned long flags;
2283
2284         if (!sas_expander)
2285                 return;
2286
2287         list_for_each_entry(mpt2sas_port,
2288            &sas_expander->sas_port_list, port_list) {
2289                 if (mpt2sas_port->remote_identify.device_type ==
2290                     SAS_END_DEVICE) {
2291                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2292                         sas_device =
2293                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2294                            mpt2sas_port->remote_identify.sas_address);
2295                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2296                         if (!sas_device)
2297                                 continue;
2298                         _scsih_block_io_device(ioc, sas_device->handle);
2299                 }
2300         }
2301
2302         list_for_each_entry(mpt2sas_port,
2303            &sas_expander->sas_port_list, port_list) {
2304
2305                 if (mpt2sas_port->remote_identify.device_type ==
2306                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2307                     mpt2sas_port->remote_identify.device_type ==
2308                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2309
2310                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
2311                         expander_sibling =
2312                             mpt2sas_scsih_expander_find_by_sas_address(
2313                             ioc, mpt2sas_port->remote_identify.sas_address);
2314                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2315                         _scsih_block_io_to_children_attached_to_ex(ioc,
2316                             expander_sibling);
2317                 }
2318         }
2319 }
2320
2321 /**
2322  * _scsih_block_io_to_children_attached_directly
2323  * @ioc: per adapter object
2324  * @event_data: topology change event data
2325  *
2326  * This routine set sdev state to SDEV_BLOCK for all devices
2327  * direct attached during device pull.
2328  */
2329 static void
2330 _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2331     Mpi2EventDataSasTopologyChangeList_t *event_data)
2332 {
2333         int i;
2334         u16 handle;
2335         u16 reason_code;
2336         u8 phy_number;
2337         u8 link_rate;
2338
2339         for (i = 0; i < event_data->NumEntries; i++) {
2340                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2341                 if (!handle)
2342                         continue;
2343                 phy_number = event_data->StartPhyNum + i;
2344                 reason_code = event_data->PHY[i].PhyStatus &
2345                     MPI2_EVENT_SAS_TOPO_RC_MASK;
2346                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2347                         _scsih_block_io_device(ioc, handle);
2348                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2349                         link_rate = event_data->PHY[i].LinkRate >> 4;
2350                         if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2351                                 _scsih_ublock_io_device(ioc, handle);
2352                 }
2353         }
2354 }
2355
2356 /**
2357  * _scsih_check_topo_delete_events - sanity check on topo events
2358  * @ioc: per adapter object
2359  * @event_data: the event data payload
2360  *
2361  * This routine added to better handle cable breaker.
2362  *
2363  * This handles the case where driver recieves multiple expander
2364  * add and delete events in a single shot.  When there is a delete event
2365  * the routine will void any pending add events waiting in the event queue.
2366  *
2367  * Return nothing.
2368  */
2369 static void
2370 _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2371     Mpi2EventDataSasTopologyChangeList_t *event_data)
2372 {
2373         struct fw_event_work *fw_event;
2374         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2375         u16 expander_handle;
2376         struct _sas_node *sas_expander;
2377         unsigned long flags;
2378
2379         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2380         if (expander_handle < ioc->sas_hba.num_phys) {
2381                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2382                 return;
2383         }
2384
2385         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2386          || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2387                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2388                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2389                     expander_handle);
2390                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2391                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2392         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2393                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2394
2395         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2396                 return;
2397
2398         /* mark ignore flag for pending events */
2399         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2400         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2401                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2402                     fw_event->ignore)
2403                         continue;
2404                 local_event_data = fw_event->event_data;
2405                 if (local_event_data->ExpStatus ==
2406                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2407                     local_event_data->ExpStatus ==
2408                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2409                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2410                             expander_handle) {
2411                                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2412                                     "setting ignoring flag\n", ioc->name));
2413                                 fw_event->ignore = 1;
2414                         }
2415                 }
2416         }
2417         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2418 }
2419
2420 /**
2421  * _scsih_flush_running_cmds - completing outstanding commands.
2422  * @ioc: per adapter object
2423  *
2424  * The flushing out of all pending scmd commands following host reset,
2425  * where all IO is dropped to the floor.
2426  *
2427  * Return nothing.
2428  */
2429 static void
2430 _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2431 {
2432         struct scsi_cmnd *scmd;
2433         u16 smid;
2434         u16 count = 0;
2435
2436         for (smid = 1; smid <= ioc->request_depth; smid++) {
2437                 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2438                 if (!scmd)
2439                         continue;
2440                 count++;
2441                 mpt2sas_base_free_smid(ioc, smid);
2442                 scsi_dma_unmap(scmd);
2443                 scmd->result = DID_RESET << 16;
2444                 scmd->scsi_done(scmd);
2445         }
2446         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2447             ioc->name, count));
2448 }
2449
2450 /**
2451  * _scsih_setup_eedp - setup MPI request for EEDP transfer
2452  * @scmd: pointer to scsi command object
2453  * @mpi_request: pointer to the SCSI_IO reqest message frame
2454  *
2455  * Supporting protection 1 and 3.
2456  *
2457  * Returns nothing
2458  */
2459 static void
2460 _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2461 {
2462         u16 eedp_flags;
2463         unsigned char prot_op = scsi_get_prot_op(scmd);
2464         unsigned char prot_type = scsi_get_prot_type(scmd);
2465
2466         if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2467            prot_type == SCSI_PROT_DIF_TYPE2 ||
2468            prot_op == SCSI_PROT_NORMAL)
2469                 return;
2470
2471         if (prot_op ==  SCSI_PROT_READ_STRIP)
2472                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2473         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
2474                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2475         else
2476                 return;
2477
2478         mpi_request->EEDPBlockSize = scmd->device->sector_size;
2479
2480         switch (prot_type) {
2481         case SCSI_PROT_DIF_TYPE1:
2482
2483                 /*
2484                 * enable ref/guard checking
2485                 * auto increment ref tag
2486                 */
2487                 mpi_request->EEDPFlags = eedp_flags |
2488                     MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2489                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2490                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2491                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2492                     cpu_to_be32(scsi_get_lba(scmd));
2493
2494                 break;
2495
2496         case SCSI_PROT_DIF_TYPE3:
2497
2498                 /*
2499                 * enable guard checking
2500                 */
2501                 mpi_request->EEDPFlags = eedp_flags |
2502                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2503
2504                 break;
2505         }
2506 }
2507
2508 /**
2509  * _scsih_eedp_error_handling - return sense code for EEDP errors
2510  * @scmd: pointer to scsi command object
2511  * @ioc_status: ioc status
2512  *
2513  * Returns nothing
2514  */
2515 static void
2516 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2517 {
2518         u8 ascq;
2519         u8 sk;
2520         u8 host_byte;
2521
2522         switch (ioc_status) {
2523         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2524                 ascq = 0x01;
2525                 break;
2526         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2527                 ascq = 0x02;
2528                 break;
2529         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2530                 ascq = 0x03;
2531                 break;
2532         default:
2533                 ascq = 0x00;
2534                 break;
2535         }
2536
2537         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2538                 sk = ILLEGAL_REQUEST;
2539                 host_byte = DID_ABORT;
2540         } else {
2541                 sk = ABORTED_COMMAND;
2542                 host_byte = DID_OK;
2543         }
2544
2545         scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2546         scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2547             SAM_STAT_CHECK_CONDITION;
2548 }
2549
2550 /**
2551  * _scsih_qcmd - main scsi request entry point
2552  * @scmd: pointer to scsi command object
2553  * @done: function pointer to be invoked on completion
2554  *
2555  * The callback index is set inside `ioc->scsi_io_cb_idx`.
2556  *
2557  * Returns 0 on success.  If there's a failure, return either:
2558  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2559  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2560  */
2561 static int
2562 _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
2563 {
2564         struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2565         struct MPT2SAS_DEVICE *sas_device_priv_data;
2566         struct MPT2SAS_TARGET *sas_target_priv_data;
2567         Mpi2SCSIIORequest_t *mpi_request;
2568         u32 mpi_control;
2569         u16 smid;
2570
2571         scmd->scsi_done = done;
2572         sas_device_priv_data = scmd->device->hostdata;
2573         if (!sas_device_priv_data) {
2574                 scmd->result = DID_NO_CONNECT << 16;
2575                 scmd->scsi_done(scmd);
2576                 return 0;
2577         }
2578
2579         sas_target_priv_data = sas_device_priv_data->sas_target;
2580         if (!sas_target_priv_data || sas_target_priv_data->handle ==
2581             MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2582                 scmd->result = DID_NO_CONNECT << 16;
2583                 scmd->scsi_done(scmd);
2584                 return 0;
2585         }
2586
2587         /* see if we are busy with task managment stuff */
2588         if (sas_target_priv_data->tm_busy)
2589                 return SCSI_MLQUEUE_DEVICE_BUSY;
2590         else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
2591                 return SCSI_MLQUEUE_HOST_BUSY;
2592
2593         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2594                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2595         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2596                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2597         else
2598                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2599
2600         /* set tags */
2601         if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2602                 if (scmd->device->tagged_supported) {
2603                         if (scmd->device->ordered_tags)
2604                                 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2605                         else
2606                                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2607                 } else
2608 /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2609 /*                      mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2610  */
2611                         mpi_control |= (0x500);
2612
2613         } else
2614                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2615
2616         if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2617                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2618
2619         smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2620         if (!smid) {
2621                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2622                     ioc->name, __func__);
2623                 goto out;
2624         }
2625         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2626         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
2627         _scsih_setup_eedp(scmd, mpi_request);
2628         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2629         if (sas_device_priv_data->sas_target->flags &
2630             MPT_TARGET_FLAGS_RAID_COMPONENT)
2631                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2632         else
2633                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2634         mpi_request->DevHandle =
2635             cpu_to_le16(sas_device_priv_data->sas_target->handle);
2636         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2637         mpi_request->Control = cpu_to_le32(mpi_control);
2638         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2639         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2640         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2641         mpi_request->SenseBufferLowAddress =
2642             (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2643         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2644         mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2645             MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2646
2647         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2648             mpi_request->LUN);
2649         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2650
2651         if (!mpi_request->DataLength) {
2652                 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2653         } else {
2654                 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2655                         mpt2sas_base_free_smid(ioc, smid);
2656                         goto out;
2657                 }
2658         }
2659
2660         _scsih_scsi_lookup_set(ioc, smid, scmd);
2661         mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2662             sas_device_priv_data->sas_target->handle);
2663         return 0;
2664
2665  out:
2666         return SCSI_MLQUEUE_HOST_BUSY;
2667 }
2668
2669 /**
2670  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2671  * @sense_buffer: sense data returned by target
2672  * @data: normalized skey/asc/ascq
2673  *
2674  * Return nothing.
2675  */
2676 static void
2677 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2678 {
2679         if ((sense_buffer[0] & 0x7F) >= 0x72) {
2680                 /* descriptor format */
2681                 data->skey = sense_buffer[1] & 0x0F;
2682                 data->asc = sense_buffer[2];
2683                 data->ascq = sense_buffer[3];
2684         } else {
2685                 /* fixed format */
2686                 data->skey = sense_buffer[2] & 0x0F;
2687                 data->asc = sense_buffer[12];
2688                 data->ascq = sense_buffer[13];
2689         }
2690 }
2691
2692 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2693 /**
2694  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2695  * @ioc: per adapter object
2696  * @scmd: pointer to scsi command object
2697  * @mpi_reply: reply mf payload returned from firmware
2698  *
2699  * scsi_status - SCSI Status code returned from target device
2700  * scsi_state - state info associated with SCSI_IO determined by ioc
2701  * ioc_status - ioc supplied status info
2702  *
2703  * Return nothing.
2704  */
2705 static void
2706 _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2707     Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2708 {
2709         u32 response_info;
2710         u8 *response_bytes;
2711         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2712             MPI2_IOCSTATUS_MASK;
2713         u8 scsi_state = mpi_reply->SCSIState;
2714         u8 scsi_status = mpi_reply->SCSIStatus;
2715         char *desc_ioc_state = NULL;
2716         char *desc_scsi_status = NULL;
2717         char *desc_scsi_state = ioc->tmp_string;
2718         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2719
2720         if (log_info == 0x31170000)
2721                 return;
2722
2723         switch (ioc_status) {
2724         case MPI2_IOCSTATUS_SUCCESS:
2725                 desc_ioc_state = "success";
2726                 break;
2727         case MPI2_IOCSTATUS_INVALID_FUNCTION:
2728                 desc_ioc_state = "invalid function";
2729                 break;
2730         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2731                 desc_ioc_state = "scsi recovered error";
2732                 break;
2733         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2734                 desc_ioc_state = "scsi invalid dev handle";
2735                 break;
2736         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2737                 desc_ioc_state = "scsi device not there";
2738                 break;
2739         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2740                 desc_ioc_state = "scsi data overrun";
2741                 break;
2742         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2743                 desc_ioc_state = "scsi data underrun";
2744                 break;
2745         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2746                 desc_ioc_state = "scsi io data error";
2747                 break;
2748         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2749                 desc_ioc_state = "scsi protocol error";
2750                 break;
2751         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2752                 desc_ioc_state = "scsi task terminated";
2753                 break;
2754         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2755                 desc_ioc_state = "scsi residual mismatch";
2756                 break;
2757         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2758                 desc_ioc_state = "scsi task mgmt failed";
2759                 break;
2760         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2761                 desc_ioc_state = "scsi ioc terminated";
2762                 break;
2763         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2764                 desc_ioc_state = "scsi ext terminated";
2765                 break;
2766         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2767                 desc_ioc_state = "eedp guard error";
2768                 break;
2769         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2770                 desc_ioc_state = "eedp ref tag error";
2771                 break;
2772         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2773                 desc_ioc_state = "eedp app tag error";
2774                 break;
2775         default:
2776                 desc_ioc_state = "unknown";
2777                 break;
2778         }
2779
2780         switch (scsi_status) {
2781         case MPI2_SCSI_STATUS_GOOD:
2782                 desc_scsi_status = "good";
2783                 break;
2784         case MPI2_SCSI_STATUS_CHECK_CONDITION:
2785                 desc_scsi_status = "check condition";
2786                 break;
2787         case MPI2_SCSI_STATUS_CONDITION_MET:
2788                 desc_scsi_status = "condition met";
2789                 break;
2790         case MPI2_SCSI_STATUS_BUSY:
2791                 desc_scsi_status = "busy";
2792                 break;
2793         case MPI2_SCSI_STATUS_INTERMEDIATE:
2794                 desc_scsi_status = "intermediate";
2795                 break;
2796         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2797                 desc_scsi_status = "intermediate condmet";
2798                 break;
2799         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2800                 desc_scsi_status = "reservation conflict";
2801                 break;
2802         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2803                 desc_scsi_status = "command terminated";
2804                 break;
2805         case MPI2_SCSI_STATUS_TASK_SET_FULL:
2806                 desc_scsi_status = "task set full";
2807                 break;
2808         case MPI2_SCSI_STATUS_ACA_ACTIVE:
2809                 desc_scsi_status = "aca active";
2810                 break;
2811         case MPI2_SCSI_STATUS_TASK_ABORTED:
2812                 desc_scsi_status = "task aborted";
2813                 break;
2814         default:
2815                 desc_scsi_status = "unknown";
2816                 break;
2817         }
2818
2819         desc_scsi_state[0] = '\0';
2820         if (!scsi_state)
2821                 desc_scsi_state = " ";
2822         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2823                 strcat(desc_scsi_state, "response info ");
2824         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2825                 strcat(desc_scsi_state, "state terminated ");
2826         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2827                 strcat(desc_scsi_state, "no status ");
2828         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2829                 strcat(desc_scsi_state, "autosense failed ");
2830         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2831                 strcat(desc_scsi_state, "autosense valid ");
2832
2833         scsi_print_command(scmd);
2834         printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2835             "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2836             le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2837                 ioc_status, smid);
2838         printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2839             "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2840             scsi_get_resid(scmd));
2841         printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2842             "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2843             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2844         printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2845             "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2846             scsi_status, desc_scsi_state, scsi_state);
2847
2848         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2849                 struct sense_info data;
2850                 _scsih_normalize_sense(scmd->sense_buffer, &data);
2851                 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2852                     "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2853                     data.asc, data.ascq);
2854         }
2855
2856         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2857                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2858                 response_bytes = (u8 *)&response_info;
2859                 _scsih_response_code(ioc, response_bytes[3]);
2860         }
2861 }
2862 #endif
2863
2864 /**
2865  * _scsih_smart_predicted_fault - illuminate Fault LED
2866  * @ioc: per adapter object
2867  * @handle: device handle
2868  *
2869  * Return nothing.
2870  */
2871 static void
2872 _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2873 {
2874         Mpi2SepReply_t mpi_reply;
2875         Mpi2SepRequest_t mpi_request;
2876         struct scsi_target *starget;
2877         struct MPT2SAS_TARGET *sas_target_priv_data;
2878         Mpi2EventNotificationReply_t *event_reply;
2879         Mpi2EventDataSasDeviceStatusChange_t *event_data;
2880         struct _sas_device *sas_device;
2881         ssize_t sz;
2882         unsigned long flags;
2883
2884         /* only handle non-raid devices */
2885         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2886         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2887         if (!sas_device) {
2888                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2889                 return;
2890         }
2891         starget = sas_device->starget;
2892         sas_target_priv_data = starget->hostdata;
2893
2894         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2895            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2896                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2897                 return;
2898         }
2899         starget_printk(KERN_WARNING, starget, "predicted fault\n");
2900         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2901
2902         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2903                 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2904                 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2905                 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2906                 mpi_request.SlotStatus =
2907                     MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2908                 mpi_request.DevHandle = cpu_to_le16(handle);
2909                 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2910                 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2911                     &mpi_request)) != 0) {
2912                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2913                             ioc->name, __FILE__, __LINE__, __func__);
2914                         return;
2915                 }
2916
2917                 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2918                         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2919                             "enclosure_processor: ioc_status (0x%04x), "
2920                             "loginfo(0x%08x)\n", ioc->name,
2921                             le16_to_cpu(mpi_reply.IOCStatus),
2922                             le32_to_cpu(mpi_reply.IOCLogInfo)));
2923                         return;
2924                 }
2925         }
2926
2927         /* insert into event log */
2928         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2929              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2930         event_reply = kzalloc(sz, GFP_KERNEL);
2931         if (!event_reply) {
2932                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2933                     ioc->name, __FILE__, __LINE__, __func__);
2934                 return;
2935         }
2936
2937         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2938         event_reply->Event =
2939             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2940         event_reply->MsgLength = sz/4;
2941         event_reply->EventDataLength =
2942             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2943         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2944             event_reply->EventData;
2945         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2946         event_data->ASC = 0x5D;
2947         event_data->DevHandle = cpu_to_le16(handle);
2948         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2949         mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2950         kfree(event_reply);
2951 }
2952
2953 /**
2954  * _scsih_io_done - scsi request callback
2955  * @ioc: per adapter object
2956  * @smid: system request message index
2957  * @VF_ID: virtual function id
2958  * @reply: reply message frame(lower 32bit addr)
2959  *
2960  * Callback handler when using scsih_qcmd.
2961  *
2962  * Return nothing.
2963  */
2964 static void
2965 _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
2966 {
2967         Mpi2SCSIIORequest_t *mpi_request;
2968         Mpi2SCSIIOReply_t *mpi_reply;
2969         struct scsi_cmnd *scmd;
2970         u16 ioc_status;
2971         u32 xfer_cnt;
2972         u8 scsi_state;
2973         u8 scsi_status;
2974         u32 log_info;
2975         struct MPT2SAS_DEVICE *sas_device_priv_data;
2976         u32 response_code;
2977
2978         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2979         scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2980         if (scmd == NULL)
2981                 return;
2982
2983         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2984
2985         if (mpi_reply == NULL) {
2986                 scmd->result = DID_OK << 16;
2987                 goto out;
2988         }
2989
2990         sas_device_priv_data = scmd->device->hostdata;
2991         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2992              sas_device_priv_data->sas_target->deleted) {
2993                 scmd->result = DID_NO_CONNECT << 16;
2994                 goto out;
2995         }
2996
2997         /* turning off TLR */
2998         if (!sas_device_priv_data->tlr_snoop_check) {
2999                 sas_device_priv_data->tlr_snoop_check++;
3000                 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
3001                         response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
3002                             >> 24);
3003                         if (response_code ==
3004                             MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3005                                 sas_device_priv_data->flags &=
3006                                     ~MPT_DEVICE_TLR_ON;
3007                 }
3008         }
3009
3010         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3011         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3012         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3013         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3014                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
3015         else
3016                 log_info = 0;
3017         ioc_status &= MPI2_IOCSTATUS_MASK;
3018         scsi_state = mpi_reply->SCSIState;
3019         scsi_status = mpi_reply->SCSIStatus;
3020
3021         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3022             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3023              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3024              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3025                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3026         }
3027
3028         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3029                 struct sense_info data;
3030                 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3031                     smid);
3032                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
3033                     le32_to_cpu(mpi_reply->SenseCount));
3034                 memcpy(scmd->sense_buffer, sense_data, sz);
3035                 _scsih_normalize_sense(scmd->sense_buffer, &data);
3036                 /* failure prediction threshold exceeded */
3037                 if (data.asc == 0x5D)
3038                         _scsih_smart_predicted_fault(ioc,
3039                             le16_to_cpu(mpi_reply->DevHandle));
3040         }
3041
3042         switch (ioc_status) {
3043         case MPI2_IOCSTATUS_BUSY:
3044         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3045                 scmd->result = SAM_STAT_BUSY;
3046                 break;
3047
3048         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3049                 scmd->result = DID_NO_CONNECT << 16;
3050                 break;
3051
3052         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3053                 if (sas_device_priv_data->block) {
3054                         scmd->result = (DID_BUS_BUSY << 16);
3055                         break;
3056                 }
3057
3058         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3059         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3060                 scmd->result = DID_RESET << 16;
3061                 break;
3062
3063         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3064                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3065                         scmd->result = DID_SOFT_ERROR << 16;
3066                 else
3067                         scmd->result = (DID_OK << 16) | scsi_status;
3068                 break;
3069
3070         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3071                 scmd->result = (DID_OK << 16) | scsi_status;
3072
3073                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3074                         break;
3075
3076                 if (xfer_cnt < scmd->underflow) {
3077                         if (scsi_status == SAM_STAT_BUSY)
3078                                 scmd->result = SAM_STAT_BUSY;
3079                         else
3080                                 scmd->result = DID_SOFT_ERROR << 16;
3081                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3082                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3083                         scmd->result = DID_SOFT_ERROR << 16;
3084                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3085                         scmd->result = DID_RESET << 16;
3086                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3087                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3088                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3089                         scmd->result = (DRIVER_SENSE << 24) |
3090                             SAM_STAT_CHECK_CONDITION;
3091                         scmd->sense_buffer[0] = 0x70;
3092                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3093                         scmd->sense_buffer[12] = 0x20;
3094                         scmd->sense_buffer[13] = 0;
3095                 }
3096                 break;
3097
3098         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3099                 scsi_set_resid(scmd, 0);
3100         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3101         case MPI2_IOCSTATUS_SUCCESS:
3102                 scmd->result = (DID_OK << 16) | scsi_status;
3103                 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3104                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
3105                         scmd->result = DID_SOFT_ERROR << 16;
3106                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3107                         scmd->result = DID_RESET << 16;
3108                 break;
3109
3110         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3111         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3112         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3113                 _scsih_eedp_error_handling(scmd, ioc_status);
3114                 break;
3115         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3116         case MPI2_IOCSTATUS_INVALID_FUNCTION:
3117         case MPI2_IOCSTATUS_INVALID_SGL:
3118         case MPI2_IOCSTATUS_INTERNAL_ERROR:
3119         case MPI2_IOCSTATUS_INVALID_FIELD:
3120         case MPI2_IOCSTATUS_INVALID_STATE:
3121         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3122         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3123         default:
3124                 scmd->result = DID_SOFT_ERROR << 16;
3125                 break;
3126
3127         }
3128
3129 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3130         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3131                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3132 #endif
3133
3134  out:
3135         scsi_dma_unmap(scmd);
3136         scmd->scsi_done(scmd);
3137 }
3138
3139 /**
3140  * _scsih_sas_host_refresh - refreshing sas host object contents
3141  * @ioc: per adapter object
3142  * @update: update link information
3143  * Context: user
3144  *
3145  * During port enable, fw will send topology events for every device. Its
3146  * possible that the handles may change from the previous setting, so this
3147  * code keeping handles updating if changed.
3148  *
3149  * Return nothing.
3150  */
3151 static void
3152 _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3153 {
3154         u16 sz;
3155         u16 ioc_status;
3156         int i;
3157         Mpi2ConfigReply_t mpi_reply;
3158         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3159
3160         dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3161             "updating handles for sas_host(0x%016llx)\n",
3162             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3163
3164         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3165             * sizeof(Mpi2SasIOUnit0PhyData_t));
3166         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3167         if (!sas_iounit_pg0) {
3168                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3169                     ioc->name, __FILE__, __LINE__, __func__);
3170                 return;
3171         }
3172         if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3173             sas_iounit_pg0, sz))) {
3174                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3175                     MPI2_IOCSTATUS_MASK;
3176                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3177                         goto out;
3178                 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3179                         ioc->sas_hba.phy[i].handle =
3180                             le16_to_cpu(sas_iounit_pg0->PhyData[i].
3181                                 ControllerDevHandle);
3182                         if (update)
3183                                 mpt2sas_transport_update_links(
3184                                     ioc,
3185                                     ioc->sas_hba.phy[i].handle,
3186                                     le16_to_cpu(sas_iounit_pg0->PhyData[i].
3187                                     AttachedDevHandle), i,
3188                                     sas_iounit_pg0->PhyData[i].
3189                                     NegotiatedLinkRate >> 4);
3190                 }
3191         }
3192
3193  out:
3194         kfree(sas_iounit_pg0);
3195 }
3196
3197 /**
3198  * _scsih_sas_host_add - create sas host object
3199  * @ioc: per adapter object
3200  *
3201  * Creating host side data object, stored in ioc->sas_hba
3202  *
3203  * Return nothing.
3204  */
3205 static void
3206 _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3207 {
3208         int i;
3209         Mpi2ConfigReply_t mpi_reply;
3210         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3211         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3212         Mpi2SasPhyPage0_t phy_pg0;
3213         Mpi2SasDevicePage0_t sas_device_pg0;
3214         Mpi2SasEnclosurePage0_t enclosure_pg0;
3215         u16 ioc_status;
3216         u16 sz;
3217         u16 device_missing_delay;
3218
3219         mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3220         if (!ioc->sas_hba.num_phys) {
3221                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3222                     ioc->name, __FILE__, __LINE__, __func__);
3223                 return;
3224         }
3225
3226         /* sas_iounit page 0 */
3227         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3228             sizeof(Mpi2SasIOUnit0PhyData_t));
3229         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3230         if (!sas_iounit_pg0) {
3231                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3232                     ioc->name, __FILE__, __LINE__, __func__);
3233                 return;
3234         }
3235         if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3236             sas_iounit_pg0, sz))) {
3237                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3238                     ioc->name, __FILE__, __LINE__, __func__);
3239                 goto out;
3240         }
3241         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3242             MPI2_IOCSTATUS_MASK;
3243         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3244                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3245                     ioc->name, __FILE__, __LINE__, __func__);
3246                 goto out;
3247         }
3248
3249         /* sas_iounit page 1 */
3250         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3251             sizeof(Mpi2SasIOUnit1PhyData_t));
3252         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3253         if (!sas_iounit_pg1) {
3254                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3255                     ioc->name, __FILE__, __LINE__, __func__);
3256                 goto out;
3257         }
3258         if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3259             sas_iounit_pg1, sz))) {
3260                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3261                     ioc->name, __FILE__, __LINE__, __func__);
3262                 goto out;
3263         }
3264         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3265             MPI2_IOCSTATUS_MASK;
3266         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3267                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3268                     ioc->name, __FILE__, __LINE__, __func__);
3269                 goto out;
3270         }
3271
3272         ioc->io_missing_delay =
3273             le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3274         device_missing_delay =
3275             le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3276         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3277                 ioc->device_missing_delay = (device_missing_delay &
3278                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3279         else
3280                 ioc->device_missing_delay = device_missing_delay &
3281                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3282
3283         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3284         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3285             sizeof(struct _sas_phy), GFP_KERNEL);
3286         if (!ioc->sas_hba.phy) {
3287                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3288                     ioc->name, __FILE__, __LINE__, __func__);
3289                 goto out;
3290         }
3291         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3292                 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3293                     i))) {
3294                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3295                             ioc->name, __FILE__, __LINE__, __func__);
3296                         goto out;
3297                 }
3298                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3299                     MPI2_IOCSTATUS_MASK;
3300                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3301                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3302                             ioc->name, __FILE__, __LINE__, __func__);
3303                         goto out;
3304                 }
3305                 ioc->sas_hba.phy[i].handle =
3306                     le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3307                 ioc->sas_hba.phy[i].phy_id = i;
3308                 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3309                     phy_pg0, ioc->sas_hba.parent_dev);
3310         }
3311         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3312             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3313                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3314                     ioc->name, __FILE__, __LINE__, __func__);
3315                 goto out;
3316         }
3317         ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3318         ioc->sas_hba.enclosure_handle =
3319             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3320         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3321         printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3322             "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3323             (unsigned long long) ioc->sas_hba.sas_address,
3324             ioc->sas_hba.num_phys) ;
3325
3326         if (ioc->sas_hba.enclosure_handle) {
3327                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3328                     &enclosure_pg0,
3329                    MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3330                    ioc->sas_hba.enclosure_handle))) {
3331                         ioc->sas_hba.enclosure_logical_id =
3332                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3333                 }
3334         }
3335
3336  out:
3337         kfree(sas_iounit_pg1);
3338         kfree(sas_iounit_pg0);
3339 }
3340
3341 /**
3342  * _scsih_expander_add -  creating expander object
3343  * @ioc: per adapter object
3344  * @handle: expander handle
3345  *
3346  * Creating expander object, stored in ioc->sas_expander_list.
3347  *
3348  * Return 0 for success, else error.
3349  */
3350 static int
3351 _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3352 {
3353         struct _sas_node *sas_expander;
3354         Mpi2ConfigReply_t mpi_reply;
3355         Mpi2ExpanderPage0_t expander_pg0;
3356         Mpi2ExpanderPage1_t expander_pg1;
3357         Mpi2SasEnclosurePage0_t enclosure_pg0;
3358         u32 ioc_status;
3359         u16 parent_handle;
3360         __le64 sas_address;
3361         int i;
3362         unsigned long flags;
3363         struct _sas_port *mpt2sas_port = NULL;
3364         int rc = 0;
3365
3366         if (!handle)
3367                 return -1;
3368
3369         if (ioc->shost_recovery)
3370                 return -1;
3371
3372         if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3373             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3374                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3375                     ioc->name, __FILE__, __LINE__, __func__);
3376                 return -1;
3377         }
3378
3379         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3380             MPI2_IOCSTATUS_MASK;
3381         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3382                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3383                     ioc->name, __FILE__, __LINE__, __func__);
3384                 return -1;
3385         }
3386
3387         /* handle out of order topology events */
3388         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3389         if (parent_handle >= ioc->sas_hba.num_phys) {
3390                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3391                 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3392                     parent_handle);
3393                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3394                 if (!sas_expander) {
3395                         rc = _scsih_expander_add(ioc, parent_handle);
3396                         if (rc != 0)
3397                                 return rc;
3398                 }
3399         }
3400
3401         sas_address = le64_to_cpu(expander_pg0.SASAddress);
3402
3403         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3404         sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3405             sas_address);
3406         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3407
3408         if (sas_expander)
3409                 return 0;
3410
3411         sas_expander = kzalloc(sizeof(struct _sas_node),
3412             GFP_KERNEL);
3413         if (!sas_expander) {
3414                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3415                     ioc->name, __FILE__, __LINE__, __func__);
3416                 return -1;
3417         }
3418
3419         sas_expander->handle = handle;
3420         sas_expander->num_phys = expander_pg0.NumPhys;
3421         sas_expander->parent_handle = parent_handle;
3422         sas_expander->enclosure_handle =
3423             le16_to_cpu(expander_pg0.EnclosureHandle);
3424         sas_expander->sas_address = sas_address;
3425
3426         printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3427             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3428             handle, sas_expander->parent_handle, (unsigned long long)
3429             sas_expander->sas_address, sas_expander->num_phys);
3430
3431         if (!sas_expander->num_phys)
3432                 goto out_fail;
3433         sas_expander->phy = kcalloc(sas_expander->num_phys,
3434             sizeof(struct _sas_phy), GFP_KERNEL);
3435         if (!sas_expander->phy) {
3436                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3437                     ioc->name, __FILE__, __LINE__, __func__);
3438                 rc = -1;
3439                 goto out_fail;
3440         }
3441
3442         INIT_LIST_HEAD(&sas_expander->sas_port_list);
3443         mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3444             sas_expander->parent_handle);
3445         if (!mpt2sas_port) {
3446                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3447                     ioc->name, __FILE__, __LINE__, __func__);
3448                 rc = -1;
3449                 goto out_fail;
3450         }
3451         sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3452
3453         for (i = 0 ; i < sas_expander->num_phys ; i++) {
3454                 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3455                     &expander_pg1, i, handle))) {
3456                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3457                             ioc->name, __FILE__, __LINE__, __func__);
3458                         rc = -1;
3459                         goto out_fail;
3460                 }
3461                 sas_expander->phy[i].handle = handle;
3462                 sas_expander->phy[i].phy_id = i;
3463
3464                 if ((mpt2sas_transport_add_expander_phy(ioc,
3465                     &sas_expander->phy[i], expander_pg1,
3466                     sas_expander->parent_dev))) {
3467                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3468                             ioc->name, __FILE__, __LINE__, __func__);
3469                         rc = -1;
3470                         goto out_fail;
3471                 }
3472         }
3473
3474         if (sas_expander->enclosure_handle) {
3475                 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3476                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3477                    sas_expander->enclosure_handle))) {
3478                         sas_expander->enclosure_logical_id =
3479                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3480                 }
3481         }
3482
3483         _scsih_expander_node_add(ioc, sas_expander);
3484          return 0;
3485
3486  out_fail:
3487
3488         if (mpt2sas_port)
3489                 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3490                     sas_expander->parent_handle);
3491         kfree(sas_expander);
3492         return rc;
3493 }
3494
3495 /**
3496  * _scsih_expander_remove - removing expander object
3497  * @ioc: per adapter object
3498  * @handle: expander handle
3499  *
3500  * Return nothing.
3501  */
3502 static void
3503 _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3504 {
3505         struct _sas_node *sas_expander;
3506         unsigned long flags;
3507
3508         if (ioc->shost_recovery)
3509                 return;
3510
3511         spin_lock_irqsave(&ioc->sas_node_lock, flags);
3512         sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3513         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3514         _scsih_expander_node_remove(ioc, sas_expander);
3515 }
3516
3517 /**
3518  * _scsih_add_device -  creating sas device object
3519  * @ioc: per adapter object
3520  * @handle: sas device handle
3521  * @phy_num: phy number end device attached to
3522  * @is_pd: is this hidden raid component
3523  *
3524  * Creating end device object, stored in ioc->sas_device_list.
3525  *
3526  * Returns 0 for success, non-zero for failure.
3527  */
3528 static int
3529 _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3530 {
3531         Mpi2ConfigReply_t mpi_reply;
3532         Mpi2SasDevicePage0_t sas_device_pg0;
3533         Mpi2SasEnclosurePage0_t enclosure_pg0;
3534         struct _sas_device *sas_device;
3535         u32 ioc_status;
3536         __le64 sas_address;
3537         u32 device_info;
3538         unsigned long flags;
3539
3540         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3541             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3542                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3543                     ioc->name, __FILE__, __LINE__, __func__);
3544                 return -1;
3545         }
3546
3547         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3548             MPI2_IOCSTATUS_MASK;
3549         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3550                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3551                     ioc->name, __FILE__, __LINE__, __func__);
3552                 return -1;
3553         }
3554
3555         /* check if device is present */
3556         if (!(le16_to_cpu(sas_device_pg0.Flags) &
3557             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3558                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3559                     ioc->name, __FILE__, __LINE__, __func__);
3560                 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3561                     ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3562                 return -1;
3563         }
3564
3565         /* check if there were any issus with discovery */
3566         if (sas_device_pg0.AccessStatus ==
3567             MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3568                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3569                     ioc->name, __FILE__, __LINE__, __func__);
3570                 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3571                     ioc->name, sas_device_pg0.AccessStatus);
3572                 return -1;
3573         }
3574
3575         /* check if this is end device */
3576         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3577         if (!(_scsih_is_end_device(device_info))) {
3578                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3579                     ioc->name, __FILE__, __LINE__, __func__);
3580                 return -1;
3581         }
3582
3583         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3584
3585         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3586         sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3587             sas_address);
3588         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3589
3590         if (sas_device) {
3591                 _scsih_ublock_io_device(ioc, handle);
3592                 return 0;
3593         }
3594
3595         sas_device = kzalloc(sizeof(struct _sas_device),
3596             GFP_KERNEL);
3597         if (!sas_device) {
3598                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3599                     ioc->name, __FILE__, __LINE__, __func__);
3600                 return -1;
3601         }
3602
3603         sas_device->handle = handle;
3604         sas_device->parent_handle =
3605             le16_to_cpu(sas_device_pg0.ParentDevHandle);
3606         sas_device->enclosure_handle =
3607             le16_to_cpu(sas_device_pg0.EnclosureHandle);
3608         sas_device->slot =
3609             le16_to_cpu(sas_device_pg0.Slot);
3610         sas_device->device_info = device_info;
3611         sas_device->sas_address = sas_address;
3612         sas_device->hidden_raid_component = is_pd;
3613
3614         /* get enclosure_logical_id */
3615         if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3616            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3617            sas_device->enclosure_handle)))
3618                 sas_device->enclosure_logical_id =
3619                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3620
3621         /* get device name */
3622         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3623
3624         if (ioc->wait_for_port_enable_to_complete)
3625                 _scsih_sas_device_init_add(ioc, sas_device);
3626         else
3627                 _scsih_sas_device_add(ioc, sas_device);
3628
3629         return 0;
3630 }
3631
3632 /**
3633  * _scsih_remove_device -  removing sas device object
3634  * @ioc: per adapter object
3635  * @handle: sas device handle
3636  *
3637  * Return nothing.
3638  */
3639 static void
3640 _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3641 {
3642         struct MPT2SAS_TARGET *sas_target_priv_data;
3643         struct _sas_device *sas_device;
3644         unsigned long flags;
3645         Mpi2SasIoUnitControlReply_t mpi_reply;
3646         Mpi2SasIoUnitControlRequest_t mpi_request;
3647         u16 device_handle;
3648
3649         /* lookup sas_device */
3650         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3651         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3652         if (!sas_device) {
3653                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3654                 return;
3655         }
3656
3657         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3658             "(0x%04x)\n", ioc->name, __func__, handle));
3659
3660         if (sas_device->starget && sas_device->starget->hostdata) {
3661                 sas_target_priv_data = sas_device->starget->hostdata;
3662                 sas_target_priv_data->deleted = 1;
3663         }
3664         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3665
3666         if (ioc->remove_host)
3667                 goto out;
3668
3669         /* Target Reset to flush out all the outstanding IO */
3670         device_handle = (sas_device->hidden_raid_component) ?
3671             sas_device->volume_handle : handle;
3672         if (device_handle) {
3673                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3674                     "handle(0x%04x)\n", ioc->name, device_handle));
3675                 mutex_lock(&ioc->tm_cmds.mutex);
3676                 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3677                     MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3678                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3679                 mutex_unlock(&ioc->tm_cmds.mutex);
3680                 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3681                     "done: handle(0x%04x)\n", ioc->name, device_handle));
3682                 if (ioc->shost_recovery)
3683                         goto out;
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
3707         _scsih_ublock_io_device(ioc, handle);
3708
3709         mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3710             sas_device->parent_handle);
3711
3712         printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3713             "(0x%016llx)\n", ioc->name, sas_device->handle,
3714             (unsigned long long) sas_device->sas_address);
3715         _scsih_sas_device_remove(ioc, sas_device);
3716
3717         dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3718             "(0x%04x)\n", ioc->name, __func__, handle));
3719 }
3720
3721 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3722 /**
3723  * _scsih_sas_topology_change_event_debug - debug for topology event
3724  * @ioc: per adapter object
3725  * @event_data: event data payload
3726  * Context: user.
3727  */
3728 static void
3729 _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3730     Mpi2EventDataSasTopologyChangeList_t *event_data)
3731 {
3732         int i;
3733         u16 handle;
3734         u16 reason_code;
3735         u8 phy_number;
3736         char *status_str = NULL;
3737         char link_rate[25];
3738
3739         switch (event_data->ExpStatus) {
3740         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3741                 status_str = "add";
3742                 break;
3743         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3744                 status_str = "remove";
3745                 break;
3746         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3747                 status_str =  "responding";
3748                 break;
3749         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3750                 status_str = "remove delay";
3751                 break;
3752         default:
3753                 status_str = "unknown status";
3754                 break;
3755         }
3756         printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3757             ioc->name, status_str);
3758         printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3759             "start_phy(%02d), count(%d)\n",
3760             le16_to_cpu(event_data->ExpanderDevHandle),
3761             le16_to_cpu(event_data->EnclosureHandle),
3762             event_data->StartPhyNum, event_data->NumEntries);
3763         for (i = 0; i < event_data->NumEntries; i++) {
3764                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3765                 if (!handle)
3766                         continue;
3767                 phy_number = event_data->StartPhyNum + i;
3768                 reason_code = event_data->PHY[i].PhyStatus &
3769                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3770                 switch (reason_code) {
3771                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3772                         snprintf(link_rate, 25, ": add, link(0x%02x)",
3773                             (event_data->PHY[i].LinkRate >> 4));
3774                         status_str = link_rate;
3775                         break;
3776                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3777                         status_str = ": remove";
3778                         break;
3779                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3780                         status_str = ": remove_delay";
3781                         break;
3782                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3783                         snprintf(link_rate, 25, ": link(0x%02x)",
3784                             (event_data->PHY[i].LinkRate >> 4));
3785                         status_str = link_rate;
3786                         break;
3787                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3788                         status_str = ": responding";
3789                         break;
3790                 default:
3791                         status_str = ": unknown";
3792                         break;
3793                 }
3794                 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3795                     phy_number, handle, status_str);
3796         }
3797 }
3798 #endif
3799
3800 /**
3801  * _scsih_sas_topology_change_event - handle topology changes
3802  * @ioc: per adapter object
3803  * @VF_ID:
3804  * @event_data: event data payload
3805  * fw_event:
3806  * Context: user.
3807  *
3808  */
3809 static void
3810 _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3811     Mpi2EventDataSasTopologyChangeList_t *event_data,
3812     struct fw_event_work *fw_event)
3813 {
3814         int i;
3815         u16 parent_handle, handle;
3816         u16 reason_code;
3817         u8 phy_number;
3818         struct _sas_node *sas_expander;
3819         unsigned long flags;
3820         u8 link_rate_;
3821
3822 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3823         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3824                 _scsih_sas_topology_change_event_debug(ioc, event_data);
3825 #endif
3826
3827         if (!ioc->sas_hba.num_phys)
3828                 _scsih_sas_host_add(ioc);
3829         else
3830                 _scsih_sas_host_refresh(ioc, 0);
3831
3832         if (fw_event->ignore) {
3833                 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3834                     "event\n", ioc->name));
3835                 return;
3836         }
3837
3838         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3839
3840         /* handle expander add */
3841         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3842                 if (_scsih_expander_add(ioc, parent_handle) != 0)
3843                         return;
3844
3845         /* handle siblings events */
3846         for (i = 0; i < event_data->NumEntries; i++) {
3847                 if (fw_event->ignore) {
3848                         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3849                             "expander event\n", ioc->name));
3850                         return;
3851                 }
3852                 if (ioc->shost_recovery)
3853                         return;
3854                 if (event_data->PHY[i].PhyStatus &
3855                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3856                         continue;
3857                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3858                 if (!handle)
3859                         continue;
3860                 phy_number = event_data->StartPhyNum + i;
3861                 reason_code = event_data->PHY[i].PhyStatus &
3862                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3863                 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3864                 switch (reason_code) {
3865                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3866                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3867                         if (!parent_handle) {
3868                                 if (phy_number < ioc->sas_hba.num_phys)
3869                                         mpt2sas_transport_update_links(
3870                                         ioc,
3871                                         ioc->sas_hba.phy[phy_number].handle,
3872                                         handle, phy_number, link_rate_);
3873                         } else {
3874                                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3875                                 sas_expander =
3876                                     mpt2sas_scsih_expander_find_by_handle(ioc,
3877                                         parent_handle);
3878                                 spin_unlock_irqrestore(&ioc->sas_node_lock,
3879                                     flags);
3880                                 if (sas_expander) {
3881                                         if (phy_number < sas_expander->num_phys)
3882                                                 mpt2sas_transport_update_links(
3883                                                 ioc,
3884                                                 sas_expander->
3885                                                 phy[phy_number].handle,
3886                                                 handle, phy_number,
3887                                                 link_rate_);
3888                                 }
3889                         }
3890                         if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3891                                 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3892                                         break;
3893                                 _scsih_add_device(ioc, handle, phy_number, 0);
3894                         }
3895                         break;
3896                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3897                         _scsih_remove_device(ioc, handle);
3898                         break;
3899                 }
3900         }
3901
3902         /* handle expander removal */
3903         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3904                 _scsih_expander_remove(ioc, parent_handle);
3905
3906 }
3907
3908 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3909 /**
3910  * _scsih_sas_device_status_change_event_debug - debug for device event
3911  * @event_data: event data payload
3912  * Context: user.
3913  *
3914  * Return nothing.
3915  */
3916 static void
3917 _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3918     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3919 {
3920         char *reason_str = NULL;
3921
3922         switch (event_data->ReasonCode) {
3923         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3924                 reason_str = "smart data";
3925                 break;
3926         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3927                 reason_str = "unsupported device discovered";
3928                 break;
3929         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3930                 reason_str = "internal device reset";
3931                 break;
3932         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3933                 reason_str = "internal task abort";
3934                 break;
3935         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3936                 reason_str = "internal task abort set";
3937                 break;
3938         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3939                 reason_str = "internal clear task set";
3940                 break;
3941         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3942                 reason_str = "internal query task";
3943                 break;
3944         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3945                 reason_str = "sata init failure";
3946                 break;
3947         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3948                 reason_str = "internal device reset complete";
3949                 break;
3950         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3951                 reason_str = "internal task abort complete";
3952                 break;
3953         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3954                 reason_str = "internal async notification";
3955                 break;
3956         default:
3957                 reason_str = "unknown reason";
3958                 break;
3959         }
3960         printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3961             "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3962             reason_str, le16_to_cpu(event_data->DevHandle),
3963             (unsigned long long)le64_to_cpu(event_data->SASAddress));
3964         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3965                 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3966                     event_data->ASC, event_data->ASCQ);
3967         printk(KERN_INFO "\n");
3968 }
3969 #endif
3970
3971 /**
3972  * _scsih_sas_device_status_change_event - handle device status change
3973  * @ioc: per adapter object
3974  * @VF_ID:
3975  * @event_data: event data payload
3976  * Context: user.
3977  *
3978  * Return nothing.
3979  */
3980 static void
3981 _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3982     Mpi2EventDataSasDeviceStatusChange_t *event_data)
3983 {
3984 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3985         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3986                 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3987 #endif
3988 }
3989
3990 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3991 /**
3992  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3993  * @ioc: per adapter object
3994  * @event_data: event data payload
3995  * Context: user.
3996  *
3997  * Return nothing.
3998  */
3999 static void
4000 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4001     Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4002 {
4003         char *reason_str = NULL;
4004
4005         switch (event_data->ReasonCode) {
4006         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4007                 reason_str = "enclosure add";
4008                 break;
4009         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4010                 reason_str = "enclosure remove";
4011                 break;
4012         default:
4013                 reason_str = "unknown reason";
4014                 break;
4015         }
4016
4017         printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4018             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4019             " number slots(%d)\n", ioc->name, reason_str,
4020             le16_to_cpu(event_data->EnclosureHandle),
4021             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4022             le16_to_cpu(event_data->StartSlot));
4023 }
4024 #endif
4025
4026 /**
4027  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4028  * @ioc: per adapter object
4029  * @VF_ID:
4030  * @event_data: event data payload
4031  * Context: user.
4032  *
4033  * Return nothing.
4034  */
4035 static void
4036 _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4037     u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4038 {
4039 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4040         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4041                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
4042                      event_data);
4043 #endif
4044 }
4045
4046 /**
4047  * _scsih_sas_broadcast_primative_event - handle broadcast events
4048  * @ioc: per adapter object
4049  * @event_data: event data payload
4050  * Context: user.
4051  *
4052  * Return nothing.
4053  */
4054 static void
4055 _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4056     Mpi2EventDataSasBroadcastPrimitive_t *event_data)
4057 {
4058         struct scsi_cmnd *scmd;
4059         u16 smid, handle;
4060         u32 lun;
4061         struct MPT2SAS_DEVICE *sas_device_priv_data;
4062         u32 termination_count;
4063         u32 query_count;
4064         Mpi2SCSITaskManagementReply_t *mpi_reply;
4065
4066         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4067             "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4068             event_data->PortWidth));
4069
4070         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4071             __func__));
4072
4073         mutex_lock(&ioc->tm_cmds.mutex);
4074         termination_count = 0;
4075         query_count = 0;
4076         mpi_reply = ioc->tm_cmds.reply;
4077         for (smid = 1; smid <= ioc->request_depth; smid++) {
4078                 scmd = _scsih_scsi_lookup_get(ioc, smid);
4079                 if (!scmd)
4080                         continue;
4081                 sas_device_priv_data = scmd->device->hostdata;
4082                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4083                         continue;
4084                  /* skip hidden raid components */
4085                 if (sas_device_priv_data->sas_target->flags &
4086                     MPT_TARGET_FLAGS_RAID_COMPONENT)
4087                         continue;
4088                  /* skip volumes */
4089                 if (sas_device_priv_data->sas_target->flags &
4090                     MPT_TARGET_FLAGS_VOLUME)
4091                         continue;
4092
4093                 handle = sas_device_priv_data->sas_target->handle;
4094                 lun = sas_device_priv_data->lun;
4095                 query_count++;
4096
4097                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4098                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
4099                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4100
4101                 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4102                     (mpi_reply->ResponseCode ==
4103                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4104                      mpi_reply->ResponseCode ==
4105                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4106                         continue;
4107
4108                 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4109                     MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4110                 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4111                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4112         }
4113         ioc->broadcast_aen_busy = 0;
4114         mutex_unlock(&ioc->tm_cmds.mutex);
4115
4116         dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4117             "%s - exit, query_count = %d termination_count = %d\n",
4118             ioc->name, __func__, query_count, termination_count));
4119 }
4120
4121 /**
4122  * _scsih_sas_discovery_event - handle discovery events
4123  * @ioc: per adapter object
4124  * @event_data: event data payload
4125  * Context: user.
4126  *
4127  * Return nothing.
4128  */
4129 static void
4130 _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4131     Mpi2EventDataSasDiscovery_t *event_data)
4132 {
4133 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4134         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4135                 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4136                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4137                     "start" : "stop");
4138         if (event_data->DiscoveryStatus)
4139                 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
4140                     ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
4141         printk("\n");
4142         }
4143 #endif
4144
4145         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4146             !ioc->sas_hba.num_phys)
4147                 _scsih_sas_host_add(ioc);
4148 }
4149
4150 /**
4151  * _scsih_reprobe_lun - reprobing lun
4152  * @sdev: scsi device struct
4153  * @no_uld_attach: sdev->no_uld_attach flag setting
4154  *
4155  **/
4156 static void
4157 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4158 {
4159         int rc;
4160
4161         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4162         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4163             sdev->no_uld_attach ? "hidding" : "exposing");
4164         rc = scsi_device_reprobe(sdev);
4165 }
4166
4167 /**
4168  * _scsih_reprobe_target - reprobing target
4169  * @starget: scsi target struct
4170  * @no_uld_attach: sdev->no_uld_attach flag setting
4171  *
4172  * Note: no_uld_attach flag determines whether the disk device is attached
4173  * to block layer. A value of `1` means to not attach.
4174  **/
4175 static void
4176 _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4177 {
4178         struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4179
4180         if (no_uld_attach)
4181                 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4182         else
4183                 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4184
4185         starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4186             _scsih_reprobe_lun);
4187 }
4188 /**
4189  * _scsih_sas_volume_add - add new volume
4190  * @ioc: per adapter object
4191  * @element: IR config element data
4192  * Context: user.
4193  *
4194  * Return nothing.
4195  */
4196 static void
4197 _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4198     Mpi2EventIrConfigElement_t *element)
4199 {
4200         struct _raid_device *raid_device;
4201         unsigned long flags;
4202         u64 wwid;
4203         u16 handle = le16_to_cpu(element->VolDevHandle);
4204         int rc;
4205
4206         mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4207         if (!wwid) {
4208                 printk(MPT2SAS_ERR_FMT
4209                     "failure at %s:%d/%s()!\n", ioc->name,
4210                     __FILE__, __LINE__, __func__);
4211                 return;
4212         }
4213
4214         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4215         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4216         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4217
4218         if (raid_device)
4219                 return;
4220
4221         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4222         if (!raid_device) {
4223                 printk(MPT2SAS_ERR_FMT
4224                     "failure at %s:%d/%s()!\n", ioc->name,
4225                     __FILE__, __LINE__, __func__);
4226                 return;
4227         }
4228
4229         raid_device->id = ioc->sas_id++;
4230         raid_device->channel = RAID_CHANNEL;
4231         raid_device->handle = handle;
4232         raid_device->wwid = wwid;
4233         _scsih_raid_device_add(ioc, raid_device);
4234         if (!ioc->wait_for_port_enable_to_complete) {
4235                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4236                     raid_device->id, 0);
4237                 if (rc)
4238                         _scsih_raid_device_remove(ioc, raid_device);
4239         } else
4240                 _scsih_determine_boot_device(ioc, raid_device, 1);
4241 }
4242
4243 /**
4244  * _scsih_sas_volume_delete - delete volume
4245  * @ioc: per adapter object
4246  * @element: IR config element data
4247  * Context: user.
4248  *
4249  * Return nothing.
4250  */
4251 static void
4252 _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4253     Mpi2EventIrConfigElement_t *element)
4254 {
4255         struct _raid_device *raid_device;
4256         u16 handle = le16_to_cpu(element->VolDevHandle);
4257         unsigned long flags;
4258         struct MPT2SAS_TARGET *sas_target_priv_data;
4259
4260         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4261         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4262         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4263         if (!raid_device)
4264                 return;
4265         if (raid_device->starget) {
4266                 sas_target_priv_data = raid_device->starget->hostdata;
4267                 sas_target_priv_data->deleted = 1;
4268                 scsi_remove_target(&raid_device->starget->dev);
4269         }
4270         _scsih_raid_device_remove(ioc, raid_device);
4271 }
4272
4273 /**
4274  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4275  * @ioc: per adapter object
4276  * @element: IR config element data
4277  * Context: user.
4278  *
4279  * Return nothing.
4280  */
4281 static void
4282 _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4283     Mpi2EventIrConfigElement_t *element)
4284 {
4285         struct _sas_device *sas_device;
4286         unsigned long flags;
4287         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4288
4289         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4290         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4291         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4292         if (!sas_device)
4293                 return;
4294
4295         /* exposing raid component */
4296         sas_device->volume_handle = 0;
4297         sas_device->volume_wwid = 0;
4298         sas_device->hidden_raid_component = 0;
4299         _scsih_reprobe_target(sas_device->starget, 0);
4300 }
4301
4302 /**
4303  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4304  * @ioc: per adapter object
4305  * @element: IR config element data
4306  * Context: user.
4307  *
4308  * Return nothing.
4309  */
4310 static void
4311 _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4312     Mpi2EventIrConfigElement_t *element)
4313 {
4314         struct _sas_device *sas_device;
4315         unsigned long flags;
4316         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4317
4318         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4319         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4320         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4321         if (!sas_device)
4322                 return;
4323
4324         /* hiding raid component */
4325         mpt2sas_config_get_volume_handle(ioc, handle,
4326             &sas_device->volume_handle);
4327         mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4328             &sas_device->volume_wwid);
4329         sas_device->hidden_raid_component = 1;
4330         _scsih_reprobe_target(sas_device->starget, 1);
4331 }
4332
4333 /**
4334  * _scsih_sas_pd_delete - delete pd component
4335  * @ioc: per adapter object
4336  * @element: IR config element data
4337  * Context: user.
4338  *
4339  * Return nothing.
4340  */
4341 static void
4342 _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4343     Mpi2EventIrConfigElement_t *element)
4344 {
4345         struct _sas_device *sas_device;
4346         unsigned long flags;
4347         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4348
4349         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4350         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4351         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4352         if (!sas_device)
4353                 return;
4354         _scsih_remove_device(ioc, handle);
4355 }
4356
4357 /**
4358  * _scsih_sas_pd_add - remove pd component
4359  * @ioc: per adapter object
4360  * @element: IR config element data
4361  * Context: user.
4362  *
4363  * Return nothing.
4364  */
4365 static void
4366 _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4367     Mpi2EventIrConfigElement_t *element)
4368 {
4369         struct _sas_device *sas_device;
4370         unsigned long flags;
4371         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4372         Mpi2ConfigReply_t mpi_reply;
4373         Mpi2SasDevicePage0_t sas_device_pg0;
4374         u32 ioc_status;
4375
4376         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4377         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4378         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4379         if (sas_device) {
4380                 sas_device->hidden_raid_component = 1;
4381                 return;
4382         }
4383
4384         if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4385             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4386                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4387                     ioc->name, __FILE__, __LINE__, __func__);
4388                 return;
4389         }
4390
4391         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4392             MPI2_IOCSTATUS_MASK;
4393         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4394                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4395                     ioc->name, __FILE__, __LINE__, __func__);
4396                 return;
4397         }
4398
4399         mpt2sas_transport_update_links(ioc,
4400             le16_to_cpu(sas_device_pg0.ParentDevHandle),
4401             handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4402
4403         _scsih_add_device(ioc, handle, 0, 1);
4404 }
4405
4406 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4407 /**
4408  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4409  * @ioc: per adapter object
4410  * @event_data: event data payload
4411  * Context: user.
4412  *
4413  * Return nothing.
4414  */
4415 static void
4416 _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4417     Mpi2EventDataIrConfigChangeList_t *event_data)
4418 {
4419         Mpi2EventIrConfigElement_t *element;
4420         u8 element_type;
4421         int i;
4422         char *reason_str = NULL, *element_str = NULL;
4423
4424         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4425
4426         printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4427             ioc->name, (le32_to_cpu(event_data->Flags) &
4428             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4429             "foreign" : "native", event_data->NumElements);
4430         for (i = 0; i < event_data->NumElements; i++, element++) {
4431                 switch (element->ReasonCode) {
4432                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4433                         reason_str = "add";
4434                         break;
4435                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4436                         reason_str = "remove";
4437                         break;
4438                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4439                         reason_str = "no change";
4440                         break;
4441                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4442                         reason_str = "hide";
4443                         break;
4444                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4445                         reason_str = "unhide";
4446                         break;
4447                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4448                         reason_str = "volume_created";
4449                         break;
4450                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4451                         reason_str = "volume_deleted";
4452                         break;
4453                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4454                         reason_str = "pd_created";
4455                         break;
4456                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4457                         reason_str = "pd_deleted";
4458                         break;
4459                 default:
4460                         reason_str = "unknown reason";
4461                         break;
4462                 }
4463                 element_type = le16_to_cpu(element->ElementFlags) &
4464                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4465                 switch (element_type) {
4466                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4467                         element_str = "volume";
4468                         break;
4469                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4470                         element_str = "phys disk";
4471                         break;
4472                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4473                         element_str = "hot spare";
4474                         break;
4475                 default:
4476                         element_str = "unknown element";
4477                         break;
4478                 }
4479                 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4480                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4481                     reason_str, le16_to_cpu(element->VolDevHandle),
4482                     le16_to_cpu(element->PhysDiskDevHandle),
4483                     element->PhysDiskNum);
4484         }
4485 }
4486 #endif
4487
4488 /**
4489  * _scsih_sas_ir_config_change_event - handle ir configuration change events
4490  * @ioc: per adapter object
4491  * @VF_ID:
4492  * @event_data: event data payload
4493  * Context: user.
4494  *
4495  * Return nothing.
4496  */
4497 static void
4498 _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4499     Mpi2EventDataIrConfigChangeList_t *event_data)
4500 {
4501         Mpi2EventIrConfigElement_t *element;
4502         int i;
4503         u8 foreign_config;
4504
4505 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4506         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4507                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4508
4509 #endif
4510         foreign_config = (le32_to_cpu(event_data->Flags) &
4511             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
4512
4513         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4514         for (i = 0; i < event_data->NumElements; i++, element++) {
4515
4516                 switch (element->ReasonCode) {
4517                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4518                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4519                         if (!foreign_config)
4520                                 _scsih_sas_volume_add(ioc, element);
4521                         break;
4522                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4523                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4524                         if (!foreign_config)
4525                                 _scsih_sas_volume_delete(ioc, element);
4526                         break;
4527                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4528                         _scsih_sas_pd_hide(ioc, element);
4529                         break;
4530                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4531                         _scsih_sas_pd_expose(ioc, element);
4532                         break;
4533                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4534                         _scsih_sas_pd_add(ioc, element);
4535                         break;
4536                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4537                         _scsih_sas_pd_delete(ioc, element);
4538                         break;
4539                 }
4540         }
4541 }
4542
4543 /**
4544  * _scsih_sas_ir_volume_event - IR volume event
4545  * @ioc: per adapter object
4546  * @event_data: event data payload
4547  * Context: user.
4548  *
4549  * Return nothing.
4550  */
4551 static void
4552 _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4553     Mpi2EventDataIrVolume_t *event_data)
4554 {
4555         u64 wwid;
4556         unsigned long flags;
4557         struct _raid_device *raid_device;
4558         u16 handle;
4559         u32 state;
4560         int rc;
4561         struct MPT2SAS_TARGET *sas_target_priv_data;
4562
4563         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4564                 return;
4565
4566         handle = le16_to_cpu(event_data->VolDevHandle);
4567         state = le32_to_cpu(event_data->NewValue);
4568         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4569             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4570             le32_to_cpu(event_data->PreviousValue), state));
4571
4572         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4573         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4574         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4575
4576         switch (state) {
4577         case MPI2_RAID_VOL_STATE_MISSING:
4578         case MPI2_RAID_VOL_STATE_FAILED:
4579                 if (!raid_device)
4580                         break;
4581                 if (raid_device->starget) {
4582                         sas_target_priv_data = raid_device->starget->hostdata;
4583                         sas_target_priv_data->deleted = 1;
4584                         scsi_remove_target(&raid_device->starget->dev);
4585                 }
4586                 _scsih_raid_device_remove(ioc, raid_device);
4587                 break;
4588
4589         case MPI2_RAID_VOL_STATE_ONLINE:
4590         case MPI2_RAID_VOL_STATE_DEGRADED:
4591         case MPI2_RAID_VOL_STATE_OPTIMAL:
4592                 if (raid_device)
4593                         break;
4594
4595                 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4596                 if (!wwid) {
4597                         printk(MPT2SAS_ERR_FMT
4598                             "failure at %s:%d/%s()!\n", ioc->name,
4599                             __FILE__, __LINE__, __func__);
4600                         break;
4601                 }
4602
4603                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4604                 if (!raid_device) {
4605                         printk(MPT2SAS_ERR_FMT
4606                             "failure at %s:%d/%s()!\n", ioc->name,
4607                             __FILE__, __LINE__, __func__);
4608                         break;
4609                 }
4610
4611                 raid_device->id = ioc->sas_id++;
4612                 raid_device->channel = RAID_CHANNEL;
4613                 raid_device->handle = handle;
4614                 raid_device->wwid = wwid;
4615                 _scsih_raid_device_add(ioc, raid_device);
4616                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4617                     raid_device->id, 0);
4618                 if (rc)
4619                         _scsih_raid_device_remove(ioc, raid_device);
4620                 break;
4621
4622         case MPI2_RAID_VOL_STATE_INITIALIZING:
4623         default:
4624                 break;
4625         }
4626 }
4627
4628 /**
4629  * _scsih_sas_ir_physical_disk_event - PD event
4630  * @ioc: per adapter object
4631  * @event_data: event data payload
4632  * Context: user.
4633  *
4634  * Return nothing.
4635  */
4636 static void
4637 _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4638    Mpi2EventDataIrPhysicalDisk_t *event_data)
4639 {
4640         u16 handle;
4641         u32 state;
4642         struct _sas_device *sas_device;
4643         unsigned long flags;
4644         Mpi2ConfigReply_t mpi_reply;
4645         Mpi2SasDevicePage0_t sas_device_pg0;
4646         u32 ioc_status;
4647
4648         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4649                 return;
4650
4651         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4652         state = le32_to_cpu(event_data->NewValue);
4653
4654         dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4655             "old(0x%08x), new(0x%08x)\n", ioc->name, __func__,  handle,
4656             le32_to_cpu(event_data->PreviousValue), state));
4657
4658         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4659         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4660         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4661
4662         switch (state) {
4663         case MPI2_RAID_PD_STATE_ONLINE:
4664         case MPI2_RAID_PD_STATE_DEGRADED:
4665         case MPI2_RAID_PD_STATE_REBUILDING:
4666         case MPI2_RAID_PD_STATE_OPTIMAL:
4667                 if (sas_device) {
4668                         sas_device->hidden_raid_component = 1;
4669                         return;
4670                 }
4671
4672                 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4673                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4674                     handle))) {
4675                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4676                             ioc->name, __FILE__, __LINE__, __func__);
4677                         return;
4678                 }
4679
4680                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4681                     MPI2_IOCSTATUS_MASK;
4682                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4683                         printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4684                             ioc->name, __FILE__, __LINE__, __func__);
4685                         return;
4686                 }
4687
4688                 mpt2sas_transport_update_links(ioc,
4689                     le16_to_cpu(sas_device_pg0.ParentDevHandle),
4690                     handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4691
4692                 _scsih_add_device(ioc, handle, 0, 1);
4693
4694                 break;
4695
4696         case MPI2_RAID_PD_STATE_OFFLINE:
4697         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4698         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4699         case MPI2_RAID_PD_STATE_HOT_SPARE:
4700         default:
4701                 break;
4702         }
4703 }
4704
4705 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4706 /**
4707  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4708  * @ioc: per adapter object
4709  * @event_data: event data payload
4710  * Context: user.
4711  *
4712  * Return nothing.
4713  */
4714 static void
4715 _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4716     Mpi2EventDataIrOperationStatus_t *event_data)
4717 {
4718         char *reason_str = NULL;
4719
4720         switch (event_data->RAIDOperation) {
4721         case MPI2_EVENT_IR_RAIDOP_RESYNC:
4722                 reason_str = "resync";
4723                 break;
4724         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4725                 reason_str = "online capacity expansion";
4726                 break;
4727         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4728                 reason_str = "consistency check";
4729                 break;
4730         default:
4731                 reason_str = "unknown reason";
4732                 break;
4733         }
4734
4735         printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4736             "\thandle(0x%04x), percent complete(%d)\n",
4737             ioc->name, reason_str,
4738             le16_to_cpu(event_data->VolDevHandle),
4739             event_data->PercentComplete);
4740 }
4741 #endif
4742
4743 /**
4744  * _scsih_sas_ir_operation_status_event - handle RAID operation events
4745  * @ioc: per adapter object
4746  * @VF_ID:
4747  * @event_data: event data payload
4748  * Context: user.
4749  *
4750  * Return nothing.
4751  */
4752 static void
4753 _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4754     Mpi2EventDataIrOperationStatus_t *event_data)
4755 {
4756 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4757         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4758                 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4759 #endif
4760 }
4761
4762 /**
4763  * _scsih_task_set_full - handle task set full
4764  * @ioc: per adapter object
4765  * @event_data: event data payload
4766  * Context: user.
4767  *
4768  * Throttle back qdepth.
4769  */
4770 static void
4771 _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4772     Mpi2EventDataTaskSetFull_t *event_data)
4773 {
4774         unsigned long flags;
4775         struct _sas_device *sas_device;
4776         static struct _raid_device *raid_device;
4777         struct scsi_device *sdev;
4778         int depth;
4779         u16 current_depth;
4780         u16 handle;
4781         int id, channel;
4782         u64 sas_address;
4783
4784         current_depth = le16_to_cpu(event_data->CurrentDepth);
4785         handle = le16_to_cpu(event_data->DevHandle);
4786         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4787         sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4788         if (!sas_device) {
4789                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4790                 return;
4791         }
4792         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4793         id = sas_device->id;
4794         channel = sas_device->channel;
4795         sas_address = sas_device->sas_address;
4796
4797         /* if hidden raid component, then change to volume characteristics */
4798         if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4799                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4800                 raid_device = _scsih_raid_device_find_by_handle(
4801                     ioc, sas_device->volume_handle);
4802                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4803                 if (raid_device) {
4804                         id = raid_device->id;
4805                         channel = raid_device->channel;
4806                         handle = raid_device->handle;
4807                         sas_address = raid_device->wwid;
4808                 }
4809         }
4810
4811         if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4812                 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4813                     "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4814                     handle, (unsigned long long)sas_address, current_depth);
4815
4816         shost_for_each_device(sdev, ioc->shost) {
4817                 if (sdev->id == id && sdev->channel == channel) {
4818                         if (current_depth > sdev->queue_depth) {
4819                                 if (ioc->logging_level &
4820                                     MPT_DEBUG_TASK_SET_FULL)
4821                                         sdev_printk(KERN_INFO, sdev, "strange "
4822                                             "observation, the queue depth is"
4823                                             " (%d) meanwhile fw queue depth "
4824                                             "is (%d)\n", sdev->queue_depth,
4825                                             current_depth);
4826                                 continue;
4827                         }
4828                         depth = scsi_track_queue_full(sdev,
4829                             current_depth - 1);
4830                         if (depth > 0)
4831                                 sdev_printk(KERN_INFO, sdev, "Queue depth "
4832                                     "reduced to (%d)\n", depth);
4833                         else if (depth < 0)
4834                                 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4835                                     "Queueing is being disabled\n");
4836                         else if (depth == 0)
4837                                 if (ioc->logging_level &
4838                                      MPT_DEBUG_TASK_SET_FULL)
4839                                         sdev_printk(KERN_INFO, sdev,
4840                                              "Queue depth not changed yet\n");
4841                 }
4842         }
4843 }
4844
4845 /**
4846  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4847  * @ioc: per adapter object
4848  * @sas_address: sas address
4849  * @slot: enclosure slot id
4850  * @handle: device handle
4851  *
4852  * After host reset, find out whether devices are still responding.
4853  * Used in _scsi_remove_unresponsive_sas_devices.
4854  *
4855  * Return nothing.
4856  */
4857 static void
4858 _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4859     u16 slot, u16 handle)
4860 {
4861         struct MPT2SAS_TARGET *sas_target_priv_data;
4862         struct scsi_target *starget;
4863         struct _sas_device *sas_device;
4864         unsigned long flags;
4865
4866         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4867         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4868                 if (sas_device->sas_address == sas_address &&
4869                     sas_device->slot == slot && sas_device->starget) {
4870                         sas_device->responding = 1;
4871                         starget_printk(KERN_INFO, sas_device->starget,
4872                             "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4873                             "logical id(0x%016llx), slot(%d)\n", handle,
4874                             (unsigned long long)sas_device->sas_address,
4875                             (unsigned long long)
4876                             sas_device->enclosure_logical_id,
4877                             sas_device->slot);
4878                         if (sas_device->handle == handle)
4879                                 goto out;
4880                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4881                             sas_device->handle);
4882                         sas_device->handle = handle;
4883                         starget = sas_device->starget;
4884                         sas_target_priv_data = starget->hostdata;
4885                         sas_target_priv_data->handle = handle;
4886                         goto out;
4887                 }
4888         }
4889  out:
4890         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4891 }
4892
4893 /**
4894  * _scsih_search_responding_sas_devices -
4895  * @ioc: per adapter object
4896  *
4897  * After host reset, find out whether devices are still responding.
4898  * If not remove.
4899  *
4900  * Return nothing.
4901  */
4902 static void
4903 _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4904 {
4905         Mpi2SasDevicePage0_t sas_device_pg0;
4906         Mpi2ConfigReply_t mpi_reply;
4907         u16 ioc_status;
4908         __le64 sas_address;
4909         u16 handle;
4910         u32 device_info;
4911         u16 slot;
4912
4913         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4914
4915         if (list_empty(&ioc->sas_device_list))
4916                 return;
4917
4918         handle = 0xFFFF;
4919         while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4920             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4921             handle))) {
4922                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4923                     MPI2_IOCSTATUS_MASK;
4924                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4925                         break;
4926                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4927                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4928                 if (!(_scsih_is_end_device(device_info)))
4929                         continue;
4930                 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4931                 slot = le16_to_cpu(sas_device_pg0.Slot);
4932                 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4933                     handle);
4934         }
4935 }
4936
4937 /**
4938  * _scsih_mark_responding_raid_device - mark a raid_device as responding
4939  * @ioc: per adapter object
4940  * @wwid: world wide identifier for raid volume
4941  * @handle: device handle
4942  *
4943  * After host reset, find out whether devices are still responding.
4944  * Used in _scsi_remove_unresponsive_raid_devices.
4945  *
4946  * Return nothing.
4947  */
4948 static void
4949 _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4950     u16 handle)
4951 {
4952         struct MPT2SAS_TARGET *sas_target_priv_data;
4953         struct scsi_target *starget;
4954         struct _raid_device *raid_device;
4955         unsigned long flags;
4956
4957         spin_lock_irqsave(&ioc->raid_device_lock, flags);
4958         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4959                 if (raid_device->wwid == wwid && raid_device->starget) {
4960                         raid_device->responding = 1;
4961                         starget_printk(KERN_INFO, raid_device->starget,
4962                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
4963                             (unsigned long long)raid_device->wwid);
4964                         if (raid_device->handle == handle)
4965                                 goto out;
4966                         printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4967                             raid_device->handle);
4968                         raid_device->handle = handle;
4969                         starget = raid_device->starget;
4970                         sas_target_priv_data = starget->hostdata;
4971                         sas_target_priv_data->handle = handle;
4972                         goto out;
4973                 }
4974         }
4975  out:
4976         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4977 }
4978
4979 /**
4980  * _scsih_search_responding_raid_devices -
4981  * @ioc: per adapter object
4982  *
4983  * After host reset, find out whether devices are still responding.
4984  * If not remove.
4985  *
4986  * Return nothing.
4987  */
4988 static void
4989 _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4990 {
4991         Mpi2RaidVolPage1_t volume_pg1;
4992         Mpi2ConfigReply_t mpi_reply;
4993         u16 ioc_status;
4994         u16 handle;
4995
4996         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4997
4998         if (list_empty(&ioc->raid_device_list))
4999                 return;
5000
5001         handle = 0xFFFF;
5002         while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5003             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5004                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5005                     MPI2_IOCSTATUS_MASK;
5006                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5007                         break;
5008                 handle = le16_to_cpu(volume_pg1.DevHandle);
5009                 _scsih_mark_responding_raid_device(ioc,
5010                     le64_to_cpu(volume_pg1.WWID), handle);
5011         }
5012 }
5013
5014 /**
5015  * _scsih_mark_responding_expander - mark a expander as responding
5016  * @ioc: per adapter object
5017  * @sas_address: sas address
5018  * @handle:
5019  *
5020  * After host reset, find out whether devices are still responding.
5021  * Used in _scsi_remove_unresponsive_expanders.
5022  *
5023  * Return nothing.
5024  */
5025 static void
5026 _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5027      u16 handle)
5028 {
5029         struct _sas_node *sas_expander;
5030         unsigned long flags;
5031
5032         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5033         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5034                 if (sas_expander->sas_address == sas_address) {
5035                         sas_expander->responding = 1;
5036                         if (sas_expander->handle != handle) {
5037                                 printk(KERN_INFO "old handle(0x%04x)\n",
5038                                     sas_expander->handle);
5039                                 sas_expander->handle = handle;
5040                         }
5041                         goto out;
5042                 }
5043         }
5044  out:
5045         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5046 }
5047
5048 /**
5049  * _scsih_search_responding_expanders -
5050  * @ioc: per adapter object
5051  *
5052  * After host reset, find out whether devices are still responding.
5053  * If not remove.
5054  *
5055  * Return nothing.
5056  */
5057 static void
5058 _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5059 {
5060         Mpi2ExpanderPage0_t expander_pg0;
5061         Mpi2ConfigReply_t mpi_reply;
5062         u16 ioc_status;
5063         __le64 sas_address;
5064         u16 handle;
5065
5066         printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5067
5068         if (list_empty(&ioc->sas_expander_list))
5069                 return;
5070
5071         handle = 0xFFFF;
5072         while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5073             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5074
5075                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5076                     MPI2_IOCSTATUS_MASK;
5077                 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5078                         break;
5079
5080                 handle = le16_to_cpu(expander_pg0.DevHandle);
5081                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5082                 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5083                     "sas_addr(0x%016llx)\n", handle,
5084                     (unsigned long long)sas_address);
5085                 _scsih_mark_responding_expander(ioc, sas_address, handle);
5086         }
5087
5088 }
5089
5090 /**
5091  * _scsih_remove_unresponding_devices - removing unresponding devices
5092  * @ioc: per adapter object
5093  *
5094  * Return nothing.
5095  */
5096 static void
5097 _scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5098 {
5099         struct _sas_device *sas_device, *sas_device_next;
5100         struct _sas_node *sas_expander;
5101         struct _raid_device *raid_device, *raid_device_next;
5102
5103
5104         list_for_each_entry_safe(sas_device, sas_device_next,
5105             &ioc->sas_device_list, list) {
5106                 if (sas_device->responding) {
5107                         sas_device->responding = 0;
5108                         continue;
5109                 }
5110                 if (sas_device->starget)
5111                         starget_printk(KERN_INFO, sas_device->starget,
5112                             "removing: handle(0x%04x), sas_addr(0x%016llx), "
5113                             "enclosure logical id(0x%016llx), slot(%d)\n",
5114                             sas_device->handle,
5115                             (unsigned long long)sas_device->sas_address,
5116                             (unsigned long long)
5117                             sas_device->enclosure_logical_id,
5118                             sas_device->slot);
5119                 _scsih_remove_device(ioc, sas_device->handle);
5120         }
5121
5122         list_for_each_entry_safe(raid_device, raid_device_next,
5123             &ioc->raid_device_list, list) {
5124                 if (raid_device->responding) {
5125                         raid_device->responding = 0;
5126                         continue;
5127                 }
5128                 if (raid_device->starget) {
5129                         starget_printk(KERN_INFO, raid_device->starget,
5130                             "removing: handle(0x%04x), wwid(0x%016llx)\n",
5131                               raid_device->handle,
5132                             (unsigned long long)raid_device->wwid);
5133                         scsi_remove_target(&raid_device->starget->dev);
5134                 }
5135                 _scsih_raid_device_remove(ioc, raid_device);
5136         }
5137
5138  retry_expander_search:
5139         sas_expander = NULL;
5140         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5141                 if (sas_expander->responding) {
5142                         sas_expander->responding = 0;
5143                         continue;
5144                 }
5145                 _scsih_expander_remove(ioc, sas_expander->handle);
5146                 goto retry_expander_search;
5147         }
5148 }
5149
5150 /**
5151  * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5152  * @ioc: per adapter object
5153  * @reset_phase: phase
5154  *
5155  * The handler for doing any required cleanup or initialization.
5156  *
5157  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5158  * MPT2_IOC_DONE_RESET
5159  *
5160  * Return nothing.
5161  */
5162 void
5163 mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5164 {
5165         switch (reset_phase) {
5166         case MPT2_IOC_PRE_RESET:
5167                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5168                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5169                 _scsih_fw_event_off(ioc);
5170                 break;
5171         case MPT2_IOC_AFTER_RESET:
5172                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5173                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5174                 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5175                         ioc->tm_cmds.status |= MPT2_CMD_RESET;
5176                         mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5177                         complete(&ioc->tm_cmds.done);
5178                 }
5179                 _scsih_fw_event_on(ioc);
5180                 _scsih_flush_running_cmds(ioc);
5181                 break;
5182         case MPT2_IOC_DONE_RESET:
5183                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5184                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5185                 _scsih_sas_host_refresh(ioc, 0);
5186                 _scsih_search_responding_sas_devices(ioc);
5187                 _scsih_search_responding_raid_devices(ioc);
5188                 _scsih_search_responding_expanders(ioc);
5189                 break;
5190         case MPT2_IOC_RUNNING:
5191                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5192                     "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5193                 _scsih_remove_unresponding_devices(ioc);
5194                 break;
5195         }
5196 }
5197
5198 /**
5199  * _firmware_event_work - delayed task for processing firmware events
5200  * @ioc: per adapter object
5201  * @work: equal to the fw_event_work object
5202  * Context: user.
5203  *
5204  * Return nothing.
5205  */
5206 static void
5207 _firmware_event_work(struct work_struct *work)
5208 {
5209         struct fw_event_work *fw_event = container_of(work,
5210             struct fw_event_work, work);
5211         unsigned long flags;
5212         struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5213
5214         /* the queue is being flushed so ignore this event */
5215         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5216         if (ioc->fw_events_off || ioc->remove_host) {
5217                 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5218                 _scsih_fw_event_free(ioc, fw_event);
5219                 return;
5220         }
5221         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5222
5223         if (ioc->shost_recovery) {
5224                 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5225                 return;
5226         }
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                         if (ioc->shost_recovery)
5429                                 return;
5430                         goto retry_device_search;
5431                 }
5432         }
5433
5434  retry_expander_search:
5435         list_for_each_entry(mpt2sas_port,
5436            &sas_expander->sas_port_list, port_list) {
5437
5438                 if (mpt2sas_port->remote_identify.device_type ==
5439                     MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5440                     mpt2sas_port->remote_identify.device_type ==
5441                     MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5442
5443                         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5444                         expander_sibling =
5445                             mpt2sas_scsih_expander_find_by_sas_address(
5446                             ioc, mpt2sas_port->remote_identify.sas_address);
5447                         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5448                         if (!expander_sibling)
5449                                 continue;
5450                         _scsih_expander_remove(ioc, expander_sibling->handle);
5451                         if (ioc->shost_recovery)
5452                                 return;
5453                         goto retry_expander_search;
5454                 }
5455         }
5456
5457         mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5458             sas_expander->parent_handle);
5459
5460         printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5461            "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5462             sas_expander->handle, (unsigned long long)
5463             sas_expander->sas_address);
5464
5465         list_del(&sas_expander->list);
5466         kfree(sas_expander->phy);
5467         kfree(sas_expander);
5468 }
5469
5470 /**
5471  * _scsih_remove - detach and remove add host
5472  * @pdev: PCI device struct
5473  *
5474  * Return nothing.
5475  */
5476 static void __devexit
5477 _scsih_remove(struct pci_dev *pdev)
5478 {
5479         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5480         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5481         struct _sas_port *mpt2sas_port;
5482         struct _sas_device *sas_device;
5483         struct _sas_node *expander_sibling;
5484         struct workqueue_struct *wq;
5485         unsigned long flags;
5486
5487         ioc->remove_host = 1;
5488         _scsih_fw_event_off(ioc);
5489
5490         spin_lock_irqsave(&ioc->fw_event_lock, flags);
5491         wq = ioc->firmware_event_thread;
5492         ioc->firmware_event_thread = NULL;
5493         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5494         if (wq)
5495                 destroy_workqueue(wq);
5496
5497         /* free ports attached to the sas_host */
5498  retry_again:
5499         list_for_each_entry(mpt2sas_port,
5500            &ioc->sas_hba.sas_port_list, port_list) {
5501                 if (mpt2sas_port->remote_identify.device_type ==
5502                     SAS_END_DEVICE) {
5503                         sas_device =
5504                             mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5505                            mpt2sas_port->remote_identify.sas_address);
5506                         if (sas_device) {
5507                                 _scsih_remove_device(ioc, sas_device->handle);
5508                                 goto retry_again;
5509                         }
5510                 } else {
5511                         expander_sibling =
5512                             mpt2sas_scsih_expander_find_by_sas_address(ioc,
5513                             mpt2sas_port->remote_identify.sas_address);
5514                         if (expander_sibling) {
5515                                 _scsih_expander_remove(ioc,
5516                                     expander_sibling->handle);
5517                                 goto retry_again;
5518                         }
5519                 }
5520         }
5521
5522         /* free phys attached to the sas_host */
5523         if (ioc->sas_hba.num_phys) {
5524                 kfree(ioc->sas_hba.phy);
5525                 ioc->sas_hba.phy = NULL;
5526                 ioc->sas_hba.num_phys = 0;
5527         }
5528
5529         sas_remove_host(shost);
5530         mpt2sas_base_detach(ioc);
5531         list_del(&ioc->list);
5532         scsi_remove_host(shost);
5533         scsi_host_put(shost);
5534 }
5535
5536 /**
5537  * _scsih_probe_boot_devices - reports 1st device
5538  * @ioc: per adapter object
5539  *
5540  * If specified in bios page 2, this routine reports the 1st
5541  * device scsi-ml or sas transport for persistent boot device
5542  * purposes.  Please refer to function _scsih_determine_boot_device()
5543  */
5544 static void
5545 _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5546 {
5547         u8 is_raid;
5548         void *device;
5549         struct _sas_device *sas_device;
5550         struct _raid_device *raid_device;
5551         u16 handle, parent_handle;
5552         u64 sas_address;
5553         unsigned long flags;
5554         int rc;
5555
5556         device = NULL;
5557         if (ioc->req_boot_device.device) {
5558                 device =  ioc->req_boot_device.device;
5559                 is_raid = ioc->req_boot_device.is_raid;
5560         } else if (ioc->req_alt_boot_device.device) {
5561                 device =  ioc->req_alt_boot_device.device;
5562                 is_raid = ioc->req_alt_boot_device.is_raid;
5563         } else if (ioc->current_boot_device.device) {
5564                 device =  ioc->current_boot_device.device;
5565                 is_raid = ioc->current_boot_device.is_raid;
5566         }
5567
5568         if (!device)
5569                 return;
5570
5571         if (is_raid) {
5572                 raid_device = device;
5573                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5574                     raid_device->id, 0);
5575                 if (rc)
5576                         _scsih_raid_device_remove(ioc, raid_device);
5577         } else {
5578                 sas_device = device;
5579                 handle = sas_device->handle;
5580                 parent_handle = sas_device->parent_handle;
5581                 sas_address = sas_device->sas_address;
5582                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5583                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5584                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5585                 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5586                     sas_device->parent_handle)) {
5587                         _scsih_sas_device_remove(ioc, sas_device);
5588                 } else if (!sas_device->starget) {
5589                         mpt2sas_transport_port_remove(ioc, sas_address,
5590                             parent_handle);
5591                         _scsih_sas_device_remove(ioc, sas_device);
5592                 }
5593         }
5594 }
5595
5596 /**
5597  * _scsih_probe_raid - reporting raid volumes to scsi-ml
5598  * @ioc: per adapter object
5599  *
5600  * Called during initial loading of the driver.
5601  */
5602 static void
5603 _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5604 {
5605         struct _raid_device *raid_device, *raid_next;
5606         int rc;
5607
5608         list_for_each_entry_safe(raid_device, raid_next,
5609             &ioc->raid_device_list, list) {
5610                 if (raid_device->starget)
5611                         continue;
5612                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5613                     raid_device->id, 0);
5614                 if (rc)
5615                         _scsih_raid_device_remove(ioc, raid_device);
5616         }
5617 }
5618
5619 /**
5620  * _scsih_probe_sas - reporting raid volumes to sas transport
5621  * @ioc: per adapter object
5622  *
5623  * Called during initial loading of the driver.
5624  */
5625 static void
5626 _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5627 {
5628         struct _sas_device *sas_device, *next;
5629         unsigned long flags;
5630         u16 handle, parent_handle;
5631         u64 sas_address;
5632
5633         /* SAS Device List */
5634         list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5635             list) {
5636                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5637                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5638                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5639
5640                 handle = sas_device->handle;
5641                 parent_handle = sas_device->parent_handle;
5642                 sas_address = sas_device->sas_address;
5643                 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5644                         _scsih_sas_device_remove(ioc, sas_device);
5645                 } else if (!sas_device->starget) {
5646                         mpt2sas_transport_port_remove(ioc, sas_address,
5647                             parent_handle);
5648                         _scsih_sas_device_remove(ioc, sas_device);
5649                 }
5650         }
5651 }
5652
5653 /**
5654  * _scsih_probe_devices - probing for devices
5655  * @ioc: per adapter object
5656  *
5657  * Called during initial loading of the driver.
5658  */
5659 static void
5660 _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5661 {
5662         u16 volume_mapping_flags =
5663             le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5664             MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5665
5666         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5667                 return;  /* return when IOC doesn't support initiator mode */
5668
5669         _scsih_probe_boot_devices(ioc);
5670
5671         if (ioc->ir_firmware) {
5672                 if ((volume_mapping_flags &
5673                      MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5674                         _scsih_probe_sas(ioc);
5675                         _scsih_probe_raid(ioc);
5676                 } else {
5677                         _scsih_probe_raid(ioc);
5678                         _scsih_probe_sas(ioc);
5679                 }
5680         } else
5681                 _scsih_probe_sas(ioc);
5682 }
5683
5684 /**
5685  * _scsih_probe - attach and add scsi host
5686  * @pdev: PCI device struct
5687  * @id: pci device id
5688  *
5689  * Returns 0 success, anything else error.
5690  */
5691 static int
5692 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5693 {
5694         struct MPT2SAS_ADAPTER *ioc;
5695         struct Scsi_Host *shost;
5696
5697         shost = scsi_host_alloc(&scsih_driver_template,
5698             sizeof(struct MPT2SAS_ADAPTER));
5699         if (!shost)
5700                 return -ENODEV;
5701
5702         /* init local params */
5703         ioc = shost_priv(shost);
5704         memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5705         INIT_LIST_HEAD(&ioc->list);
5706         list_add_tail(&ioc->list, &mpt2sas_ioc_list);
5707         ioc->shost = shost;
5708         ioc->id = mpt_ids++;
5709         sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5710         ioc->pdev = pdev;
5711         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5712         ioc->tm_cb_idx = tm_cb_idx;
5713         ioc->ctl_cb_idx = ctl_cb_idx;
5714         ioc->base_cb_idx = base_cb_idx;
5715         ioc->transport_cb_idx = transport_cb_idx;
5716         ioc->config_cb_idx = config_cb_idx;
5717         ioc->logging_level = logging_level;
5718         /* misc semaphores and spin locks */
5719         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5720         spin_lock_init(&ioc->scsi_lookup_lock);
5721         spin_lock_init(&ioc->sas_device_lock);
5722         spin_lock_init(&ioc->sas_node_lock);
5723         spin_lock_init(&ioc->fw_event_lock);
5724         spin_lock_init(&ioc->raid_device_lock);
5725
5726         INIT_LIST_HEAD(&ioc->sas_device_list);
5727         INIT_LIST_HEAD(&ioc->sas_device_init_list);
5728         INIT_LIST_HEAD(&ioc->sas_expander_list);
5729         INIT_LIST_HEAD(&ioc->fw_event_list);
5730         INIT_LIST_HEAD(&ioc->raid_device_list);
5731         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5732
5733         /* init shost parameters */
5734         shost->max_cmd_len = 16;
5735         shost->max_lun = max_lun;
5736         shost->transportt = mpt2sas_transport_template;
5737         shost->unique_id = ioc->id;
5738
5739         if ((scsi_add_host(shost, &pdev->dev))) {
5740                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5741                     ioc->name, __FILE__, __LINE__, __func__);
5742                 list_del(&ioc->list);
5743                 goto out_add_shost_fail;
5744         }
5745
5746         scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5747             | SHOST_DIF_TYPE3_PROTECTION);
5748
5749         /* event thread */
5750         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5751             "fw_event%d", ioc->id);
5752         ioc->firmware_event_thread = create_singlethread_workqueue(
5753             ioc->firmware_event_name);
5754         if (!ioc->firmware_event_thread) {
5755                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5756                     ioc->name, __FILE__, __LINE__, __func__);
5757                 goto out_thread_fail;
5758         }
5759
5760         ioc->wait_for_port_enable_to_complete = 1;
5761         if ((mpt2sas_base_attach(ioc))) {
5762                 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5763                     ioc->name, __FILE__, __LINE__, __func__);
5764                 goto out_attach_fail;
5765         }
5766
5767         ioc->wait_for_port_enable_to_complete = 0;
5768         _scsih_probe_devices(ioc);
5769         return 0;
5770
5771  out_attach_fail:
5772         destroy_workqueue(ioc->firmware_event_thread);
5773  out_thread_fail:
5774         list_del(&ioc->list);
5775         scsi_remove_host(shost);
5776  out_add_shost_fail:
5777         return -ENODEV;
5778 }
5779
5780 #ifdef CONFIG_PM
5781 /**
5782  * _scsih_suspend - power management suspend main entry point
5783  * @pdev: PCI device struct
5784  * @state: PM state change to (usually PCI_D3)
5785  *
5786  * Returns 0 success, anything else error.
5787  */
5788 static int
5789 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
5790 {
5791         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5792         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5793         u32 device_state;
5794
5795         mpt2sas_base_stop_watchdog(ioc);
5796         flush_scheduled_work();
5797         scsi_block_requests(shost);
5798         device_state = pci_choose_state(pdev, state);
5799         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5800             "operating state [D%d]\n", ioc->name, pdev,
5801             pci_name(pdev), device_state);
5802
5803         mpt2sas_base_free_resources(ioc);
5804         pci_save_state(pdev);
5805         pci_disable_device(pdev);
5806         pci_set_power_state(pdev, device_state);
5807         return 0;
5808 }
5809
5810 /**
5811  * _scsih_resume - power management resume main entry point
5812  * @pdev: PCI device struct
5813  *
5814  * Returns 0 success, anything else error.
5815  */
5816 static int
5817 _scsih_resume(struct pci_dev *pdev)
5818 {
5819         struct Scsi_Host *shost = pci_get_drvdata(pdev);
5820         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5821         u32 device_state = pdev->current_state;
5822         int r;
5823
5824         printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5825             "operating state [D%d]\n", ioc->name, pdev,
5826             pci_name(pdev), device_state);
5827
5828         pci_set_power_state(pdev, PCI_D0);
5829         pci_enable_wake(pdev, PCI_D0, 0);
5830         pci_restore_state(pdev);
5831         ioc->pdev = pdev;
5832         r = mpt2sas_base_map_resources(ioc);
5833         if (r)
5834                 return r;
5835
5836         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5837         scsi_unblock_requests(shost);
5838         mpt2sas_base_start_watchdog(ioc);
5839         return 0;
5840 }
5841 #endif /* CONFIG_PM */
5842
5843
5844 static struct pci_driver scsih_driver = {
5845         .name           = MPT2SAS_DRIVER_NAME,
5846         .id_table       = scsih_pci_table,
5847         .probe          = _scsih_probe,
5848         .remove         = __devexit_p(_scsih_remove),
5849 #ifdef CONFIG_PM
5850         .suspend        = _scsih_suspend,
5851         .resume         = _scsih_resume,
5852 #endif
5853 };
5854
5855
5856 /**
5857  * _scsih_init - main entry point for this driver.
5858  *
5859  * Returns 0 success, anything else error.
5860  */
5861 static int __init
5862 _scsih_init(void)
5863 {
5864         int error;
5865
5866         mpt_ids = 0;
5867         printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5868             MPT2SAS_DRIVER_VERSION);
5869
5870         mpt2sas_transport_template =
5871             sas_attach_transport(&mpt2sas_transport_functions);
5872         if (!mpt2sas_transport_template)
5873                 return -ENODEV;
5874
5875         mpt2sas_base_initialize_callback_handler();
5876
5877          /* queuecommand callback hander */
5878         scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
5879
5880         /* task managment callback handler */
5881         tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
5882
5883         /* base internal commands callback handler */
5884         base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5885
5886         /* transport internal commands callback handler */
5887         transport_cb_idx = mpt2sas_base_register_callback_handler(
5888             mpt2sas_transport_done);
5889
5890         /* configuration page API internal commands callback handler */
5891         config_cb_idx = mpt2sas_base_register_callback_handler(
5892             mpt2sas_config_done);
5893
5894         /* ctl module callback handler */
5895         ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5896
5897         mpt2sas_ctl_init();
5898
5899         error = pci_register_driver(&scsih_driver);
5900         if (error)
5901                 sas_release_transport(mpt2sas_transport_template);
5902
5903         return error;
5904 }
5905
5906 /**
5907  * _scsih_exit - exit point for this driver (when it is a module).
5908  *
5909  * Returns 0 success, anything else error.
5910  */
5911 static void __exit
5912 _scsih_exit(void)
5913 {
5914         printk(KERN_INFO "mpt2sas version %s unloading\n",
5915             MPT2SAS_DRIVER_VERSION);
5916
5917         pci_unregister_driver(&scsih_driver);
5918
5919         sas_release_transport(mpt2sas_transport_template);
5920         mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5921         mpt2sas_base_release_callback_handler(tm_cb_idx);
5922         mpt2sas_base_release_callback_handler(base_cb_idx);
5923         mpt2sas_base_release_callback_handler(transport_cb_idx);
5924         mpt2sas_base_release_callback_handler(config_cb_idx);
5925         mpt2sas_base_release_callback_handler(ctl_cb_idx);
5926
5927         mpt2sas_ctl_exit();
5928 }
5929
5930 module_init(_scsih_init);
5931 module_exit(_scsih_exit);