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