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