mpt2sas: MPI2 Rev Y (2.00.17) and Rev Z (2.00.18) specifications
[linux-2.6-block.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
d648d5a9 5 * Copyright (C) 2007-2013 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
635374e7
EM
44#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/blkdev.h>
49#include <linux/sched.h>
50#include <linux/workqueue.h>
51#include <linux/delay.h>
52#include <linux/pci.h>
53#include <linux/interrupt.h>
ef7c80c1 54#include <linux/aer.h>
f7c95ef0 55#include <linux/raid_class.h>
5a0e3ad6 56#include <linux/slab.h>
635374e7
EM
57
58#include "mpt2sas_base.h"
59
60MODULE_AUTHOR(MPT2SAS_AUTHOR);
61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
62MODULE_LICENSE("GPL");
63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
64
65#define RAID_CHANNEL 1
66
67/* forward proto's */
68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
69 struct _sas_node *sas_expander);
70static void _firmware_event_work(struct work_struct *work);
71
f3eedd69
KD
72static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
73
921cd802 74static void _scsih_scan_start(struct Scsi_Host *shost);
75static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
76
635374e7 77/* global parameters */
ba33fadf 78LIST_HEAD(mpt2sas_ioc_list);
635374e7
EM
79
80/* local parameters */
635374e7
EM
81static u8 scsi_io_cb_idx = -1;
82static u8 tm_cb_idx = -1;
83static u8 ctl_cb_idx = -1;
84static u8 base_cb_idx = -1;
921cd802 85static u8 port_enable_cb_idx = -1;
635374e7 86static u8 transport_cb_idx = -1;
744090d3 87static u8 scsih_cb_idx = -1;
635374e7
EM
88static u8 config_cb_idx = -1;
89static int mpt_ids;
90
77e63ed4 91static u8 tm_tr_cb_idx = -1 ;
f3eedd69 92static u8 tm_tr_volume_cb_idx = -1 ;
77e63ed4
KD
93static u8 tm_sas_control_cb_idx = -1;
94
635374e7 95/* command line options */
ba33fadf 96static u32 logging_level;
635374e7
EM
97MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
98 "(default=0)");
99
a3e1e55e
KD
100static ushort max_sectors = 0xFFFF;
101module_param(max_sectors, ushort, 0);
9ac49d3a 102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
a3e1e55e 103
b0df96a0
RS
104static int missing_delay[2] = {-1, -1};
105module_param_array(missing_delay, int, NULL, 0);
106MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
107
635374e7
EM
108/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
109#define MPT2SAS_MAX_LUN (16895)
110static int max_lun = MPT2SAS_MAX_LUN;
111module_param(max_lun, int, 0);
112MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
113
921cd802 114/* diag_buffer_enable is bitwise
115 * bit 0 set = TRACE
116 * bit 1 set = SNAPSHOT
117 * bit 2 set = EXTENDED
118 *
119 * Either bit can be set, or both
120 */
121static int diag_buffer_enable = -1;
122module_param(diag_buffer_enable, int, 0);
123MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
124 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
125
4b193175 126static int disable_discovery = -1;
127module_param(disable_discovery, int, 0);
128MODULE_PARM_DESC(disable_discovery, " disable discovery ");
129
5e95e731
MP
130/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
131static int prot_mask = 0;
132module_param(prot_mask, int, 0);
133MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
134
635374e7
EM
135/**
136 * struct sense_info - common structure for obtaining sense keys
137 * @skey: sense key
138 * @asc: additional sense code
139 * @ascq: additional sense code qualifier
140 */
141struct sense_info {
142 u8 skey;
143 u8 asc;
144 u8 ascq;
145};
146
147
3ace8e05 148#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
921cd802 149#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
150#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
635374e7
EM
151/**
152 * struct fw_event_work - firmware event struct
153 * @list: link list framework
154 * @work: work object (ioc->fault_reset_work_q)
f1c35e6a 155 * @cancel_pending_work: flag set during reset handling
635374e7 156 * @ioc: per adapter object
3ace8e05 157 * @device_handle: device handle
635374e7 158 * @VF_ID: virtual function id
7b936b02 159 * @VP_ID: virtual port id
635374e7
EM
160 * @ignore: flag meaning this event has been marked to ignore
161 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
162 * @event_data: reply event data payload follows
163 *
164 * This object stored on ioc->fw_event_list.
165 */
166struct fw_event_work {
167 struct list_head list;
f1c35e6a
KD
168 u8 cancel_pending_work;
169 struct delayed_work delayed_work;
635374e7 170 struct MPT2SAS_ADAPTER *ioc;
3ace8e05 171 u16 device_handle;
635374e7 172 u8 VF_ID;
7b936b02 173 u8 VP_ID;
635374e7
EM
174 u8 ignore;
175 u16 event;
00713ad7 176 char event_data[0] __aligned(4);
635374e7
EM
177};
178
f7c95ef0
KD
179/* raid transport support */
180static struct raid_template *mpt2sas_raid_template;
181
635374e7
EM
182/**
183 * struct _scsi_io_transfer - scsi io transfer
184 * @handle: sas device handle (assigned by firmware)
185 * @is_raid: flag set for hidden raid components
186 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
187 * @data_length: data transfer length
188 * @data_dma: dma pointer to data
189 * @sense: sense data
190 * @lun: lun number
191 * @cdb_length: cdb length
192 * @cdb: cdb contents
635374e7 193 * @timeout: timeout for this command
7b936b02
KD
194 * @VF_ID: virtual function id
195 * @VP_ID: virtual port id
196 * @valid_reply: flag set for reply message
635374e7
EM
197 * @sense_length: sense length
198 * @ioc_status: ioc status
199 * @scsi_state: scsi state
200 * @scsi_status: scsi staus
201 * @log_info: log information
202 * @transfer_length: data length transfer when there is a reply message
203 *
204 * Used for sending internal scsi commands to devices within this module.
205 * Refer to _scsi_send_scsi_io().
206 */
207struct _scsi_io_transfer {
208 u16 handle;
209 u8 is_raid;
210 enum dma_data_direction dir;
211 u32 data_length;
212 dma_addr_t data_dma;
213 u8 sense[SCSI_SENSE_BUFFERSIZE];
214 u32 lun;
215 u8 cdb_length;
216 u8 cdb[32];
217 u8 timeout;
7b936b02
KD
218 u8 VF_ID;
219 u8 VP_ID;
635374e7
EM
220 u8 valid_reply;
221 /* the following bits are only valid when 'valid_reply = 1' */
222 u32 sense_length;
223 u16 ioc_status;
224 u8 scsi_state;
225 u8 scsi_status;
226 u32 log_info;
227 u32 transfer_length;
228};
229
230/*
231 * The pci device ids are defined in mpi/mpi2_cnfg.h.
232 */
233static struct pci_device_id scsih_pci_table[] = {
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
235 PCI_ANY_ID, PCI_ANY_ID },
236 /* Falcon ~ 2008*/
237 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
238 PCI_ANY_ID, PCI_ANY_ID },
239 /* Liberator ~ 2108 */
240 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
241 PCI_ANY_ID, PCI_ANY_ID },
242 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
243 PCI_ANY_ID, PCI_ANY_ID },
244 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
245 PCI_ANY_ID, PCI_ANY_ID },
db27136a 246 /* Meteor ~ 2116 */
635374e7
EM
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
250 PCI_ANY_ID, PCI_ANY_ID },
db27136a
KD
251 /* Thunderbolt ~ 2208 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
253 PCI_ANY_ID, PCI_ANY_ID },
254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
257 PCI_ANY_ID, PCI_ANY_ID },
258 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
259 PCI_ANY_ID, PCI_ANY_ID },
260 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
261 PCI_ANY_ID, PCI_ANY_ID },
262 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
263 PCI_ANY_ID, PCI_ANY_ID },
203d65b1
KD
264 /* Mustang ~ 2308 */
265 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
db27136a 266 PCI_ANY_ID, PCI_ANY_ID },
203d65b1
KD
267 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
268 PCI_ANY_ID, PCI_ANY_ID },
269 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
db27136a 270 PCI_ANY_ID, PCI_ANY_ID },
0bdccdb0
KD
271 /* SSS6200 */
272 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
273 PCI_ANY_ID, PCI_ANY_ID },
635374e7
EM
274 {0} /* Terminating entry */
275};
276MODULE_DEVICE_TABLE(pci, scsih_pci_table);
277
278/**
d5d135b3 279 * _scsih_set_debug_level - global setting of ioc->logging_level.
635374e7
EM
280 *
281 * Note: The logging levels are defined in mpt2sas_debug.h.
282 */
283static int
d5d135b3 284_scsih_set_debug_level(const char *val, struct kernel_param *kp)
635374e7
EM
285{
286 int ret = param_set_int(val, kp);
287 struct MPT2SAS_ADAPTER *ioc;
288
289 if (ret)
290 return ret;
291
292 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
ba33fadf 293 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
635374e7
EM
294 ioc->logging_level = logging_level;
295 return 0;
296}
d5d135b3 297module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
635374e7
EM
298 &logging_level, 0644);
299
300/**
301 * _scsih_srch_boot_sas_address - search based on sas_address
302 * @sas_address: sas address
303 * @boot_device: boot device object from bios page 2
304 *
305 * Returns 1 when there's a match, 0 means no match.
306 */
307static inline int
308_scsih_srch_boot_sas_address(u64 sas_address,
309 Mpi2BootDeviceSasWwid_t *boot_device)
310{
311 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
312}
313
314/**
315 * _scsih_srch_boot_device_name - search based on device name
316 * @device_name: device name specified in INDENTIFY fram
317 * @boot_device: boot device object from bios page 2
318 *
319 * Returns 1 when there's a match, 0 means no match.
320 */
321static inline int
322_scsih_srch_boot_device_name(u64 device_name,
323 Mpi2BootDeviceDeviceName_t *boot_device)
324{
325 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
326}
327
328/**
329 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
330 * @enclosure_logical_id: enclosure logical id
331 * @slot_number: slot number
332 * @boot_device: boot device object from bios page 2
333 *
334 * Returns 1 when there's a match, 0 means no match.
335 */
336static inline int
337_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
338 Mpi2BootDeviceEnclosureSlot_t *boot_device)
339{
340 return (enclosure_logical_id == le64_to_cpu(boot_device->
341 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
342 SlotNumber)) ? 1 : 0;
343}
344
345/**
346 * _scsih_is_boot_device - search for matching boot device.
347 * @sas_address: sas address
348 * @device_name: device name specified in INDENTIFY fram
349 * @enclosure_logical_id: enclosure logical id
350 * @slot_number: slot number
351 * @form: specifies boot device form
352 * @boot_device: boot device object from bios page 2
353 *
354 * Returns 1 when there's a match, 0 means no match.
355 */
356static int
357_scsih_is_boot_device(u64 sas_address, u64 device_name,
358 u64 enclosure_logical_id, u16 slot, u8 form,
359 Mpi2BiosPage2BootDevice_t *boot_device)
360{
361 int rc = 0;
362
363 switch (form) {
364 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
365 if (!sas_address)
366 break;
367 rc = _scsih_srch_boot_sas_address(
368 sas_address, &boot_device->SasWwid);
369 break;
370 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
371 if (!enclosure_logical_id)
372 break;
373 rc = _scsih_srch_boot_encl_slot(
374 enclosure_logical_id,
375 slot, &boot_device->EnclosureSlot);
376 break;
377 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
378 if (!device_name)
379 break;
380 rc = _scsih_srch_boot_device_name(
381 device_name, &boot_device->DeviceName);
382 break;
383 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
384 break;
385 }
386
387 return rc;
388}
389
c5e039be
KD
390/**
391 * _scsih_get_sas_address - set the sas_address for given device handle
392 * @handle: device handle
393 * @sas_address: sas address
394 *
395 * Returns 0 success, non-zero when failure
396 */
397static int
398_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
399 u64 *sas_address)
400{
401 Mpi2SasDevicePage0_t sas_device_pg0;
402 Mpi2ConfigReply_t mpi_reply;
403 u32 ioc_status;
24f09b59 404 *sas_address = 0;
c5e039be
KD
405
406 if (handle <= ioc->sas_hba.num_phys) {
407 *sas_address = ioc->sas_hba.sas_address;
408 return 0;
24f09b59 409 }
c5e039be
KD
410
411 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
412 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
24f09b59 413 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
414 __FILE__, __LINE__, __func__);
c5e039be
KD
415 return -ENXIO;
416 }
417
24f09b59 418 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
419 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
420 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
421 return 0;
c5e039be
KD
422 }
423
24f09b59 424 /* we hit this becuase the given parent handle doesn't exist */
425 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
426 return -ENXIO;
427 /* else error case */
428 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
429 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
430 __FILE__, __LINE__, __func__);
431 return -EIO;
c5e039be
KD
432}
433
635374e7
EM
434/**
435 * _scsih_determine_boot_device - determine boot device.
436 * @ioc: per adapter object
437 * @device: either sas_device or raid_device object
438 * @is_raid: [flag] 1 = raid object, 0 = sas object
439 *
440 * Determines whether this device should be first reported device to
25985edc 441 * to scsi-ml or sas transport, this purpose is for persistent boot device.
635374e7
EM
442 * There are primary, alternate, and current entries in bios page 2. The order
443 * priority is primary, alternate, then current. This routine saves
444 * the corresponding device object and is_raid flag in the ioc object.
445 * The saved data to be used later in _scsih_probe_boot_devices().
446 */
447static void
448_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
449 void *device, u8 is_raid)
450{
451 struct _sas_device *sas_device;
452 struct _raid_device *raid_device;
453 u64 sas_address;
454 u64 device_name;
455 u64 enclosure_logical_id;
456 u16 slot;
457
458 /* only process this function when driver loads */
921cd802 459 if (!ioc->is_driver_loading)
460 return;
461
462 /* no Bios, return immediately */
463 if (!ioc->bios_pg3.BiosVersion)
635374e7
EM
464 return;
465
466 if (!is_raid) {
467 sas_device = device;
468 sas_address = sas_device->sas_address;
469 device_name = sas_device->device_name;
470 enclosure_logical_id = sas_device->enclosure_logical_id;
471 slot = sas_device->slot;
472 } else {
473 raid_device = device;
474 sas_address = raid_device->wwid;
475 device_name = 0;
476 enclosure_logical_id = 0;
477 slot = 0;
478 }
479
480 if (!ioc->req_boot_device.device) {
481 if (_scsih_is_boot_device(sas_address, device_name,
482 enclosure_logical_id, slot,
483 (ioc->bios_pg2.ReqBootDeviceForm &
484 MPI2_BIOSPAGE2_FORM_MASK),
485 &ioc->bios_pg2.RequestedBootDevice)) {
eabb08ad 486 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
487 "%s: req_boot_device(0x%016llx)\n",
488 ioc->name, __func__,
489 (unsigned long long)sas_address));
490 ioc->req_boot_device.device = device;
491 ioc->req_boot_device.is_raid = is_raid;
492 }
493 }
494
495 if (!ioc->req_alt_boot_device.device) {
496 if (_scsih_is_boot_device(sas_address, device_name,
497 enclosure_logical_id, slot,
498 (ioc->bios_pg2.ReqAltBootDeviceForm &
499 MPI2_BIOSPAGE2_FORM_MASK),
500 &ioc->bios_pg2.RequestedAltBootDevice)) {
eabb08ad 501 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
502 "%s: req_alt_boot_device(0x%016llx)\n",
503 ioc->name, __func__,
504 (unsigned long long)sas_address));
505 ioc->req_alt_boot_device.device = device;
506 ioc->req_alt_boot_device.is_raid = is_raid;
507 }
508 }
509
510 if (!ioc->current_boot_device.device) {
511 if (_scsih_is_boot_device(sas_address, device_name,
512 enclosure_logical_id, slot,
513 (ioc->bios_pg2.CurrentBootDeviceForm &
514 MPI2_BIOSPAGE2_FORM_MASK),
515 &ioc->bios_pg2.CurrentBootDevice)) {
eabb08ad 516 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
517 "%s: current_boot_device(0x%016llx)\n",
518 ioc->name, __func__,
519 (unsigned long long)sas_address));
520 ioc->current_boot_device.device = device;
521 ioc->current_boot_device.is_raid = is_raid;
522 }
523 }
524}
525
526/**
527 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
528 * @ioc: per adapter object
529 * @sas_address: sas address
530 * Context: Calling function should acquire ioc->sas_device_lock
531 *
532 * This searches for sas_device based on sas_address, then return sas_device
533 * object.
534 */
535struct _sas_device *
536mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
537 u64 sas_address)
538{
cd9843f8 539 struct _sas_device *sas_device;
635374e7 540
cd9843f8
KD
541 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
542 if (sas_device->sas_address == sas_address)
543 return sas_device;
544
545 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
546 if (sas_device->sas_address == sas_address)
547 return sas_device;
548
549 return NULL;
635374e7
EM
550}
551
552/**
553 * _scsih_sas_device_find_by_handle - sas device search
554 * @ioc: per adapter object
555 * @handle: sas device handle (assigned by firmware)
556 * Context: Calling function should acquire ioc->sas_device_lock
557 *
558 * This searches for sas_device based on sas_address, then return sas_device
559 * object.
560 */
561static struct _sas_device *
562_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
563{
cd9843f8
KD
564 struct _sas_device *sas_device;
565
566 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
567 if (sas_device->handle == handle)
568 return sas_device;
569
570 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
571 if (sas_device->handle == handle)
572 return sas_device;
635374e7 573
cd9843f8 574 return NULL;
635374e7
EM
575}
576
577/**
578 * _scsih_sas_device_remove - remove sas_device from list.
579 * @ioc: per adapter object
580 * @sas_device: the sas_device object
581 * Context: This function will acquire ioc->sas_device_lock.
582 *
583 * Removing object and freeing associated memory from the ioc->sas_device_list.
584 */
585static void
586_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
587 struct _sas_device *sas_device)
588{
589 unsigned long flags;
590
980ead31
KD
591 if (!sas_device)
592 return;
593
635374e7 594 spin_lock_irqsave(&ioc->sas_device_lock, flags);
09da0b32 595 list_del(&sas_device->list);
596 kfree(sas_device);
635374e7
EM
597 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
598}
599
09da0b32 600
635374e7
EM
601/**
602 * _scsih_sas_device_add - insert sas_device to the list.
603 * @ioc: per adapter object
604 * @sas_device: the sas_device object
605 * Context: This function will acquire ioc->sas_device_lock.
606 *
607 * Adding new object to the ioc->sas_device_list.
608 */
609static void
610_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
611 struct _sas_device *sas_device)
612{
613 unsigned long flags;
635374e7 614
eabb08ad 615 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
635374e7
EM
616 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
617 sas_device->handle, (unsigned long long)sas_device->sas_address));
618
619 spin_lock_irqsave(&ioc->sas_device_lock, flags);
620 list_add_tail(&sas_device->list, &ioc->sas_device_list);
621 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
622
c5e039be 623 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
35116db9 624 sas_device->sas_address_parent)) {
635374e7 625 _scsih_sas_device_remove(ioc, sas_device);
23edb6e7 626 } else if (!sas_device->starget) {
627 /* When asyn scanning is enabled, its not possible to remove
628 * devices while scanning is turned on due to an oops in
629 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
630 */
bc6d4c3b 631 if (!ioc->is_driver_loading) {
23edb6e7 632 mpt2sas_transport_port_remove(ioc,
633 sas_device->sas_address,
634 sas_device->sas_address_parent);
bc6d4c3b
SR
635 _scsih_sas_device_remove(ioc, sas_device);
636 }
23edb6e7 637 }
635374e7
EM
638}
639
640/**
641 * _scsih_sas_device_init_add - insert sas_device to the list.
642 * @ioc: per adapter object
643 * @sas_device: the sas_device object
644 * Context: This function will acquire ioc->sas_device_lock.
645 *
646 * Adding new object at driver load time to the ioc->sas_device_init_list.
647 */
648static void
649_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
650 struct _sas_device *sas_device)
651{
652 unsigned long flags;
653
eabb08ad 654 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
635374e7
EM
655 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
656 sas_device->handle, (unsigned long long)sas_device->sas_address));
657
658 spin_lock_irqsave(&ioc->sas_device_lock, flags);
659 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
635374e7 660 _scsih_determine_boot_device(ioc, sas_device, 0);
09da0b32 661 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
635374e7
EM
662}
663
635374e7
EM
664/**
665 * _scsih_raid_device_find_by_id - raid device search
666 * @ioc: per adapter object
667 * @id: sas device target id
668 * @channel: sas device channel
669 * Context: Calling function should acquire ioc->raid_device_lock
670 *
671 * This searches for raid_device based on target id, then return raid_device
672 * object.
673 */
674static struct _raid_device *
675_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
676{
677 struct _raid_device *raid_device, *r;
678
679 r = NULL;
680 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
681 if (raid_device->id == id && raid_device->channel == channel) {
682 r = raid_device;
683 goto out;
684 }
685 }
686
687 out:
688 return r;
689}
690
691/**
692 * _scsih_raid_device_find_by_handle - raid device search
693 * @ioc: per adapter object
694 * @handle: sas device handle (assigned by firmware)
695 * Context: Calling function should acquire ioc->raid_device_lock
696 *
697 * This searches for raid_device based on handle, then return raid_device
698 * object.
699 */
700static struct _raid_device *
701_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
702{
703 struct _raid_device *raid_device, *r;
704
705 r = NULL;
706 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
707 if (raid_device->handle != handle)
708 continue;
709 r = raid_device;
710 goto out;
711 }
712
713 out:
714 return r;
715}
716
717/**
718 * _scsih_raid_device_find_by_wwid - raid device search
719 * @ioc: per adapter object
720 * @handle: sas device handle (assigned by firmware)
721 * Context: Calling function should acquire ioc->raid_device_lock
722 *
723 * This searches for raid_device based on wwid, then return raid_device
724 * object.
725 */
726static struct _raid_device *
727_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
728{
729 struct _raid_device *raid_device, *r;
730
731 r = NULL;
732 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
733 if (raid_device->wwid != wwid)
734 continue;
735 r = raid_device;
736 goto out;
737 }
738
739 out:
740 return r;
741}
742
743/**
744 * _scsih_raid_device_add - add raid_device object
745 * @ioc: per adapter object
746 * @raid_device: raid_device object
747 *
748 * This is added to the raid_device_list link list.
749 */
750static void
751_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
752 struct _raid_device *raid_device)
753{
754 unsigned long flags;
755
eabb08ad 756 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
635374e7
EM
757 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
758 raid_device->handle, (unsigned long long)raid_device->wwid));
759
760 spin_lock_irqsave(&ioc->raid_device_lock, flags);
761 list_add_tail(&raid_device->list, &ioc->raid_device_list);
762 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
763}
764
765/**
766 * _scsih_raid_device_remove - delete raid_device object
767 * @ioc: per adapter object
768 * @raid_device: raid_device object
769 *
635374e7
EM
770 */
771static void
772_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
773 struct _raid_device *raid_device)
774{
775 unsigned long flags;
776
777 spin_lock_irqsave(&ioc->raid_device_lock, flags);
778 list_del(&raid_device->list);
635374e7
EM
779 kfree(raid_device);
780 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
781}
782
c5e039be
KD
783/**
784 * mpt2sas_scsih_expander_find_by_handle - expander device search
785 * @ioc: per adapter object
786 * @handle: expander handle (assigned by firmware)
787 * Context: Calling function should acquire ioc->sas_device_lock
788 *
789 * This searches for expander device based on handle, then returns the
790 * sas_node object.
791 */
792struct _sas_node *
793mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
794{
795 struct _sas_node *sas_expander, *r;
796
797 r = NULL;
798 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
799 if (sas_expander->handle != handle)
800 continue;
801 r = sas_expander;
802 goto out;
803 }
804 out:
805 return r;
806}
807
635374e7
EM
808/**
809 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
810 * @ioc: per adapter object
811 * @sas_address: sas address
812 * Context: Calling function should acquire ioc->sas_node_lock.
813 *
814 * This searches for expander device based on sas_address, then returns the
815 * sas_node object.
816 */
817struct _sas_node *
818mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
819 u64 sas_address)
820{
821 struct _sas_node *sas_expander, *r;
822
823 r = NULL;
824 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
825 if (sas_expander->sas_address != sas_address)
826 continue;
827 r = sas_expander;
828 goto out;
829 }
830 out:
831 return r;
832}
833
834/**
835 * _scsih_expander_node_add - insert expander device to the list.
836 * @ioc: per adapter object
837 * @sas_expander: the sas_device object
838 * Context: This function will acquire ioc->sas_node_lock.
839 *
840 * Adding new object to the ioc->sas_expander_list.
841 *
842 * Return nothing.
843 */
844static void
845_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
846 struct _sas_node *sas_expander)
847{
848 unsigned long flags;
849
850 spin_lock_irqsave(&ioc->sas_node_lock, flags);
851 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
852 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
853}
854
855/**
856 * _scsih_is_end_device - determines if device is an end device
857 * @device_info: bitfield providing information about the device.
858 * Context: none
859 *
860 * Returns 1 if end device.
861 */
862static int
863_scsih_is_end_device(u32 device_info)
864{
865 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
866 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
867 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
868 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
869 return 1;
870 else
871 return 0;
872}
873
874/**
ec07a053 875 * _scsih_scsi_lookup_get - returns scmd entry
635374e7
EM
876 * @ioc: per adapter object
877 * @smid: system request message index
635374e7
EM
878 *
879 * Returns the smid stored scmd pointer.
880 */
881static struct scsi_cmnd *
882_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
883{
595bb0bd 884 return ioc->scsi_lookup[smid - 1].scmd;
635374e7
EM
885}
886
ec07a053
KD
887/**
888 * _scsih_scsi_lookup_get_clear - returns scmd entry
889 * @ioc: per adapter object
890 * @smid: system request message index
891 *
892 * Returns the smid stored scmd pointer.
893 * Then will derefrence the stored scmd pointer.
894 */
895static inline struct scsi_cmnd *
896_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
897{
898 unsigned long flags;
899 struct scsi_cmnd *scmd;
900
901 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
902 scmd = ioc->scsi_lookup[smid - 1].scmd;
903 ioc->scsi_lookup[smid - 1].scmd = NULL;
904 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
905
906 return scmd;
907}
908
635374e7
EM
909/**
910 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
911 * @ioc: per adapter object
912 * @smid: system request message index
913 * @scmd: pointer to scsi command object
914 * Context: This function will acquire ioc->scsi_lookup_lock.
915 *
916 * This will search for a scmd pointer in the scsi_lookup array,
917 * returning the revelent smid. A returned value of zero means invalid.
918 */
919static u16
920_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
921 *scmd)
922{
923 u16 smid;
924 unsigned long flags;
925 int i;
926
927 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
928 smid = 0;
595bb0bd 929 for (i = 0; i < ioc->scsiio_depth; i++) {
635374e7 930 if (ioc->scsi_lookup[i].scmd == scmd) {
595bb0bd 931 smid = ioc->scsi_lookup[i].smid;
635374e7
EM
932 goto out;
933 }
934 }
935 out:
936 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
937 return smid;
938}
939
940/**
941 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
942 * @ioc: per adapter object
943 * @id: target id
944 * @channel: channel
945 * Context: This function will acquire ioc->scsi_lookup_lock.
946 *
947 * This will search for a matching channel:id in the scsi_lookup array,
948 * returning 1 if found.
949 */
950static u8
951_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
952 int channel)
953{
954 u8 found;
955 unsigned long flags;
956 int i;
957
958 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
959 found = 0;
595bb0bd 960 for (i = 0 ; i < ioc->scsiio_depth; i++) {
635374e7
EM
961 if (ioc->scsi_lookup[i].scmd &&
962 (ioc->scsi_lookup[i].scmd->device->id == id &&
963 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
964 found = 1;
965 goto out;
966 }
967 }
968 out:
969 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
970 return found;
971}
972
993e0da7
EM
973/**
974 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
975 * @ioc: per adapter object
976 * @id: target id
977 * @lun: lun number
978 * @channel: channel
979 * Context: This function will acquire ioc->scsi_lookup_lock.
980 *
981 * This will search for a matching channel:id:lun in the scsi_lookup array,
982 * returning 1 if found.
983 */
984static u8
985_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
986 unsigned int lun, int channel)
987{
988 u8 found;
989 unsigned long flags;
990 int i;
991
992 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
993 found = 0;
595bb0bd 994 for (i = 0 ; i < ioc->scsiio_depth; i++) {
993e0da7
EM
995 if (ioc->scsi_lookup[i].scmd &&
996 (ioc->scsi_lookup[i].scmd->device->id == id &&
997 ioc->scsi_lookup[i].scmd->device->channel == channel &&
998 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
999 found = 1;
1000 goto out;
1001 }
1002 }
1003 out:
1004 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1005 return found;
1006}
1007
635374e7 1008/**
35f805b5 1009 * _scsih_get_chain_buffer_tracker - obtain chain tracker
635374e7 1010 * @ioc: per adapter object
35f805b5 1011 * @smid: smid associated to an IO request
635374e7 1012 *
35f805b5 1013 * Returns chain tracker(from ioc->free_chain_list)
635374e7 1014 */
35f805b5
KD
1015static struct chain_tracker *
1016_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
635374e7 1017{
35f805b5
KD
1018 struct chain_tracker *chain_req;
1019 unsigned long flags;
635374e7 1020
35f805b5
KD
1021 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1022 if (list_empty(&ioc->free_chain_list)) {
1023 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
aff132d9 1024 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1025 "available\n", ioc->name));
35f805b5
KD
1026 return NULL;
1027 }
1028 chain_req = list_entry(ioc->free_chain_list.next,
1029 struct chain_tracker, tracker_list);
1030 list_del_init(&chain_req->tracker_list);
1031 list_add_tail(&chain_req->tracker_list,
1032 &ioc->scsi_lookup[smid - 1].chain_list);
1033 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1034 return chain_req;
635374e7
EM
1035}
1036
1037/**
1038 * _scsih_build_scatter_gather - main sg creation routine
1039 * @ioc: per adapter object
1040 * @scmd: scsi command
1041 * @smid: system request message index
1042 * Context: none.
1043 *
1044 * The main routine that builds scatter gather table from a given
1045 * scsi request sent via the .queuecommand main handler.
1046 *
1047 * Returns 0 success, anything else error
1048 */
1049static int
1050_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1051 struct scsi_cmnd *scmd, u16 smid)
1052{
1053 Mpi2SCSIIORequest_t *mpi_request;
1054 dma_addr_t chain_dma;
1055 struct scatterlist *sg_scmd;
1056 void *sg_local, *chain;
1057 u32 chain_offset;
1058 u32 chain_length;
1059 u32 chain_flags;
bb789d01 1060 int sges_left;
635374e7
EM
1061 u32 sges_in_segment;
1062 u32 sgl_flags;
1063 u32 sgl_flags_last_element;
1064 u32 sgl_flags_end_buffer;
35f805b5 1065 struct chain_tracker *chain_req;
635374e7
EM
1066
1067 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1068
1069 /* init scatter gather flags */
1070 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1071 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1072 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1073 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1074 << MPI2_SGE_FLAGS_SHIFT;
1075 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1076 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1077 << MPI2_SGE_FLAGS_SHIFT;
1078 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1079
1080 sg_scmd = scsi_sglist(scmd);
1081 sges_left = scsi_dma_map(scmd);
bb789d01 1082 if (sges_left < 0) {
635374e7
EM
1083 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1084 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1085 return -ENOMEM;
1086 }
1087
1088 sg_local = &mpi_request->SGL;
1089 sges_in_segment = ioc->max_sges_in_main_message;
1090 if (sges_left <= sges_in_segment)
1091 goto fill_in_last_segment;
1092
1093 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1094 (sges_in_segment * ioc->sge_size))/4;
1095
1096 /* fill in main message segment when there is a chain following */
1097 while (sges_in_segment) {
1098 if (sges_in_segment == 1)
1099 ioc->base_add_sg_single(sg_local,
1100 sgl_flags_last_element | sg_dma_len(sg_scmd),
1101 sg_dma_address(sg_scmd));
1102 else
1103 ioc->base_add_sg_single(sg_local, sgl_flags |
1104 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1105 sg_scmd = sg_next(sg_scmd);
1106 sg_local += ioc->sge_size;
1107 sges_left--;
1108 sges_in_segment--;
1109 }
1110
1111 /* initializing the chain flags and pointers */
1112 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
35f805b5
KD
1113 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1114 if (!chain_req)
1115 return -1;
1116 chain = chain_req->chain_buffer;
1117 chain_dma = chain_req->chain_buffer_dma;
635374e7
EM
1118 do {
1119 sges_in_segment = (sges_left <=
1120 ioc->max_sges_in_chain_message) ? sges_left :
1121 ioc->max_sges_in_chain_message;
1122 chain_offset = (sges_left == sges_in_segment) ?
1123 0 : (sges_in_segment * ioc->sge_size)/4;
1124 chain_length = sges_in_segment * ioc->sge_size;
1125 if (chain_offset) {
1126 chain_offset = chain_offset <<
1127 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1128 chain_length += ioc->sge_size;
1129 }
1130 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1131 chain_length, chain_dma);
1132 sg_local = chain;
1133 if (!chain_offset)
1134 goto fill_in_last_segment;
1135
1136 /* fill in chain segments */
1137 while (sges_in_segment) {
1138 if (sges_in_segment == 1)
1139 ioc->base_add_sg_single(sg_local,
1140 sgl_flags_last_element |
1141 sg_dma_len(sg_scmd),
1142 sg_dma_address(sg_scmd));
1143 else
1144 ioc->base_add_sg_single(sg_local, sgl_flags |
1145 sg_dma_len(sg_scmd),
1146 sg_dma_address(sg_scmd));
1147 sg_scmd = sg_next(sg_scmd);
1148 sg_local += ioc->sge_size;
1149 sges_left--;
1150 sges_in_segment--;
1151 }
1152
35f805b5
KD
1153 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1154 if (!chain_req)
1155 return -1;
1156 chain = chain_req->chain_buffer;
1157 chain_dma = chain_req->chain_buffer_dma;
635374e7
EM
1158 } while (1);
1159
1160
1161 fill_in_last_segment:
1162
1163 /* fill the last segment */
1164 while (sges_left) {
1165 if (sges_left == 1)
1166 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1167 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1168 else
1169 ioc->base_add_sg_single(sg_local, sgl_flags |
1170 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1171 sg_scmd = sg_next(sg_scmd);
1172 sg_local += ioc->sge_size;
1173 sges_left--;
1174 }
1175
1176 return 0;
1177}
1178
1179/**
a93c6b45 1180 * _scsih_adjust_queue_depth - setting device queue depth
635374e7
EM
1181 * @sdev: scsi device struct
1182 * @qdepth: requested queue depth
1183 *
a93c6b45
KD
1184 *
1185 * Returns nothing
635374e7 1186 */
a93c6b45
KD
1187static void
1188_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
635374e7
EM
1189{
1190 struct Scsi_Host *shost = sdev->host;
1191 int max_depth;
e0077d60
KD
1192 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1193 struct MPT2SAS_DEVICE *sas_device_priv_data;
1194 struct MPT2SAS_TARGET *sas_target_priv_data;
1195 struct _sas_device *sas_device;
1196 unsigned long flags;
635374e7
EM
1197
1198 max_depth = shost->can_queue;
e0077d60
KD
1199
1200 /* limit max device queue for SATA to 32 */
1201 sas_device_priv_data = sdev->hostdata;
1202 if (!sas_device_priv_data)
1203 goto not_sata;
1204 sas_target_priv_data = sas_device_priv_data->sas_target;
1205 if (!sas_target_priv_data)
1206 goto not_sata;
1207 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1208 goto not_sata;
1209 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1210 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1211 sas_device_priv_data->sas_target->sas_address);
e0077d60
KD
1212 if (sas_device && sas_device->device_info &
1213 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1214 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
09da0b32 1215 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
e0077d60
KD
1216
1217 not_sata:
1218
635374e7
EM
1219 if (!sdev->tagged_supported)
1220 max_depth = 1;
1221 if (qdepth > max_depth)
1222 qdepth = max_depth;
a93c6b45
KD
1223 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1224}
1225
1226/**
1227 * _scsih_change_queue_depth - setting device queue depth
1228 * @sdev: scsi device struct
1229 * @qdepth: requested queue depth
1230 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1231 * (see include/scsi/scsi_host.h for definition)
1232 *
1233 * Returns queue depth.
1234 */
1235static int
1236_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1237{
1238 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1239 _scsih_adjust_queue_depth(sdev, qdepth);
1240 else if (reason == SCSI_QDEPTH_QFULL)
1241 scsi_track_queue_full(sdev, qdepth);
1242 else
1243 return -EOPNOTSUPP;
635374e7
EM
1244
1245 if (sdev->inquiry_len > 7)
1246 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1247 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1248 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1249 sdev->ordered_tags, sdev->scsi_level,
1250 (sdev->inquiry[7] & 2) >> 1);
1251
1252 return sdev->queue_depth;
1253}
1254
1255/**
595bb0bd 1256 * _scsih_change_queue_type - changing device queue tag type
635374e7
EM
1257 * @sdev: scsi device struct
1258 * @tag_type: requested tag type
1259 *
1260 * Returns queue tag type.
1261 */
1262static int
d5d135b3 1263_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
635374e7
EM
1264{
1265 if (sdev->tagged_supported) {
1266 scsi_set_tag_type(sdev, tag_type);
1267 if (tag_type)
1268 scsi_activate_tcq(sdev, sdev->queue_depth);
1269 else
1270 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1271 } else
1272 tag_type = 0;
1273
1274 return tag_type;
1275}
1276
1277/**
d5d135b3 1278 * _scsih_target_alloc - target add routine
635374e7
EM
1279 * @starget: scsi target struct
1280 *
1281 * Returns 0 if ok. Any other return is assumed to be an error and
1282 * the device is ignored.
1283 */
1284static int
d5d135b3 1285_scsih_target_alloc(struct scsi_target *starget)
635374e7
EM
1286{
1287 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1288 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1289 struct MPT2SAS_TARGET *sas_target_priv_data;
1290 struct _sas_device *sas_device;
1291 struct _raid_device *raid_device;
1292 unsigned long flags;
1293 struct sas_rphy *rphy;
1294
feafe7c5
JL
1295 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1296 GFP_KERNEL);
635374e7
EM
1297 if (!sas_target_priv_data)
1298 return -ENOMEM;
1299
1300 starget->hostdata = sas_target_priv_data;
1301 sas_target_priv_data->starget = starget;
1302 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1303
1304 /* RAID volumes */
1305 if (starget->channel == RAID_CHANNEL) {
1306 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1307 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1308 starget->channel);
1309 if (raid_device) {
1310 sas_target_priv_data->handle = raid_device->handle;
1311 sas_target_priv_data->sas_address = raid_device->wwid;
1312 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
09da0b32 1313 if (ioc->is_warpdrive)
1314 sas_target_priv_data->raid_device = raid_device;
635374e7
EM
1315 raid_device->starget = starget;
1316 }
1317 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1318 return 0;
1319 }
1320
1321 /* sas/sata devices */
1322 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1323 rphy = dev_to_rphy(starget->dev.parent);
1324 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1325 rphy->identify.sas_address);
1326
1327 if (sas_device) {
1328 sas_target_priv_data->handle = sas_device->handle;
1329 sas_target_priv_data->sas_address = sas_device->sas_address;
1330 sas_device->starget = starget;
1331 sas_device->id = starget->id;
1332 sas_device->channel = starget->channel;
f3eedd69 1333 if (test_bit(sas_device->handle, ioc->pd_handles))
635374e7
EM
1334 sas_target_priv_data->flags |=
1335 MPT_TARGET_FLAGS_RAID_COMPONENT;
1336 }
1337 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1338
1339 return 0;
1340}
1341
1342/**
d5d135b3 1343 * _scsih_target_destroy - target destroy routine
635374e7
EM
1344 * @starget: scsi target struct
1345 *
1346 * Returns nothing.
1347 */
1348static void
d5d135b3 1349_scsih_target_destroy(struct scsi_target *starget)
635374e7
EM
1350{
1351 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1352 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1353 struct MPT2SAS_TARGET *sas_target_priv_data;
1354 struct _sas_device *sas_device;
1355 struct _raid_device *raid_device;
1356 unsigned long flags;
1357 struct sas_rphy *rphy;
1358
1359 sas_target_priv_data = starget->hostdata;
1360 if (!sas_target_priv_data)
1361 return;
1362
1363 if (starget->channel == RAID_CHANNEL) {
1364 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1365 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1366 starget->channel);
1367 if (raid_device) {
1368 raid_device->starget = NULL;
1369 raid_device->sdev = NULL;
1370 }
1371 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1372 goto out;
1373 }
1374
1375 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1376 rphy = dev_to_rphy(starget->dev.parent);
1377 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1378 rphy->identify.sas_address);
8901cbb4
EM
1379 if (sas_device && (sas_device->starget == starget) &&
1380 (sas_device->id == starget->id) &&
1381 (sas_device->channel == starget->channel))
635374e7
EM
1382 sas_device->starget = NULL;
1383
1384 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1385
1386 out:
1387 kfree(sas_target_priv_data);
1388 starget->hostdata = NULL;
1389}
1390
1391/**
d5d135b3 1392 * _scsih_slave_alloc - device add routine
635374e7
EM
1393 * @sdev: scsi device struct
1394 *
1395 * Returns 0 if ok. Any other return is assumed to be an error and
1396 * the device is ignored.
1397 */
1398static int
d5d135b3 1399_scsih_slave_alloc(struct scsi_device *sdev)
635374e7
EM
1400{
1401 struct Scsi_Host *shost;
1402 struct MPT2SAS_ADAPTER *ioc;
1403 struct MPT2SAS_TARGET *sas_target_priv_data;
1404 struct MPT2SAS_DEVICE *sas_device_priv_data;
1405 struct scsi_target *starget;
1406 struct _raid_device *raid_device;
63e359d4 1407 struct _sas_device *sas_device;
635374e7
EM
1408 unsigned long flags;
1409
feafe7c5
JL
1410 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1411 GFP_KERNEL);
635374e7
EM
1412 if (!sas_device_priv_data)
1413 return -ENOMEM;
1414
1415 sas_device_priv_data->lun = sdev->lun;
1416 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1417
1418 starget = scsi_target(sdev);
1419 sas_target_priv_data = starget->hostdata;
1420 sas_target_priv_data->num_luns++;
1421 sas_device_priv_data->sas_target = sas_target_priv_data;
1422 sdev->hostdata = sas_device_priv_data;
1423 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1424 sdev->no_uld_attach = 1;
1425
1426 shost = dev_to_shost(&starget->dev);
1427 ioc = shost_priv(shost);
1428 if (starget->channel == RAID_CHANNEL) {
1429 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1430 raid_device = _scsih_raid_device_find_by_id(ioc,
1431 starget->id, starget->channel);
1432 if (raid_device)
1433 raid_device->sdev = sdev; /* raid is single lun */
1434 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
635374e7
EM
1435 }
1436
63e359d4
SR
1437 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1438 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1439 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1440 sas_target_priv_data->sas_address);
1441 if (sas_device && (sas_device->starget == NULL)) {
1442 sdev_printk(KERN_INFO, sdev,
1443 "%s : sas_device->starget set to starget @ %d\n",
1444 __func__, __LINE__);
1445 sas_device->starget = starget;
1446 }
1447 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1448 }
1449
635374e7
EM
1450 return 0;
1451}
1452
1453/**
d5d135b3 1454 * _scsih_slave_destroy - device destroy routine
635374e7
EM
1455 * @sdev: scsi device struct
1456 *
1457 * Returns nothing.
1458 */
1459static void
d5d135b3 1460_scsih_slave_destroy(struct scsi_device *sdev)
635374e7
EM
1461{
1462 struct MPT2SAS_TARGET *sas_target_priv_data;
1463 struct scsi_target *starget;
35116db9 1464 struct Scsi_Host *shost;
1465 struct MPT2SAS_ADAPTER *ioc;
1466 struct _sas_device *sas_device;
1467 unsigned long flags;
635374e7
EM
1468
1469 if (!sdev->hostdata)
1470 return;
1471
1472 starget = scsi_target(sdev);
1473 sas_target_priv_data = starget->hostdata;
1474 sas_target_priv_data->num_luns--;
35116db9 1475
1476 shost = dev_to_shost(&starget->dev);
1477 ioc = shost_priv(shost);
1478
1479 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1480 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1481 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1482 sas_target_priv_data->sas_address);
23edb6e7 1483 if (sas_device && !sas_target_priv_data->num_luns)
35116db9 1484 sas_device->starget = NULL;
1485 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1486 }
1487
635374e7
EM
1488 kfree(sdev->hostdata);
1489 sdev->hostdata = NULL;
1490}
1491
1492/**
d5d135b3 1493 * _scsih_display_sata_capabilities - sata capabilities
635374e7 1494 * @ioc: per adapter object
09da0b32 1495 * @handle: device handle
635374e7
EM
1496 * @sdev: scsi device struct
1497 */
1498static void
d5d135b3 1499_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
09da0b32 1500 u16 handle, struct scsi_device *sdev)
635374e7
EM
1501{
1502 Mpi2ConfigReply_t mpi_reply;
1503 Mpi2SasDevicePage0_t sas_device_pg0;
1504 u32 ioc_status;
1505 u16 flags;
1506 u32 device_info;
1507
1508 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
09da0b32 1509 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
635374e7
EM
1510 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1511 ioc->name, __FILE__, __LINE__, __func__);
1512 return;
1513 }
1514
1515 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1516 MPI2_IOCSTATUS_MASK;
1517 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1518 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1519 ioc->name, __FILE__, __LINE__, __func__);
1520 return;
1521 }
1522
1523 flags = le16_to_cpu(sas_device_pg0.Flags);
e94f6747 1524 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
635374e7
EM
1525
1526 sdev_printk(KERN_INFO, sdev,
1527 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1528 "sw_preserve(%s)\n",
1529 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1530 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1531 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1532 "n",
1533 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1534 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1535 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1536}
1537
f7c95ef0
KD
1538/**
1539 * _scsih_is_raid - return boolean indicating device is raid volume
1540 * @dev the device struct object
1541 */
1542static int
1543_scsih_is_raid(struct device *dev)
1544{
1545 struct scsi_device *sdev = to_scsi_device(dev);
0bdccdb0 1546 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
f7c95ef0 1547
0bdccdb0
KD
1548 if (ioc->is_warpdrive)
1549 return 0;
f7c95ef0
KD
1550 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1551}
1552
1553/**
1554 * _scsih_get_resync - get raid volume resync percent complete
1555 * @dev the device struct object
1556 */
1557static void
1558_scsih_get_resync(struct device *dev)
1559{
1560 struct scsi_device *sdev = to_scsi_device(dev);
1561 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1562 static struct _raid_device *raid_device;
1563 unsigned long flags;
1564 Mpi2RaidVolPage0_t vol_pg0;
1565 Mpi2ConfigReply_t mpi_reply;
1566 u32 volume_status_flags;
09da0b32 1567 u8 percent_complete;
1568 u16 handle;
1569
1570 percent_complete = 0;
1571 handle = 0;
1572 if (ioc->is_warpdrive)
1573 goto out;
f7c95ef0
KD
1574
1575 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1576 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1577 sdev->channel);
09da0b32 1578 if (raid_device) {
1579 handle = raid_device->handle;
1580 percent_complete = raid_device->percent_complete;
1581 }
f7c95ef0
KD
1582 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1583
09da0b32 1584 if (!handle)
f7c95ef0
KD
1585 goto out;
1586
1587 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
09da0b32 1588 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
f7c95ef0
KD
1589 sizeof(Mpi2RaidVolPage0_t))) {
1590 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1591 ioc->name, __FILE__, __LINE__, __func__);
09da0b32 1592 percent_complete = 0;
f7c95ef0
KD
1593 goto out;
1594 }
1595
1596 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
09da0b32 1597 if (!(volume_status_flags &
1598 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1599 percent_complete = 0;
1600
f7c95ef0
KD
1601 out:
1602 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1603}
1604
1605/**
1606 * _scsih_get_state - get raid volume level
1607 * @dev the device struct object
1608 */
1609static void
1610_scsih_get_state(struct device *dev)
1611{
1612 struct scsi_device *sdev = to_scsi_device(dev);
1613 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1614 static struct _raid_device *raid_device;
1615 unsigned long flags;
1616 Mpi2RaidVolPage0_t vol_pg0;
1617 Mpi2ConfigReply_t mpi_reply;
1618 u32 volstate;
1619 enum raid_state state = RAID_STATE_UNKNOWN;
09da0b32 1620 u16 handle = 0;
f7c95ef0
KD
1621
1622 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1623 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1624 sdev->channel);
09da0b32 1625 if (raid_device)
1626 handle = raid_device->handle;
f7c95ef0
KD
1627 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1628
1629 if (!raid_device)
1630 goto out;
1631
1632 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
09da0b32 1633 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
f7c95ef0
KD
1634 sizeof(Mpi2RaidVolPage0_t))) {
1635 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1636 ioc->name, __FILE__, __LINE__, __func__);
1637 goto out;
1638 }
1639
1640 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1641 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1642 state = RAID_STATE_RESYNCING;
1643 goto out;
1644 }
1645
1646 switch (vol_pg0.VolumeState) {
1647 case MPI2_RAID_VOL_STATE_OPTIMAL:
1648 case MPI2_RAID_VOL_STATE_ONLINE:
1649 state = RAID_STATE_ACTIVE;
1650 break;
1651 case MPI2_RAID_VOL_STATE_DEGRADED:
1652 state = RAID_STATE_DEGRADED;
1653 break;
1654 case MPI2_RAID_VOL_STATE_FAILED:
1655 case MPI2_RAID_VOL_STATE_MISSING:
1656 state = RAID_STATE_OFFLINE;
1657 break;
1658 }
1659 out:
1660 raid_set_state(mpt2sas_raid_template, dev, state);
1661}
1662
1663/**
1664 * _scsih_set_level - set raid level
1665 * @sdev: scsi device struct
09da0b32 1666 * @volume_type: volume type
f7c95ef0
KD
1667 */
1668static void
09da0b32 1669_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
f7c95ef0
KD
1670{
1671 enum raid_level level = RAID_LEVEL_UNKNOWN;
1672
09da0b32 1673 switch (volume_type) {
f7c95ef0
KD
1674 case MPI2_RAID_VOL_TYPE_RAID0:
1675 level = RAID_LEVEL_0;
1676 break;
1677 case MPI2_RAID_VOL_TYPE_RAID10:
1678 level = RAID_LEVEL_10;
1679 break;
1680 case MPI2_RAID_VOL_TYPE_RAID1E:
1681 level = RAID_LEVEL_1E;
1682 break;
1683 case MPI2_RAID_VOL_TYPE_RAID1:
1684 level = RAID_LEVEL_1;
1685 break;
1686 }
1687
1688 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1689}
1690
635374e7
EM
1691/**
1692 * _scsih_get_volume_capabilities - volume capabilities
1693 * @ioc: per adapter object
1694 * @sas_device: the raid_device object
6faace2a 1695 *
1696 * Returns 0 for success, else 1
635374e7 1697 */
6faace2a 1698static int
635374e7
EM
1699_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1700 struct _raid_device *raid_device)
1701{
1702 Mpi2RaidVolPage0_t *vol_pg0;
1703 Mpi2RaidPhysDiskPage0_t pd_pg0;
1704 Mpi2SasDevicePage0_t sas_device_pg0;
1705 Mpi2ConfigReply_t mpi_reply;
1706 u16 sz;
1707 u8 num_pds;
1708
1709 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1710 &num_pds)) || !num_pds) {
6faace2a 1711 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1712 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1713 __func__));
1714 return 1;
635374e7
EM
1715 }
1716
1717 raid_device->num_pds = num_pds;
1718 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1719 sizeof(Mpi2RaidVol0PhysDisk_t));
1720 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1721 if (!vol_pg0) {
6faace2a 1722 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1723 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1724 __func__));
1725 return 1;
635374e7
EM
1726 }
1727
1728 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1729 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
6faace2a 1730 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1731 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1732 __func__));
635374e7 1733 kfree(vol_pg0);
6faace2a 1734 return 1;
635374e7
EM
1735 }
1736
1737 raid_device->volume_type = vol_pg0->VolumeType;
1738
1739 /* figure out what the underlying devices are by
1740 * obtaining the device_info bits for the 1st device
1741 */
1742 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1743 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1744 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1745 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1746 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1747 le16_to_cpu(pd_pg0.DevHandle)))) {
1748 raid_device->device_info =
1749 le32_to_cpu(sas_device_pg0.DeviceInfo);
1750 }
1751 }
1752
1753 kfree(vol_pg0);
6faace2a 1754 return 0;
635374e7 1755}
0bdccdb0
KD
1756/**
1757 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1758 * @ioc: per adapter object
1759 */
1760static void
1761_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1762{
1763 Mpi2RaidVolPage1_t vol_pg1;
1764 Mpi2ConfigReply_t mpi_reply;
1765 struct _raid_device *raid_device;
1766 u16 handle;
1767 u16 ioc_status;
09da0b32 1768 unsigned long flags;
0bdccdb0
KD
1769
1770 handle = 0xFFFF;
1771 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1772 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1773 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1774 MPI2_IOCSTATUS_MASK;
1775 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1776 break;
1777 handle = le16_to_cpu(vol_pg1.DevHandle);
09da0b32 1778 spin_lock_irqsave(&ioc->raid_device_lock, flags);
0bdccdb0
KD
1779 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1780 if (raid_device)
1781 raid_device->direct_io_enabled = 0;
09da0b32 1782 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
0bdccdb0
KD
1783 }
1784 return;
1785}
1786
1787
1788/**
1789 * _scsih_get_num_volumes - Get number of volumes in the ioc
1790 * @ioc: per adapter object
1791 */
1792static u8
1793_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1794{
1795 Mpi2RaidVolPage1_t vol_pg1;
1796 Mpi2ConfigReply_t mpi_reply;
1797 u16 handle;
1798 u8 vol_cnt = 0;
1799 u16 ioc_status;
1800
1801 handle = 0xFFFF;
1802 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1803 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1804 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1805 MPI2_IOCSTATUS_MASK;
1806 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1807 break;
1808 vol_cnt++;
1809 handle = le16_to_cpu(vol_pg1.DevHandle);
1810 }
1811 return vol_cnt;
1812}
1813
1814
1815/**
1816 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1817 * @ioc: per adapter object
1818 * @raid_device: the raid_device object
1819 */
1820static void
1821_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1822 struct _raid_device *raid_device)
1823{
1824 Mpi2RaidVolPage0_t *vol_pg0;
1825 Mpi2RaidPhysDiskPage0_t pd_pg0;
1826 Mpi2ConfigReply_t mpi_reply;
1827 u16 sz;
1828 u8 num_pds, count;
ba96bd0b 1829 unsigned long stripe_sz, block_sz;
1830 u8 stripe_exp, block_exp;
1831 u64 dev_max_lba;
0bdccdb0
KD
1832
1833 if (!ioc->is_warpdrive)
1834 return;
1835
1836 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1837 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1838 "globally as drives are exposed\n", ioc->name);
1839 return;
1840 }
1841 if (_scsih_get_num_volumes(ioc) > 1) {
1842 _scsih_disable_ddio(ioc);
1843 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1844 "globally as number of drives > 1\n", ioc->name);
1845 return;
1846 }
1847 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1848 &num_pds)) || !num_pds) {
1849 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1850 "Failure in computing number of drives\n", ioc->name);
1851 return;
1852 }
1853
1854 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1855 sizeof(Mpi2RaidVol0PhysDisk_t));
1856 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1857 if (!vol_pg0) {
1858 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1859 "Memory allocation failure for RVPG0\n", ioc->name);
1860 return;
1861 }
1862
1863 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1864 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1865 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1866 "Failure in retrieving RVPG0\n", ioc->name);
1867 kfree(vol_pg0);
1868 return;
1869 }
1870
1871 /*
1872 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1873 * assumed for WARPDRIVE, disable direct I/O
1874 */
1875 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1876 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1877 "for the drive with handle(0x%04x): num_mem=%d, "
1878 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1879 num_pds, MPT_MAX_WARPDRIVE_PDS);
1880 kfree(vol_pg0);
1881 return;
1882 }
1883 for (count = 0; count < num_pds; count++) {
1884 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1885 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1886 vol_pg0->PhysDisk[count].PhysDiskNum) ||
d838c36c 1887 le16_to_cpu(pd_pg0.DevHandle) ==
1888 MPT2SAS_INVALID_DEVICE_HANDLE) {
0bdccdb0
KD
1889 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1890 "disabled for the drive with handle(0x%04x) member"
1891 "handle retrieval failed for member number=%d\n",
1892 ioc->name, raid_device->handle,
1893 vol_pg0->PhysDisk[count].PhysDiskNum);
1894 goto out_error;
1895 }
ba96bd0b 1896 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1897 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1898 if (dev_max_lba >> 32) {
1899 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1900 "disabled for the drive with handle(0x%04x) member"
1901 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1902 ioc->name, raid_device->handle,
1903 le16_to_cpu(pd_pg0.DevHandle),
1904 (unsigned long long)dev_max_lba);
1905 goto out_error;
1906 }
1907
0bdccdb0
KD
1908 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1909 }
1910
1911 /*
1912 * Assumption for WD: Direct I/O is not supported if the volume is
ba96bd0b 1913 * not RAID0
0bdccdb0 1914 */
ba96bd0b 1915 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
0bdccdb0
KD
1916 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1917 "for the drive with handle(0x%04x): type=%d, "
1918 "s_sz=%uK, blk_size=%u\n", ioc->name,
1919 raid_device->handle, raid_device->volume_type,
ba96bd0b 1920 (le32_to_cpu(vol_pg0->StripeSize) *
1921 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
0bdccdb0
KD
1922 le16_to_cpu(vol_pg0->BlockSize));
1923 goto out_error;
1924 }
1925
ba96bd0b 1926 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1927 stripe_exp = find_first_bit(&stripe_sz, 32);
1928 if (stripe_exp == 32) {
0bdccdb0 1929 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
ba96bd0b 1930 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1931 ioc->name, raid_device->handle,
1932 (le32_to_cpu(vol_pg0->StripeSize) *
1933 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
0bdccdb0
KD
1934 goto out_error;
1935 }
ba96bd0b 1936 raid_device->stripe_exponent = stripe_exp;
1937 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1938 block_exp = find_first_bit(&block_sz, 16);
1939 if (block_exp == 16) {
0bdccdb0 1940 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
ba96bd0b 1941 "for the drive with handle(0x%04x) invalid block sz %u\n",
0bdccdb0 1942 ioc->name, raid_device->handle,
ba96bd0b 1943 le16_to_cpu(vol_pg0->BlockSize));
0bdccdb0
KD
1944 goto out_error;
1945 }
ba96bd0b 1946 raid_device->block_exponent = block_exp;
0bdccdb0
KD
1947 raid_device->direct_io_enabled = 1;
1948
1949 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1950 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1951 /*
1952 * WARPDRIVE: Though the following fields are not used for direct IO,
1953 * stored for future purpose:
1954 */
1955 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1956 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1957 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1958
1959
1960 kfree(vol_pg0);
1961 return;
1962
1963out_error:
1964 raid_device->direct_io_enabled = 0;
1965 for (count = 0; count < num_pds; count++)
1966 raid_device->pd_handle[count] = 0;
1967 kfree(vol_pg0);
1968 return;
1969}
635374e7 1970
84f0b04a
KD
1971/**
1972 * _scsih_enable_tlr - setting TLR flags
1973 * @ioc: per adapter object
1974 * @sdev: scsi device struct
1975 *
1976 * Enabling Transaction Layer Retries for tape devices when
1977 * vpd page 0x90 is present
1978 *
1979 */
1980static void
1981_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1982{
1983 /* only for TAPE */
1984 if (sdev->type != TYPE_TAPE)
1985 return;
1986
1987 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1988 return;
1989
1990 sas_enable_tlr(sdev);
1991 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1992 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1993 return;
1994
1995}
1996
635374e7 1997/**
d5d135b3 1998 * _scsih_slave_configure - device configure routine.
635374e7
EM
1999 * @sdev: scsi device struct
2000 *
2001 * Returns 0 if ok. Any other return is assumed to be an error and
2002 * the device is ignored.
2003 */
2004static int
d5d135b3 2005_scsih_slave_configure(struct scsi_device *sdev)
635374e7
EM
2006{
2007 struct Scsi_Host *shost = sdev->host;
2008 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2009 struct MPT2SAS_DEVICE *sas_device_priv_data;
2010 struct MPT2SAS_TARGET *sas_target_priv_data;
2011 struct _sas_device *sas_device;
2012 struct _raid_device *raid_device;
2013 unsigned long flags;
2014 int qdepth;
2015 u8 ssp_target = 0;
2016 char *ds = "";
2017 char *r_level = "";
09da0b32 2018 u16 handle, volume_handle = 0;
2019 u64 volume_wwid = 0;
635374e7
EM
2020
2021 qdepth = 1;
2022 sas_device_priv_data = sdev->hostdata;
2023 sas_device_priv_data->configured_lun = 1;
2024 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2025 sas_target_priv_data = sas_device_priv_data->sas_target;
09da0b32 2026 handle = sas_target_priv_data->handle;
635374e7
EM
2027
2028 /* raid volume handling */
2029 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2030
2031 spin_lock_irqsave(&ioc->raid_device_lock, flags);
09da0b32 2032 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
635374e7
EM
2033 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2034 if (!raid_device) {
6faace2a 2035 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2036 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2037 __LINE__, __func__));
2038 return 1;
635374e7
EM
2039 }
2040
6faace2a 2041 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2042 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2043 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2044 __LINE__, __func__));
2045 return 1;
2046 }
0bdccdb0
KD
2047 /*
2048 * WARPDRIVE: Initialize the required data for Direct IO
2049 */
2050 _scsih_init_warpdrive_properties(ioc, raid_device);
2051
635374e7
EM
2052 /* RAID Queue Depth Support
2053 * IS volume = underlying qdepth of drive type, either
2054 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2055 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2056 */
2057 if (raid_device->device_info &
2058 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2059 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2060 ds = "SSP";
2061 } else {
2062 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2063 if (raid_device->device_info &
2064 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2065 ds = "SATA";
2066 else
2067 ds = "STP";
2068 }
2069
2070 switch (raid_device->volume_type) {
2071 case MPI2_RAID_VOL_TYPE_RAID0:
2072 r_level = "RAID0";
2073 break;
2074 case MPI2_RAID_VOL_TYPE_RAID1E:
2075 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
ed79f128 2076 if (ioc->manu_pg10.OEMIdentifier &&
c97951ec 2077 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
ed79f128
KD
2078 MFG10_GF0_R10_DISPLAY) &&
2079 !(raid_device->num_pds % 2))
2080 r_level = "RAID10";
2081 else
2082 r_level = "RAID1E";
635374e7
EM
2083 break;
2084 case MPI2_RAID_VOL_TYPE_RAID1:
2085 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2086 r_level = "RAID1";
2087 break;
2088 case MPI2_RAID_VOL_TYPE_RAID10:
2089 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2090 r_level = "RAID10";
2091 break;
2092 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2093 default:
2094 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2095 r_level = "RAIDX";
2096 break;
2097 }
2098
0bdccdb0
KD
2099 if (!ioc->hide_ir_msg)
2100 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2101 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2102 r_level, raid_device->handle,
2103 (unsigned long long)raid_device->wwid,
2104 raid_device->num_pds, ds);
e881a172 2105 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
f7c95ef0 2106 /* raid transport support */
0bdccdb0 2107 if (!ioc->is_warpdrive)
09da0b32 2108 _scsih_set_level(sdev, raid_device->volume_type);
635374e7
EM
2109 return 0;
2110 }
2111
2112 /* non-raid handling */
09da0b32 2113 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2114 if (mpt2sas_config_get_volume_handle(ioc, handle,
2115 &volume_handle)) {
2116 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2117 "failure at %s:%d/%s()!\n", ioc->name,
2118 __FILE__, __LINE__, __func__));
2119 return 1;
635374e7 2120 }
09da0b32 2121 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2122 volume_handle, &volume_wwid)) {
2123 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2124 "failure at %s:%d/%s()!\n", ioc->name,
2125 __FILE__, __LINE__, __func__));
2126 return 1;
635374e7 2127 }
09da0b32 2128 }
635374e7 2129
09da0b32 2130 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2131 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2132 sas_device_priv_data->sas_target->sas_address);
2133 if (!sas_device) {
2134 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6faace2a 2135 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
09da0b32 2136 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2137 __LINE__, __func__));
6faace2a 2138 return 1;
635374e7 2139 }
09da0b32 2140 sas_device->volume_handle = volume_handle;
2141 sas_device->volume_wwid = volume_wwid;
2142 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2143 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2144 ssp_target = 1;
2145 ds = "SSP";
2146 } else {
2147 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2148 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2149 ds = "STP";
2150 else if (sas_device->device_info &
2151 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2152 ds = "SATA";
2153 }
2154 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2155 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2156 ds, sas_device->handle,
2157 (unsigned long long)sas_device->sas_address,
2158 sas_device->phy,
2159 (unsigned long long)sas_device->device_name);
2160 sdev_printk(KERN_INFO, sdev, "%s: "
2161 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2162 (unsigned long long) sas_device->enclosure_logical_id,
2163 sas_device->slot);
2164
2165 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2166 if (!ssp_target)
2167 _scsih_display_sata_capabilities(ioc, handle, sdev);
2168
635374e7 2169
e881a172 2170 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
635374e7 2171
84f0b04a 2172 if (ssp_target) {
635374e7 2173 sas_read_port_mode_page(sdev);
84f0b04a
KD
2174 _scsih_enable_tlr(ioc, sdev);
2175 }
635374e7
EM
2176 return 0;
2177}
2178
2179/**
d5d135b3 2180 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
635374e7
EM
2181 * @sdev: scsi device struct
2182 * @bdev: pointer to block device context
2183 * @capacity: device size (in 512 byte sectors)
2184 * @params: three element array to place output:
2185 * params[0] number of heads (max 255)
2186 * params[1] number of sectors (max 63)
2187 * params[2] number of cylinders
2188 *
2189 * Return nothing.
2190 */
2191static int
d5d135b3 2192_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
635374e7
EM
2193 sector_t capacity, int params[])
2194{
2195 int heads;
2196 int sectors;
2197 sector_t cylinders;
2198 ulong dummy;
2199
2200 heads = 64;
2201 sectors = 32;
2202
2203 dummy = heads * sectors;
2204 cylinders = capacity;
2205 sector_div(cylinders, dummy);
2206
2207 /*
2208 * Handle extended translation size for logical drives
2209 * > 1Gb
2210 */
2211 if ((ulong)capacity >= 0x200000) {
2212 heads = 255;
2213 sectors = 63;
2214 dummy = heads * sectors;
2215 cylinders = capacity;
2216 sector_div(cylinders, dummy);
2217 }
2218
2219 /* return result */
2220 params[0] = heads;
2221 params[1] = sectors;
2222 params[2] = cylinders;
2223
2224 return 0;
2225}
2226
2227/**
2228 * _scsih_response_code - translation of device response code
2229 * @ioc: per adapter object
2230 * @response_code: response code returned by the device
2231 *
2232 * Return nothing.
2233 */
2234static void
2235_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2236{
2237 char *desc;
2238
2239 switch (response_code) {
2240 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2241 desc = "task management request completed";
2242 break;
2243 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2244 desc = "invalid frame";
2245 break;
2246 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2247 desc = "task management request not supported";
2248 break;
2249 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2250 desc = "task management request failed";
2251 break;
2252 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2253 desc = "task management request succeeded";
2254 break;
2255 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2256 desc = "invalid lun";
2257 break;
2258 case 0xA:
2259 desc = "overlapped tag attempted";
2260 break;
2261 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2262 desc = "task queued, however not sent to target";
2263 break;
2264 default:
2265 desc = "unknown";
2266 break;
2267 }
2268 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2269 ioc->name, response_code, desc);
2270}
2271
2272/**
d5d135b3 2273 * _scsih_tm_done - tm completion routine
635374e7
EM
2274 * @ioc: per adapter object
2275 * @smid: system request message index
7b936b02 2276 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
2277 * @reply: reply message frame(lower 32bit addr)
2278 * Context: none.
2279 *
2280 * The callback handler when using scsih_issue_tm.
2281 *
77e63ed4
KD
2282 * Return 1 meaning mf should be freed from _base_interrupt
2283 * 0 means the mf is freed from this function.
635374e7 2284 */
77e63ed4 2285static u8
7b936b02 2286_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
2287{
2288 MPI2DefaultReply_t *mpi_reply;
2289
2290 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
77e63ed4 2291 return 1;
635374e7 2292 if (ioc->tm_cmds.smid != smid)
77e63ed4 2293 return 1;
911ae943 2294 mpt2sas_base_flush_reply_queues(ioc);
635374e7
EM
2295 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2296 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2297 if (mpi_reply) {
2298 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2299 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2300 }
2301 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2302 complete(&ioc->tm_cmds.done);
77e63ed4 2303 return 1;
635374e7
EM
2304}
2305
2306/**
2307 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2308 * @ioc: per adapter object
2309 * @handle: device handle
2310 *
2311 * During taskmangement request, we need to freeze the device queue.
2312 */
2313void
2314mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2315{
2316 struct MPT2SAS_DEVICE *sas_device_priv_data;
2317 struct scsi_device *sdev;
2318 u8 skip = 0;
2319
2320 shost_for_each_device(sdev, ioc->shost) {
2321 if (skip)
2322 continue;
2323 sas_device_priv_data = sdev->hostdata;
2324 if (!sas_device_priv_data)
2325 continue;
2326 if (sas_device_priv_data->sas_target->handle == handle) {
2327 sas_device_priv_data->sas_target->tm_busy = 1;
2328 skip = 1;
2329 ioc->ignore_loginfos = 1;
2330 }
2331 }
2332}
2333
2334/**
2335 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2336 * @ioc: per adapter object
2337 * @handle: device handle
2338 *
2339 * During taskmangement request, we need to freeze the device queue.
2340 */
2341void
2342mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2343{
2344 struct MPT2SAS_DEVICE *sas_device_priv_data;
2345 struct scsi_device *sdev;
2346 u8 skip = 0;
2347
2348 shost_for_each_device(sdev, ioc->shost) {
2349 if (skip)
2350 continue;
2351 sas_device_priv_data = sdev->hostdata;
2352 if (!sas_device_priv_data)
2353 continue;
2354 if (sas_device_priv_data->sas_target->handle == handle) {
2355 sas_device_priv_data->sas_target->tm_busy = 0;
2356 skip = 1;
2357 ioc->ignore_loginfos = 0;
2358 }
2359 }
2360}
2361
8ed9a03a 2362
635374e7
EM
2363/**
2364 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2365 * @ioc: per adapter struct
2366 * @device_handle: device handle
8ed9a03a
KD
2367 * @channel: the channel assigned by the OS
2368 * @id: the id assigned by the OS
635374e7
EM
2369 * @lun: lun number
2370 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2371 * @smid_task: smid assigned to the task
2372 * @timeout: timeout in seconds
f93213de 2373 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
8ed9a03a 2374 * Context: user
635374e7
EM
2375 *
2376 * A generic API for sending task management requests to firmware.
2377 *
635374e7
EM
2378 * The callback index is set inside `ioc->tm_cb_idx`.
2379 *
8ed9a03a 2380 * Return SUCCESS or FAILED.
635374e7 2381 */
8ed9a03a
KD
2382int
2383mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2384 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2e45c8b7 2385 enum mutex_type m_type)
635374e7
EM
2386{
2387 Mpi2SCSITaskManagementRequest_t *mpi_request;
2388 Mpi2SCSITaskManagementReply_t *mpi_reply;
2389 u16 smid = 0;
2390 u32 ioc_state;
2391 unsigned long timeleft;
f93213de 2392 struct scsiio_tracker *scsi_lookup = NULL;
8ed9a03a 2393 int rc;
635374e7 2394
f93213de
KD
2395 if (m_type == TM_MUTEX_ON)
2396 mutex_lock(&ioc->tm_cmds.mutex);
155dd4c7
KD
2397 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2398 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2399 __func__, ioc->name);
8ed9a03a
KD
2400 rc = FAILED;
2401 goto err_out;
155dd4c7
KD
2402 }
2403
3cb5469a
EM
2404 if (ioc->shost_recovery || ioc->remove_host ||
2405 ioc->pci_error_recovery) {
635374e7
EM
2406 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2407 __func__, ioc->name);
8ed9a03a
KD
2408 rc = FAILED;
2409 goto err_out;
635374e7 2410 }
635374e7
EM
2411
2412 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2413 if (ioc_state & MPI2_DOORBELL_USED) {
eabb08ad 2414 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
635374e7 2415 "active!\n", ioc->name));
f93213de 2416 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8ed9a03a 2417 FORCE_BIG_HAMMER);
f93213de 2418 rc = (!rc) ? SUCCESS : FAILED;
8ed9a03a 2419 goto err_out;
635374e7
EM
2420 }
2421
2422 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2423 mpt2sas_base_fault_info(ioc, ioc_state &
2424 MPI2_DOORBELL_DATA_MASK);
f93213de 2425 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8ed9a03a 2426 FORCE_BIG_HAMMER);
f93213de 2427 rc = (!rc) ? SUCCESS : FAILED;
8ed9a03a 2428 goto err_out;
635374e7
EM
2429 }
2430
595bb0bd 2431 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
635374e7
EM
2432 if (!smid) {
2433 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2434 ioc->name, __func__);
8ed9a03a
KD
2435 rc = FAILED;
2436 goto err_out;
635374e7
EM
2437 }
2438
f93213de
KD
2439 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2440 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2441
635374e7 2442 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
595bb0bd
KD
2443 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2444 smid_task));
635374e7
EM
2445 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2446 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2447 ioc->tm_cmds.smid = smid;
2448 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
f93213de 2449 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
635374e7
EM
2450 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2451 mpi_request->DevHandle = cpu_to_le16(handle);
2452 mpi_request->TaskType = type;
2453 mpi_request->TaskMID = cpu_to_le16(smid_task);
2454 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2455 mpt2sas_scsih_set_tm_flag(ioc, handle);
5b768581 2456 init_completion(&ioc->tm_cmds.done);
7b936b02 2457 mpt2sas_base_put_smid_hi_priority(ioc, smid);
635374e7 2458 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
635374e7
EM
2459 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2460 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2461 ioc->name, __func__);
2462 _debug_dump_mf(mpi_request,
2463 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
8ed9a03a 2464 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
f93213de 2465 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8ed9a03a 2466 FORCE_BIG_HAMMER);
f93213de 2467 rc = (!rc) ? SUCCESS : FAILED;
8ed9a03a
KD
2468 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2469 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2470 goto err_out;
2471 }
635374e7
EM
2472 }
2473
2474 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2475 mpi_reply = ioc->tm_cmds.reply;
2476 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2477 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2478 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2479 le32_to_cpu(mpi_reply->IOCLogInfo),
2480 le32_to_cpu(mpi_reply->TerminationCount)));
8ed9a03a 2481 if (ioc->logging_level & MPT_DEBUG_TM) {
635374e7 2482 _scsih_response_code(ioc, mpi_reply->ResponseCode);
8ed9a03a
KD
2483 if (mpi_reply->IOCStatus)
2484 _debug_dump_mf(mpi_request,
2485 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2486 }
635374e7 2487 }
8ed9a03a 2488
8ed9a03a
KD
2489 switch (type) {
2490 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
f93213de
KD
2491 rc = SUCCESS;
2492 if (scsi_lookup->scmd == NULL)
2493 break;
2494 rc = FAILED;
8ed9a03a
KD
2495 break;
2496
2497 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2498 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2499 rc = FAILED;
2500 else
2501 rc = SUCCESS;
2502 break;
2503
f93213de 2504 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
8ed9a03a
KD
2505 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2506 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2507 rc = FAILED;
2508 else
2509 rc = SUCCESS;
2510 break;
f93213de
KD
2511 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2512 rc = SUCCESS;
2513 break;
2514 default:
2515 rc = FAILED;
2516 break;
8ed9a03a
KD
2517 }
2518
8ed9a03a
KD
2519 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2520 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
f93213de
KD
2521 if (m_type == TM_MUTEX_ON)
2522 mutex_unlock(&ioc->tm_cmds.mutex);
8ed9a03a
KD
2523
2524 return rc;
2525
2526 err_out:
f93213de
KD
2527 if (m_type == TM_MUTEX_ON)
2528 mutex_unlock(&ioc->tm_cmds.mutex);
8ed9a03a 2529 return rc;
635374e7
EM
2530}
2531
8e864a81
KD
2532/**
2533 * _scsih_tm_display_info - displays info about the device
2534 * @ioc: per adapter struct
2535 * @scmd: pointer to scsi command object
2536 *
2537 * Called by task management callback handlers.
2538 */
2539static void
2540_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2541{
2542 struct scsi_target *starget = scmd->device->sdev_target;
2543 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2544 struct _sas_device *sas_device = NULL;
2545 unsigned long flags;
0bdccdb0 2546 char *device_str = NULL;
8e864a81
KD
2547
2548 if (!priv_target)
2549 return;
0bdccdb0
KD
2550 if (ioc->hide_ir_msg)
2551 device_str = "WarpDrive";
2552 else
2553 device_str = "volume";
8e864a81
KD
2554
2555 scsi_print_command(scmd);
2556 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
0bdccdb0
KD
2557 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2558 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2559 device_str, (unsigned long long)priv_target->sas_address);
8e864a81
KD
2560 } else {
2561 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2562 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2563 priv_target->sas_address);
2564 if (sas_device) {
2565 if (priv_target->flags &
2566 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2567 starget_printk(KERN_INFO, starget,
2568 "volume handle(0x%04x), "
2569 "volume wwid(0x%016llx)\n",
2570 sas_device->volume_handle,
2571 (unsigned long long)sas_device->volume_wwid);
2572 }
2573 starget_printk(KERN_INFO, starget,
2574 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2575 sas_device->handle,
2576 (unsigned long long)sas_device->sas_address,
2577 sas_device->phy);
2578 starget_printk(KERN_INFO, starget,
2579 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2580 (unsigned long long)sas_device->enclosure_logical_id,
2581 sas_device->slot);
2582 }
2583 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2584 }
2585}
2586
635374e7 2587/**
d5d135b3 2588 * _scsih_abort - eh threads main abort routine
8e864a81 2589 * @scmd: pointer to scsi command object
635374e7
EM
2590 *
2591 * Returns SUCCESS if command aborted else FAILED
2592 */
2593static int
d5d135b3 2594_scsih_abort(struct scsi_cmnd *scmd)
635374e7
EM
2595{
2596 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2597 struct MPT2SAS_DEVICE *sas_device_priv_data;
2598 u16 smid;
2599 u16 handle;
2600 int r;
635374e7 2601
8e864a81
KD
2602 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2603 "scmd(%p)\n", scmd);
2604 _scsih_tm_display_info(ioc, scmd);
635374e7
EM
2605
2606 sas_device_priv_data = scmd->device->hostdata;
2607 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
8e864a81
KD
2608 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2609 "scmd(%p)\n", scmd);
635374e7
EM
2610 scmd->result = DID_NO_CONNECT << 16;
2611 scmd->scsi_done(scmd);
2612 r = SUCCESS;
2613 goto out;
2614 }
2615
2616 /* search for the command */
2617 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2618 if (!smid) {
2619 scmd->result = DID_RESET << 16;
2620 r = SUCCESS;
2621 goto out;
2622 }
2623
2624 /* for hidden raid components and volumes this is not supported */
2625 if (sas_device_priv_data->sas_target->flags &
2626 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2627 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2628 scmd->result = DID_RESET << 16;
2629 r = FAILED;
2630 goto out;
2631 }
2632
fa7f3167
KD
2633 mpt2sas_halt_firmware(ioc);
2634
635374e7 2635 handle = sas_device_priv_data->sas_target->handle;
8ed9a03a
KD
2636 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2637 scmd->device->id, scmd->device->lun,
2e45c8b7 2638 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
635374e7
EM
2639
2640 out:
8e864a81
KD
2641 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2642 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
635374e7
EM
2643 return r;
2644}
2645
635374e7 2646/**
d5d135b3 2647 * _scsih_dev_reset - eh threads main device reset routine
8e864a81 2648 * @scmd: pointer to scsi command object
635374e7
EM
2649 *
2650 * Returns SUCCESS if command aborted else FAILED
2651 */
2652static int
d5d135b3 2653_scsih_dev_reset(struct scsi_cmnd *scmd)
635374e7
EM
2654{
2655 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2656 struct MPT2SAS_DEVICE *sas_device_priv_data;
2657 struct _sas_device *sas_device;
2658 unsigned long flags;
2659 u16 handle;
2660 int r;
2661
8e864a81
KD
2662 struct scsi_target *starget = scmd->device->sdev_target;
2663
37aaa78b 2664 starget_printk(KERN_INFO, starget, "attempting device reset! "
8e864a81
KD
2665 "scmd(%p)\n", scmd);
2666 _scsih_tm_display_info(ioc, scmd);
635374e7
EM
2667
2668 sas_device_priv_data = scmd->device->hostdata;
2669 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
37aaa78b 2670 starget_printk(KERN_INFO, starget, "device been deleted! "
8e864a81 2671 "scmd(%p)\n", scmd);
635374e7
EM
2672 scmd->result = DID_NO_CONNECT << 16;
2673 scmd->scsi_done(scmd);
2674 r = SUCCESS;
2675 goto out;
2676 }
2677
2678 /* for hidden raid components obtain the volume_handle */
2679 handle = 0;
2680 if (sas_device_priv_data->sas_target->flags &
2681 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2682 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2683 sas_device = _scsih_sas_device_find_by_handle(ioc,
2684 sas_device_priv_data->sas_target->handle);
2685 if (sas_device)
2686 handle = sas_device->volume_handle;
2687 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2688 } else
2689 handle = sas_device_priv_data->sas_target->handle;
2690
2691 if (!handle) {
2692 scmd->result = DID_RESET << 16;
2693 r = FAILED;
2694 goto out;
2695 }
2696
8ed9a03a
KD
2697 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2698 scmd->device->id, scmd->device->lun,
2e45c8b7 2699 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
993e0da7
EM
2700
2701 out:
8e864a81
KD
2702 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2703 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
993e0da7
EM
2704 return r;
2705}
2706
2707/**
d5d135b3 2708 * _scsih_target_reset - eh threads main target reset routine
8e864a81 2709 * @scmd: pointer to scsi command object
993e0da7
EM
2710 *
2711 * Returns SUCCESS if command aborted else FAILED
2712 */
2713static int
d5d135b3 2714_scsih_target_reset(struct scsi_cmnd *scmd)
993e0da7
EM
2715{
2716 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2717 struct MPT2SAS_DEVICE *sas_device_priv_data;
2718 struct _sas_device *sas_device;
2719 unsigned long flags;
2720 u16 handle;
2721 int r;
8e864a81 2722 struct scsi_target *starget = scmd->device->sdev_target;
993e0da7 2723
8e864a81
KD
2724 starget_printk(KERN_INFO, starget, "attempting target reset! "
2725 "scmd(%p)\n", scmd);
2726 _scsih_tm_display_info(ioc, scmd);
993e0da7
EM
2727
2728 sas_device_priv_data = scmd->device->hostdata;
2729 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
8e864a81
KD
2730 starget_printk(KERN_INFO, starget, "target been deleted! "
2731 "scmd(%p)\n", scmd);
993e0da7
EM
2732 scmd->result = DID_NO_CONNECT << 16;
2733 scmd->scsi_done(scmd);
2734 r = SUCCESS;
2735 goto out;
2736 }
2737
2738 /* for hidden raid components obtain the volume_handle */
2739 handle = 0;
2740 if (sas_device_priv_data->sas_target->flags &
2741 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2742 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2743 sas_device = _scsih_sas_device_find_by_handle(ioc,
2744 sas_device_priv_data->sas_target->handle);
2745 if (sas_device)
2746 handle = sas_device->volume_handle;
2747 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2748 } else
2749 handle = sas_device_priv_data->sas_target->handle;
2750
2751 if (!handle) {
2752 scmd->result = DID_RESET << 16;
2753 r = FAILED;
2754 goto out;
2755 }
2756
8ed9a03a
KD
2757 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2758 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2e45c8b7 2759 30, TM_MUTEX_ON);
635374e7
EM
2760
2761 out:
8e864a81
KD
2762 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2763 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
635374e7
EM
2764 return r;
2765}
2766
2767/**
595bb0bd 2768 * _scsih_host_reset - eh threads main host reset routine
8e864a81 2769 * @scmd: pointer to scsi command object
635374e7
EM
2770 *
2771 * Returns SUCCESS if command aborted else FAILED
2772 */
2773static int
d5d135b3 2774_scsih_host_reset(struct scsi_cmnd *scmd)
635374e7
EM
2775{
2776 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2777 int r, retval;
2778
2779 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2780 ioc->name, scmd);
2781 scsi_print_command(scmd);
2782
2783 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2784 FORCE_BIG_HAMMER);
2785 r = (retval < 0) ? FAILED : SUCCESS;
2786 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2787 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2788
2789 return r;
2790}
2791
2792/**
2793 * _scsih_fw_event_add - insert and queue up fw_event
2794 * @ioc: per adapter object
2795 * @fw_event: object describing the event
2796 * Context: This function will acquire ioc->fw_event_lock.
2797 *
2798 * This adds the firmware event object into link list, then queues it up to
2799 * be processed from user context.
2800 *
2801 * Return nothing.
2802 */
2803static void
2804_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2805{
2806 unsigned long flags;
2807
2808 if (ioc->firmware_event_thread == NULL)
2809 return;
2810
2811 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2812 list_add_tail(&fw_event->list, &ioc->fw_event_list);
f1c35e6a
KD
2813 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2814 queue_delayed_work(ioc->firmware_event_thread,
2815 &fw_event->delayed_work, 0);
635374e7
EM
2816 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2817}
2818
2819/**
2820 * _scsih_fw_event_free - delete fw_event
2821 * @ioc: per adapter object
2822 * @fw_event: object describing the event
2823 * Context: This function will acquire ioc->fw_event_lock.
2824 *
2825 * This removes firmware event object from link list, frees associated memory.
2826 *
2827 * Return nothing.
2828 */
2829static void
2830_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2831 *fw_event)
2832{
2833 unsigned long flags;
2834
2835 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2836 list_del(&fw_event->list);
635374e7
EM
2837 kfree(fw_event);
2838 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2839}
2840
f1c35e6a 2841
635374e7 2842/**
921cd802 2843 * _scsih_error_recovery_delete_devices - remove devices not responding
635374e7 2844 * @ioc: per adapter object
635374e7
EM
2845 *
2846 * Return nothing.
2847 */
2848static void
921cd802 2849_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
635374e7 2850{
f1c35e6a 2851 struct fw_event_work *fw_event;
635374e7 2852
921cd802 2853 if (ioc->is_driver_loading)
f1c35e6a 2854 return;
181a9d79
DC
2855
2856 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2857 if (!fw_event)
2858 return;
2859
921cd802 2860 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2861 fw_event->ioc = ioc;
2862 _scsih_fw_event_add(ioc, fw_event);
2863}
2864
2865/**
2866 * mpt2sas_port_enable_complete - port enable completed (fake event)
2867 * @ioc: per adapter object
2868 *
2869 * Return nothing.
2870 */
2871void
2872mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2873{
2874 struct fw_event_work *fw_event;
2875
f1c35e6a
KD
2876 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2877 if (!fw_event)
2878 return;
921cd802 2879 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
f1c35e6a
KD
2880 fw_event->ioc = ioc;
2881 _scsih_fw_event_add(ioc, fw_event);
635374e7
EM
2882}
2883
2884/**
f1c35e6a 2885 * _scsih_fw_event_cleanup_queue - cleanup event queue
635374e7
EM
2886 * @ioc: per adapter object
2887 *
f1c35e6a
KD
2888 * Walk the firmware event queue, either killing timers, or waiting
2889 * for outstanding events to complete
635374e7
EM
2890 *
2891 * Return nothing.
2892 */
2893static void
f1c35e6a 2894_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
635374e7 2895{
f1c35e6a 2896 struct fw_event_work *fw_event, *next;
635374e7 2897
f1c35e6a
KD
2898 if (list_empty(&ioc->fw_event_list) ||
2899 !ioc->firmware_event_thread || in_interrupt())
2900 return;
635374e7 2901
f1c35e6a 2902 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
02b77081 2903 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
f1c35e6a
KD
2904 _scsih_fw_event_free(ioc, fw_event);
2905 continue;
2906 }
f1c35e6a 2907 }
635374e7
EM
2908}
2909
f93213de
KD
2910/**
2911 * _scsih_ublock_io_all_device - unblock every device
2912 * @ioc: per adapter object
2913 *
2914 * change the device state from block to running
2915 */
2916static void
2917_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2918{
2919 struct MPT2SAS_DEVICE *sas_device_priv_data;
2920 struct scsi_device *sdev;
2921
2922 shost_for_each_device(sdev, ioc->shost) {
2923 sas_device_priv_data = sdev->hostdata;
2924 if (!sas_device_priv_data)
2925 continue;
2926 if (!sas_device_priv_data->block)
2927 continue;
2928 sas_device_priv_data->block = 0;
2929 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2930 "handle(0x%04x)\n",
2931 sas_device_priv_data->sas_target->handle));
5d9fb5cc 2932 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
f93213de
KD
2933 }
2934}
635374e7
EM
2935/**
2936 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2937 * @ioc: per adapter object
2938 * @handle: device handle
2939 *
2940 * During device pull we need to appropiately set the sdev state.
2941 */
2942static void
39af7a98 2943_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
635374e7
EM
2944{
2945 struct MPT2SAS_DEVICE *sas_device_priv_data;
2946 struct scsi_device *sdev;
2947
2948 shost_for_each_device(sdev, ioc->shost) {
2949 sas_device_priv_data = sdev->hostdata;
2950 if (!sas_device_priv_data)
2951 continue;
2952 if (!sas_device_priv_data->block)
2953 continue;
39af7a98 2954 if (sas_device_priv_data->sas_target->sas_address ==
2955 sas_address) {
635374e7
EM
2956 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2957 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
39af7a98 2958 "sas address(0x%016llx)\n", ioc->name,
2959 (unsigned long long)sas_address));
635374e7 2960 sas_device_priv_data->block = 0;
5d9fb5cc 2961 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
635374e7
EM
2962 }
2963 }
2964}
2965
f93213de
KD
2966/**
2967 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2968 * @ioc: per adapter object
2969 * @handle: device handle
2970 *
2971 * During device pull we need to appropiately set the sdev state.
2972 */
2973static void
2974_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2975{
2976 struct MPT2SAS_DEVICE *sas_device_priv_data;
2977 struct scsi_device *sdev;
2978
2979 shost_for_each_device(sdev, ioc->shost) {
2980 sas_device_priv_data = sdev->hostdata;
2981 if (!sas_device_priv_data)
2982 continue;
2983 if (sas_device_priv_data->block)
2984 continue;
2985 sas_device_priv_data->block = 1;
2986 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2987 "handle(0x%04x)\n",
2988 sas_device_priv_data->sas_target->handle));
2989 scsi_internal_device_block(sdev);
2990 }
2991}
2992
2993
635374e7
EM
2994/**
2995 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2996 * @ioc: per adapter object
2997 * @handle: device handle
2998 *
2999 * During device pull we need to appropiately set the sdev state.
3000 */
3001static void
3002_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3003{
3004 struct MPT2SAS_DEVICE *sas_device_priv_data;
3005 struct scsi_device *sdev;
3006
3007 shost_for_each_device(sdev, ioc->shost) {
3008 sas_device_priv_data = sdev->hostdata;
3009 if (!sas_device_priv_data)
3010 continue;
3011 if (sas_device_priv_data->block)
3012 continue;
3013 if (sas_device_priv_data->sas_target->handle == handle) {
3014 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
3015 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
3016 "handle(0x%04x)\n", ioc->name, handle));
3017 sas_device_priv_data->block = 1;
34a03bef 3018 scsi_internal_device_block(sdev);
635374e7
EM
3019 }
3020 }
3021}
3022
3023/**
3024 * _scsih_block_io_to_children_attached_to_ex
3025 * @ioc: per adapter object
3026 * @sas_expander: the sas_device object
3027 *
3028 * This routine set sdev state to SDEV_BLOCK for all devices
3029 * attached to this expander. This function called when expander is
3030 * pulled.
3031 */
3032static void
3033_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
3034 struct _sas_node *sas_expander)
3035{
3036 struct _sas_port *mpt2sas_port;
3037 struct _sas_device *sas_device;
3038 struct _sas_node *expander_sibling;
3039 unsigned long flags;
3040
3041 if (!sas_expander)
3042 return;
3043
3044 list_for_each_entry(mpt2sas_port,
3045 &sas_expander->sas_port_list, port_list) {
3046 if (mpt2sas_port->remote_identify.device_type ==
3047 SAS_END_DEVICE) {
3048 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3049 sas_device =
3050 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3051 mpt2sas_port->remote_identify.sas_address);
09da0b32 3052 if (sas_device)
3053 set_bit(sas_device->handle,
3054 ioc->blocking_handles);
635374e7 3055 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
635374e7
EM
3056 }
3057 }
3058
3059 list_for_each_entry(mpt2sas_port,
3060 &sas_expander->sas_port_list, port_list) {
3061
3062 if (mpt2sas_port->remote_identify.device_type ==
7f6f794d 3063 SAS_EDGE_EXPANDER_DEVICE ||
635374e7 3064 mpt2sas_port->remote_identify.device_type ==
7f6f794d 3065 SAS_FANOUT_EXPANDER_DEVICE) {
635374e7
EM
3066 expander_sibling =
3067 mpt2sas_scsih_expander_find_by_sas_address(
3068 ioc, mpt2sas_port->remote_identify.sas_address);
635374e7
EM
3069 _scsih_block_io_to_children_attached_to_ex(ioc,
3070 expander_sibling);
3071 }
3072 }
3073}
3074
3075/**
3076 * _scsih_block_io_to_children_attached_directly
3077 * @ioc: per adapter object
3078 * @event_data: topology change event data
3079 *
3080 * This routine set sdev state to SDEV_BLOCK for all devices
3081 * direct attached during device pull.
3082 */
3083static void
3084_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3085 Mpi2EventDataSasTopologyChangeList_t *event_data)
3086{
3087 int i;
3088 u16 handle;
3089 u16 reason_code;
3090 u8 phy_number;
3091
3092 for (i = 0; i < event_data->NumEntries; i++) {
3093 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3094 if (!handle)
3095 continue;
3096 phy_number = event_data->StartPhyNum + i;
3097 reason_code = event_data->PHY[i].PhyStatus &
3098 MPI2_EVENT_SAS_TOPO_RC_MASK;
3099 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3100 _scsih_block_io_device(ioc, handle);
3101 }
3102}
3103
77e63ed4
KD
3104/**
3105 * _scsih_tm_tr_send - send task management request
3106 * @ioc: per adapter object
3107 * @handle: device handle
3108 * Context: interrupt time.
3109 *
25985edc 3110 * This code is to initiate the device removal handshake protocol
77e63ed4
KD
3111 * with controller firmware. This function will issue target reset
3112 * using high priority request queue. It will send a sas iounit
25985edc 3113 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
77e63ed4
KD
3114 *
3115 * This is designed to send muliple task management request at the same
3116 * time to the fifo. If the fifo is full, we will append the request,
3117 * and process it in a future completion.
3118 */
3119static void
3120_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3121{
3122 Mpi2SCSITaskManagementRequest_t *mpi_request;
77e63ed4
KD
3123 u16 smid;
3124 struct _sas_device *sas_device;
f3db032f 3125 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3126 u64 sas_address = 0;
77e63ed4
KD
3127 unsigned long flags;
3128 struct _tr_list *delayed_tr;
f881cead 3129 u32 ioc_state;
77e63ed4 3130
f881cead 3131 if (ioc->remove_host) {
3132 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3133 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3134 return;
3135 } else if (ioc->pci_error_recovery) {
3136 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3137 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3138 handle));
3139 return;
3140 }
3141 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3142 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3143 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3144 "operational: handle(0x%04x)\n", __func__, ioc->name,
3145 handle));
77e63ed4
KD
3146 return;
3147 }
3148
f3eedd69
KD
3149 /* if PD, then return */
3150 if (test_bit(handle, ioc->pd_handles))
3151 return;
3152
77e63ed4
KD
3153 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3154 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
f3eedd69
KD
3155 if (sas_device && sas_device->starget &&
3156 sas_device->starget->hostdata) {
3157 sas_target_priv_data = sas_device->starget->hostdata;
3158 sas_target_priv_data->deleted = 1;
f3db032f 3159 sas_address = sas_device->sas_address;
1278b11f
KD
3160 }
3161 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
77e63ed4 3162
f3db032f 3163 if (sas_target_priv_data) {
3164 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3165 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3166 (unsigned long long)sas_address));
39af7a98 3167 _scsih_ublock_io_device(ioc, sas_address);
918134ef 3168 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
f3db032f 3169 }
3170
77e63ed4
KD
3171 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3172 if (!smid) {
3173 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3174 if (!delayed_tr)
3175 return;
3176 INIT_LIST_HEAD(&delayed_tr->list);
3177 delayed_tr->handle = handle;
1278b11f 3178 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
a28eb222 3179 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
1278b11f
KD
3180 "DELAYED:tr:handle(0x%04x), (open)\n",
3181 ioc->name, handle));
3182 return;
77e63ed4
KD
3183 }
3184
1278b11f
KD
3185 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3186 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3187 ioc->tm_tr_cb_idx));
77e63ed4
KD
3188 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3189 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3190 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3191 mpi_request->DevHandle = cpu_to_le16(handle);
3192 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
77e63ed4
KD
3193 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3194}
3195
3196
3197
3198/**
3199 * _scsih_sas_control_complete - completion routine
3200 * @ioc: per adapter object
3201 * @smid: system request message index
3202 * @msix_index: MSIX table index supplied by the OS
3203 * @reply: reply message frame(lower 32bit addr)
3204 * Context: interrupt time.
3205 *
25985edc 3206 * This is the sas iounit control completion routine.
77e63ed4 3207 * This code is part of the code to initiate the device removal
25985edc 3208 * handshake protocol with controller firmware.
77e63ed4
KD
3209 *
3210 * Return 1 meaning mf should be freed from _base_interrupt
3211 * 0 means the mf is freed from this function.
3212 */
3213static u8
3214_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3215 u8 msix_index, u32 reply)
3216{
77e63ed4
KD
3217 Mpi2SasIoUnitControlReply_t *mpi_reply =
3218 mpt2sas_base_get_reply_virt_addr(ioc, reply);
298c794d 3219 if (likely(mpi_reply)) {
3220 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3221 "sc_complete:handle(0x%04x), (open) "
3222 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3223 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3224 le16_to_cpu(mpi_reply->IOCStatus),
3225 le32_to_cpu(mpi_reply->IOCLogInfo)));
3226 } else {
3227 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3228 ioc->name, __FILE__, __LINE__, __func__);
3229 }
77e63ed4
KD
3230 return 1;
3231}
3232
f3eedd69
KD
3233/**
3234 * _scsih_tm_tr_volume_send - send target reset request for volumes
3235 * @ioc: per adapter object
3236 * @handle: device handle
3237 * Context: interrupt time.
3238 *
3239 * This is designed to send muliple task management request at the same
3240 * time to the fifo. If the fifo is full, we will append the request,
3241 * and process it in a future completion.
3242 */
3243static void
3244_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3245{
3246 Mpi2SCSITaskManagementRequest_t *mpi_request;
3247 u16 smid;
3248 struct _tr_list *delayed_tr;
3249
3cb5469a
EM
3250 if (ioc->shost_recovery || ioc->remove_host ||
3251 ioc->pci_error_recovery) {
f3eedd69
KD
3252 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3253 "progress!\n", __func__, ioc->name));
3254 return;
3255 }
3256
3257 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3258 if (!smid) {
3259 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3260 if (!delayed_tr)
3261 return;
3262 INIT_LIST_HEAD(&delayed_tr->list);
3263 delayed_tr->handle = handle;
3264 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3265 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3266 "DELAYED:tr:handle(0x%04x), (open)\n",
3267 ioc->name, handle));
3268 return;
3269 }
3270
3271 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3272 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3273 ioc->tm_tr_volume_cb_idx));
3274 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3275 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3276 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3277 mpi_request->DevHandle = cpu_to_le16(handle);
3278 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3279 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3280}
3281
3282/**
3283 * _scsih_tm_volume_tr_complete - target reset completion
3284 * @ioc: per adapter object
3285 * @smid: system request message index
3286 * @msix_index: MSIX table index supplied by the OS
3287 * @reply: reply message frame(lower 32bit addr)
3288 * Context: interrupt time.
3289 *
3290 * Return 1 meaning mf should be freed from _base_interrupt
3291 * 0 means the mf is freed from this function.
3292 */
3293static u8
3294_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3295 u8 msix_index, u32 reply)
3296{
3297 u16 handle;
3298 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3299 Mpi2SCSITaskManagementReply_t *mpi_reply =
3300 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3301
3cb5469a
EM
3302 if (ioc->shost_recovery || ioc->remove_host ||
3303 ioc->pci_error_recovery) {
f3eedd69
KD
3304 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3305 "progress!\n", __func__, ioc->name));
3306 return 1;
3307 }
298c794d 3308 if (unlikely(!mpi_reply)) {
3309 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3310 ioc->name, __FILE__, __LINE__, __func__);
3311 return 1;
3312 }
f3eedd69
KD
3313 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3314 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3315 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3316 dewtprintk(ioc, printk("spurious interrupt: "
3317 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3318 le16_to_cpu(mpi_reply->DevHandle), smid));
3319 return 0;
3320 }
3321
3322 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3323 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3324 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3325 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3326 le32_to_cpu(mpi_reply->IOCLogInfo),
3327 le32_to_cpu(mpi_reply->TerminationCount)));
3328
3329 return _scsih_check_for_pending_tm(ioc, smid);
3330}
3331
77e63ed4
KD
3332/**
3333 * _scsih_tm_tr_complete -
3334 * @ioc: per adapter object
3335 * @smid: system request message index
3336 * @msix_index: MSIX table index supplied by the OS
3337 * @reply: reply message frame(lower 32bit addr)
3338 * Context: interrupt time.
3339 *
3340 * This is the target reset completion routine.
3341 * This code is part of the code to initiate the device removal
25985edc
LDM
3342 * handshake protocol with controller firmware.
3343 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
77e63ed4
KD
3344 *
3345 * Return 1 meaning mf should be freed from _base_interrupt
3346 * 0 means the mf is freed from this function.
3347 */
3348static u8
3349_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3350 u32 reply)
3351{
77e63ed4 3352 u16 handle;
1278b11f 3353 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
77e63ed4
KD
3354 Mpi2SCSITaskManagementReply_t *mpi_reply =
3355 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3356 Mpi2SasIoUnitControlRequest_t *mpi_request;
3357 u16 smid_sas_ctrl;
f881cead 3358 u32 ioc_state;
77e63ed4 3359
f881cead 3360 if (ioc->remove_host) {
3361 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3362 "removed\n", __func__, ioc->name));
3363 return 1;
3364 } else if (ioc->pci_error_recovery) {
3365 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3366 "error recovery\n", __func__, ioc->name));
3367 return 1;
3368 }
3369 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3370 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3371 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3372 "operational\n", __func__, ioc->name));
1278b11f 3373 return 1;
77e63ed4 3374 }
298c794d 3375 if (unlikely(!mpi_reply)) {
3376 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3377 ioc->name, __FILE__, __LINE__, __func__);
3378 return 1;
3379 }
1278b11f
KD
3380 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3381 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3382 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3383 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3384 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3385 le16_to_cpu(mpi_reply->DevHandle), smid));
3386 return 0;
77e63ed4
KD
3387 }
3388
1278b11f
KD
3389 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3390 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3391 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3392 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3393 le32_to_cpu(mpi_reply->IOCLogInfo),
3394 le32_to_cpu(mpi_reply->TerminationCount)));
3395
77e63ed4
KD
3396 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3397 if (!smid_sas_ctrl) {
3398 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3399 ioc->name, __func__);
1278b11f 3400 return 1;
77e63ed4
KD
3401 }
3402
1278b11f
KD
3403 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3404 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3405 ioc->tm_sas_control_cb_idx));
77e63ed4
KD
3406 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3407 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3408 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3409 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
1278b11f 3410 mpi_request->DevHandle = mpi_request_tm->DevHandle;
77e63ed4 3411 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
1278b11f 3412
f3eedd69
KD
3413 return _scsih_check_for_pending_tm(ioc, smid);
3414}
3415
3416/**
3417 * _scsih_check_for_pending_tm - check for pending task management
3418 * @ioc: per adapter object
3419 * @smid: system request message index
3420 *
3421 * This will check delayed target reset list, and feed the
3422 * next reqeust.
3423 *
3424 * Return 1 meaning mf should be freed from _base_interrupt
3425 * 0 means the mf is freed from this function.
3426 */
3427static u8
3428_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3429{
3430 struct _tr_list *delayed_tr;
3431
3432 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3433 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3434 struct _tr_list, list);
3435 mpt2sas_base_free_smid(ioc, smid);
3436 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3437 list_del(&delayed_tr->list);
3438 kfree(delayed_tr);
3439 return 0;
3440 }
3441
1278b11f
KD
3442 if (!list_empty(&ioc->delayed_tr_list)) {
3443 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3444 struct _tr_list, list);
3445 mpt2sas_base_free_smid(ioc, smid);
3446 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3447 list_del(&delayed_tr->list);
3448 kfree(delayed_tr);
f3eedd69 3449 return 0;
1278b11f 3450 }
f3eedd69 3451
1278b11f 3452 return 1;
77e63ed4
KD
3453}
3454
635374e7
EM
3455/**
3456 * _scsih_check_topo_delete_events - sanity check on topo events
3457 * @ioc: per adapter object
3458 * @event_data: the event data payload
3459 *
3460 * This routine added to better handle cable breaker.
3461 *
25985edc 3462 * This handles the case where driver receives multiple expander
635374e7
EM
3463 * add and delete events in a single shot. When there is a delete event
3464 * the routine will void any pending add events waiting in the event queue.
3465 *
3466 * Return nothing.
3467 */
3468static void
3469_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3470 Mpi2EventDataSasTopologyChangeList_t *event_data)
3471{
3472 struct fw_event_work *fw_event;
3473 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3474 u16 expander_handle;
3475 struct _sas_node *sas_expander;
3476 unsigned long flags;
77e63ed4
KD
3477 int i, reason_code;
3478 u16 handle;
3479
3480 for (i = 0 ; i < event_data->NumEntries; i++) {
77e63ed4
KD
3481 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3482 if (!handle)
3483 continue;
3484 reason_code = event_data->PHY[i].PhyStatus &
3485 MPI2_EVENT_SAS_TOPO_RC_MASK;
3486 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3487 _scsih_tm_tr_send(ioc, handle);
3488 }
635374e7
EM
3489
3490 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3491 if (expander_handle < ioc->sas_hba.num_phys) {
3492 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3493 return;
3494 }
09da0b32 3495 if (event_data->ExpStatus ==
3496 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3497 /* put expander attached devices into blocking state */
635374e7
EM
3498 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3499 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3500 expander_handle);
635374e7 3501 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
09da0b32 3502 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3503 do {
3504 handle = find_first_bit(ioc->blocking_handles,
3505 ioc->facts.MaxDevHandle);
3506 if (handle < ioc->facts.MaxDevHandle)
3507 _scsih_block_io_device(ioc, handle);
3508 } while (test_and_clear_bit(handle, ioc->blocking_handles));
635374e7
EM
3509 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3510 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3511
3512 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3513 return;
3514
3515 /* mark ignore flag for pending events */
3516 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3517 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3518 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3519 fw_event->ignore)
3520 continue;
00713ad7
JL
3521 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3522 fw_event->event_data;
635374e7
EM
3523 if (local_event_data->ExpStatus ==
3524 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3525 local_event_data->ExpStatus ==
3526 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3527 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3528 expander_handle) {
eabb08ad 3529 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
3530 "setting ignoring flag\n", ioc->name));
3531 fw_event->ignore = 1;
3532 }
3533 }
3534 }
3535 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3536}
3537
f3eedd69
KD
3538/**
3539 * _scsih_set_volume_delete_flag - setting volume delete flag
3540 * @ioc: per adapter object
3541 * @handle: device handle
3542 *
3543 * This
3544 * Return nothing.
3545 */
3546static void
3547_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3548{
3549 struct _raid_device *raid_device;
3550 struct MPT2SAS_TARGET *sas_target_priv_data;
3551 unsigned long flags;
3552
3553 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3554 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3555 if (raid_device && raid_device->starget &&
3556 raid_device->starget->hostdata) {
3557 sas_target_priv_data =
3558 raid_device->starget->hostdata;
3559 sas_target_priv_data->deleted = 1;
3560 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3561 "setting delete flag: handle(0x%04x), "
3562 "wwid(0x%016llx)\n", ioc->name, handle,
3563 (unsigned long long) raid_device->wwid));
3564 }
3565 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3566}
3567
3568/**
3569 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3570 * @handle: input handle
3571 * @a: handle for volume a
3572 * @b: handle for volume b
3573 *
3574 * IR firmware only supports two raid volumes. The purpose of this
3575 * routine is to set the volume handle in either a or b. When the given
3576 * input handle is non-zero, or when a and b have not been set before.
3577 */
3578static void
3579_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3580{
3581 if (!handle || handle == *a || handle == *b)
3582 return;
3583 if (!*a)
3584 *a = handle;
3585 else if (!*b)
3586 *b = handle;
3587}
3588
3589/**
3590 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3591 * @ioc: per adapter object
3592 * @event_data: the event data payload
3593 * Context: interrupt time.
3594 *
3595 * This routine will send target reset to volume, followed by target
3596 * resets to the PDs. This is called when a PD has been removed, or
3597 * volume has been deleted or removed. When the target reset is sent
3598 * to volume, the PD target resets need to be queued to start upon
3599 * completion of the volume target reset.
3600 *
3601 * Return nothing.
3602 */
3603static void
3604_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3605 Mpi2EventDataIrConfigChangeList_t *event_data)
3606{
3607 Mpi2EventIrConfigElement_t *element;
3608 int i;
3609 u16 handle, volume_handle, a, b;
3610 struct _tr_list *delayed_tr;
3611
3612 a = 0;
3613 b = 0;
3614
0bdccdb0
KD
3615 if (ioc->is_warpdrive)
3616 return;
3617
f3eedd69
KD
3618 /* Volume Resets for Deleted or Removed */
3619 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3620 for (i = 0; i < event_data->NumElements; i++, element++) {
3621 if (element->ReasonCode ==
3622 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3623 element->ReasonCode ==
3624 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3625 volume_handle = le16_to_cpu(element->VolDevHandle);
3626 _scsih_set_volume_delete_flag(ioc, volume_handle);
3627 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3628 }
3629 }
3630
3631 /* Volume Resets for UNHIDE events */
3632 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3633 for (i = 0; i < event_data->NumElements; i++, element++) {
3634 if (le32_to_cpu(event_data->Flags) &
3635 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3636 continue;
3637 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3638 volume_handle = le16_to_cpu(element->VolDevHandle);
3639 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3640 }
3641 }
3642
3643 if (a)
3644 _scsih_tm_tr_volume_send(ioc, a);
3645 if (b)
3646 _scsih_tm_tr_volume_send(ioc, b);
3647
3648 /* PD target resets */
3649 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3650 for (i = 0; i < event_data->NumElements; i++, element++) {
3651 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3652 continue;
3653 handle = le16_to_cpu(element->PhysDiskDevHandle);
3654 volume_handle = le16_to_cpu(element->VolDevHandle);
3655 clear_bit(handle, ioc->pd_handles);
3656 if (!volume_handle)
3657 _scsih_tm_tr_send(ioc, handle);
3658 else if (volume_handle == a || volume_handle == b) {
3659 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3660 BUG_ON(!delayed_tr);
3661 INIT_LIST_HEAD(&delayed_tr->list);
3662 delayed_tr->handle = handle;
3663 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3664 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3665 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3666 handle));
3667 } else
3668 _scsih_tm_tr_send(ioc, handle);
3669 }
3670}
3671
3672
3673/**
3674 * _scsih_check_volume_delete_events - set delete flag for volumes
3675 * @ioc: per adapter object
3676 * @event_data: the event data payload
3677 * Context: interrupt time.
3678 *
3679 * This will handle the case when the cable connected to entire volume is
3680 * pulled. We will take care of setting the deleted flag so normal IO will
3681 * not be sent.
3682 *
3683 * Return nothing.
3684 */
3685static void
3686_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3687 Mpi2EventDataIrVolume_t *event_data)
3688{
3689 u32 state;
3690
3691 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3692 return;
3693 state = le32_to_cpu(event_data->NewValue);
3694 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3695 MPI2_RAID_VOL_STATE_FAILED)
3696 _scsih_set_volume_delete_flag(ioc,
3697 le16_to_cpu(event_data->VolDevHandle));
3698}
3699
635374e7
EM
3700/**
3701 * _scsih_flush_running_cmds - completing outstanding commands.
3702 * @ioc: per adapter object
3703 *
3704 * The flushing out of all pending scmd commands following host reset,
3705 * where all IO is dropped to the floor.
3706 *
3707 * Return nothing.
3708 */
3709static void
3710_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3711{
3712 struct scsi_cmnd *scmd;
3713 u16 smid;
3714 u16 count = 0;
3715
595bb0bd 3716 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
ec07a053 3717 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
635374e7
EM
3718 if (!scmd)
3719 continue;
3720 count++;
3721 mpt2sas_base_free_smid(ioc, smid);
3722 scsi_dma_unmap(scmd);
3cb5469a
EM
3723 if (ioc->pci_error_recovery)
3724 scmd->result = DID_NO_CONNECT << 16;
3725 else
3726 scmd->result = DID_RESET << 16;
635374e7
EM
3727 scmd->scsi_done(scmd);
3728 }
3729 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3730 ioc->name, count));
3731}
3732
3c621b3e
EM
3733/**
3734 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3735 * @scmd: pointer to scsi command object
3736 * @mpi_request: pointer to the SCSI_IO reqest message frame
3737 *
3738 * Supporting protection 1 and 3.
3739 *
3740 * Returns nothing
3741 */
3742static void
3743_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3744{
3745 u16 eedp_flags;
3746 unsigned char prot_op = scsi_get_prot_op(scmd);
3747 unsigned char prot_type = scsi_get_prot_type(scmd);
3748
d334aa79 3749 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3c621b3e
EM
3750 return;
3751
3752 if (prot_op == SCSI_PROT_READ_STRIP)
3753 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3754 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3755 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3756 else
3757 return;
3758
3c621b3e
EM
3759 switch (prot_type) {
3760 case SCSI_PROT_DIF_TYPE1:
756aca7e 3761 case SCSI_PROT_DIF_TYPE2:
3c621b3e
EM
3762
3763 /*
3764 * enable ref/guard checking
3765 * auto increment ref tag
3766 */
463217bf 3767 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3c621b3e
EM
3768 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3769 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3770 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3771 cpu_to_be32(scsi_get_lba(scmd));
d334aa79
EM
3772 break;
3773
3c621b3e
EM
3774 case SCSI_PROT_DIF_TYPE3:
3775
3776 /*
3777 * enable guard checking
3778 */
463217bf 3779 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3c621b3e
EM
3780 break;
3781 }
463217bf
KD
3782 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3783 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3c621b3e
EM
3784}
3785
3786/**
3787 * _scsih_eedp_error_handling - return sense code for EEDP errors
3788 * @scmd: pointer to scsi command object
3789 * @ioc_status: ioc status
3790 *
3791 * Returns nothing
3792 */
3793static void
3794_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3795{
3796 u8 ascq;
3c621b3e
EM
3797
3798 switch (ioc_status) {
3799 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3800 ascq = 0x01;
3801 break;
3802 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3803 ascq = 0x02;
3804 break;
3805 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3806 ascq = 0x03;
3807 break;
3808 default:
3809 ascq = 0x00;
3810 break;
3811 }
3812
9b5cd132
MP
3813 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3814 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3c621b3e
EM
3815 SAM_STAT_CHECK_CONDITION;
3816}
3817
0bdccdb0
KD
3818/**
3819 * _scsih_scsi_direct_io_get - returns direct io flag
3820 * @ioc: per adapter object
3821 * @smid: system request message index
3822 *
3823 * Returns the smid stored scmd pointer.
3824 */
3825static inline u8
3826_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3827{
3828 return ioc->scsi_lookup[smid - 1].direct_io;
3829}
3830
3831/**
3832 * _scsih_scsi_direct_io_set - sets direct io flag
3833 * @ioc: per adapter object
3834 * @smid: system request message index
3835 * @direct_io: Zero or non-zero value to set in the direct_io flag
3836 *
3837 * Returns Nothing.
3838 */
3839static inline void
3840_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3841{
3842 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3843}
3844
3845
3846/**
3847 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3848 * @ioc: per adapter object
3849 * @scmd: pointer to scsi command object
3850 * @raid_device: pointer to raid device data structure
3851 * @mpi_request: pointer to the SCSI_IO reqest message frame
3852 * @smid: system request message index
3853 *
3854 * Returns nothing
3855 */
3856static void
3857_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3858 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3859 u16 smid)
3860{
3861 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3862 u32 stripe_sz, stripe_exp;
ba96bd0b 3863 u8 num_pds, *cdb_ptr, i;
0bdccdb0 3864 u8 cdb0 = scmd->cmnd[0];
ba96bd0b 3865 u64 v_llba;
0bdccdb0
KD
3866
3867 /*
3868 * Try Direct I/O to RAID memeber disks
3869 */
3870 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3871 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3872 cdb_ptr = mpi_request->CDB.CDB32;
3873
3874 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3875 | cdb_ptr[5])) {
ba96bd0b 3876 io_size = scsi_bufflen(scmd) >>
3877 raid_device->block_exponent;
3878 i = (cdb0 < READ_16) ? 2 : 6;
0bdccdb0 3879 /* get virtual lba */
ba96bd0b 3880 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
0bdccdb0
KD
3881
3882 if (((u64)v_lba + (u64)io_size - 1) <=
3883 (u32)raid_device->max_lba) {
3884 stripe_sz = raid_device->stripe_sz;
3885 stripe_exp = raid_device->stripe_exponent;
3886 stripe_off = v_lba & (stripe_sz - 1);
3887
3888 /* Check whether IO falls within a stripe */
3889 if ((stripe_off + io_size) <= stripe_sz) {
3890 num_pds = raid_device->num_pds;
3891 p_lba = v_lba >> stripe_exp;
3892 stripe_unit = p_lba / num_pds;
3893 column = p_lba % num_pds;
3894 p_lba = (stripe_unit << stripe_exp) +
3895 stripe_off;
3896 mpi_request->DevHandle =
3897 cpu_to_le16(raid_device->
3898 pd_handle[column]);
ba96bd0b 3899 (*(__be32 *)(&cdb_ptr[i])) =
3900 cpu_to_be32(p_lba);
3901 /*
3902 * WD: To indicate this I/O is directI/O
3903 */
3904 _scsih_scsi_direct_io_set(ioc, smid, 1);
3905 }
3906 }
3907 } else {
3908 io_size = scsi_bufflen(scmd) >>
3909 raid_device->block_exponent;
3910 /* get virtual lba */
3911 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3912
3913 if ((v_llba + (u64)io_size - 1) <=
3914 raid_device->max_lba) {
3915 stripe_sz = raid_device->stripe_sz;
3916 stripe_exp = raid_device->stripe_exponent;
3917 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3918
3919 /* Check whether IO falls within a stripe */
3920 if ((stripe_off + io_size) <= stripe_sz) {
3921 num_pds = raid_device->num_pds;
3922 p_lba = (u32)(v_llba >> stripe_exp);
3923 stripe_unit = p_lba / num_pds;
3924 column = p_lba % num_pds;
3925 p_lba = (stripe_unit << stripe_exp) +
3926 stripe_off;
3927 mpi_request->DevHandle =
3928 cpu_to_le16(raid_device->
3929 pd_handle[column]);
3930 (*(__be64 *)(&cdb_ptr[2])) =
3931 cpu_to_be64((u64)p_lba);
0bdccdb0
KD
3932 /*
3933 * WD: To indicate this I/O is directI/O
3934 */
3935 _scsih_scsi_direct_io_set(ioc, smid, 1);
3936 }
3937 }
3938 }
3939 }
3940}
3941
635374e7 3942/**
d5d135b3 3943 * _scsih_qcmd - main scsi request entry point
635374e7
EM
3944 * @scmd: pointer to scsi command object
3945 * @done: function pointer to be invoked on completion
3946 *
3947 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3948 *
3949 * Returns 0 on success. If there's a failure, return either:
3950 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3951 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3952 */
3953static int
90f951ed 3954_scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
635374e7 3955{
90f951ed 3956 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
635374e7
EM
3957 struct MPT2SAS_DEVICE *sas_device_priv_data;
3958 struct MPT2SAS_TARGET *sas_target_priv_data;
0bdccdb0 3959 struct _raid_device *raid_device;
635374e7
EM
3960 Mpi2SCSIIORequest_t *mpi_request;
3961 u32 mpi_control;
3962 u16 smid;
635374e7 3963
635374e7 3964 sas_device_priv_data = scmd->device->hostdata;
130b958a 3965 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
635374e7
EM
3966 scmd->result = DID_NO_CONNECT << 16;
3967 scmd->scsi_done(scmd);
3968 return 0;
3969 }
3970
7821578c 3971 if (ioc->pci_error_recovery || ioc->remove_host) {
3cb5469a
EM
3972 scmd->result = DID_NO_CONNECT << 16;
3973 scmd->scsi_done(scmd);
3974 return 0;
3975 }
3976
635374e7 3977 sas_target_priv_data = sas_device_priv_data->sas_target;
130b958a
KD
3978 /* invalid device handle */
3979 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
635374e7
EM
3980 scmd->result = DID_NO_CONNECT << 16;
3981 scmd->scsi_done(scmd);
3982 return 0;
3983 }
3984
130b958a
KD
3985 /* host recovery or link resets sent via IOCTLs */
3986 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
635374e7 3987 return SCSI_MLQUEUE_HOST_BUSY;
65155b37 3988 /* device busy with task management */
130b958a
KD
3989 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3990 return SCSI_MLQUEUE_DEVICE_BUSY;
3991 /* device has been deleted */
3992 else if (sas_target_priv_data->deleted) {
3993 scmd->result = DID_NO_CONNECT << 16;
3994 scmd->scsi_done(scmd);
3995 return 0;
3996 }
635374e7
EM
3997
3998 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3999 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4000 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4001 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4002 else
4003 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4004
4005 /* set tags */
4006 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
4007 if (scmd->device->tagged_supported) {
4008 if (scmd->device->ordered_tags)
4009 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
4010 else
4011 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4012 } else
48ba2efc 4013 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
635374e7
EM
4014 } else
4015 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
0bdccdb0
KD
4016 /* Make sure Device is not raid volume.
4017 * We do not expose raid functionality to upper layer for warpdrive.
4018 */
4019 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
d334aa79 4020 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
635374e7
EM
4021 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4022
595bb0bd 4023 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
635374e7
EM
4024 if (!smid) {
4025 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
4026 ioc->name, __func__);
4027 goto out;
4028 }
4029 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4030 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3c621b3e 4031 _scsih_setup_eedp(scmd, mpi_request);
d334aa79
EM
4032 if (scmd->cmd_len == 32)
4033 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
635374e7
EM
4034 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4035 if (sas_device_priv_data->sas_target->flags &
4036 MPT_TARGET_FLAGS_RAID_COMPONENT)
4037 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4038 else
90f951ed 4039 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
635374e7
EM
4040 mpi_request->DevHandle =
4041 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4042 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4043 mpi_request->Control = cpu_to_le32(mpi_control);
4044 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4045 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4046 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4047 mpi_request->SenseBufferLowAddress =
ec9472c7 4048 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
635374e7
EM
4049 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4050 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
4051 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
7b936b02
KD
4052 mpi_request->VF_ID = 0; /* TODO */
4053 mpi_request->VP_ID = 0;
635374e7
EM
4054 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4055 mpi_request->LUN);
4056 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4057
4058 if (!mpi_request->DataLength) {
4059 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
4060 } else {
4061 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
4062 mpt2sas_base_free_smid(ioc, smid);
4063 goto out;
4064 }
4065 }
4066
0bdccdb0
KD
4067 raid_device = sas_target_priv_data->raid_device;
4068 if (raid_device && raid_device->direct_io_enabled)
4069 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4070 smid);
4071
58287fd5
KD
4072 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4073 mpt2sas_base_put_smid_scsi_io(ioc, smid,
0bdccdb0 4074 le16_to_cpu(mpi_request->DevHandle));
58287fd5
KD
4075 else
4076 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
4077 return 0;
4078
4079 out:
4080 return SCSI_MLQUEUE_HOST_BUSY;
4081}
4082
4083/**
4084 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4085 * @sense_buffer: sense data returned by target
4086 * @data: normalized skey/asc/ascq
4087 *
4088 * Return nothing.
4089 */
4090static void
4091_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4092{
4093 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4094 /* descriptor format */
4095 data->skey = sense_buffer[1] & 0x0F;
4096 data->asc = sense_buffer[2];
4097 data->ascq = sense_buffer[3];
4098 } else {
4099 /* fixed format */
4100 data->skey = sense_buffer[2] & 0x0F;
4101 data->asc = sense_buffer[12];
4102 data->ascq = sense_buffer[13];
4103 }
4104}
4105
4106#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4107/**
25985edc 4108 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
635374e7
EM
4109 * @ioc: per adapter object
4110 * @scmd: pointer to scsi command object
4111 * @mpi_reply: reply mf payload returned from firmware
4112 *
4113 * scsi_status - SCSI Status code returned from target device
4114 * scsi_state - state info associated with SCSI_IO determined by ioc
4115 * ioc_status - ioc supplied status info
4116 *
4117 * Return nothing.
4118 */
4119static void
4120_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4121 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4122{
4123 u32 response_info;
4124 u8 *response_bytes;
4125 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4126 MPI2_IOCSTATUS_MASK;
4127 u8 scsi_state = mpi_reply->SCSIState;
4128 u8 scsi_status = mpi_reply->SCSIStatus;
4129 char *desc_ioc_state = NULL;
4130 char *desc_scsi_status = NULL;
4131 char *desc_scsi_state = ioc->tmp_string;
be9e8cd7 4132 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
7fbae67a
KD
4133 struct _sas_device *sas_device = NULL;
4134 unsigned long flags;
8e864a81
KD
4135 struct scsi_target *starget = scmd->device->sdev_target;
4136 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
0bdccdb0 4137 char *device_str = NULL;
8e864a81
KD
4138
4139 if (!priv_target)
4140 return;
be9e8cd7 4141
0bdccdb0
KD
4142 if (ioc->hide_ir_msg)
4143 device_str = "WarpDrive";
4144 else
4145 device_str = "volume";
4146
be9e8cd7
KD
4147 if (log_info == 0x31170000)
4148 return;
635374e7
EM
4149
4150 switch (ioc_status) {
4151 case MPI2_IOCSTATUS_SUCCESS:
4152 desc_ioc_state = "success";
4153 break;
4154 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4155 desc_ioc_state = "invalid function";
4156 break;
4157 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4158 desc_ioc_state = "scsi recovered error";
4159 break;
4160 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4161 desc_ioc_state = "scsi invalid dev handle";
4162 break;
4163 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4164 desc_ioc_state = "scsi device not there";
4165 break;
4166 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4167 desc_ioc_state = "scsi data overrun";
4168 break;
4169 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4170 desc_ioc_state = "scsi data underrun";
4171 break;
4172 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4173 desc_ioc_state = "scsi io data error";
4174 break;
4175 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4176 desc_ioc_state = "scsi protocol error";
4177 break;
4178 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4179 desc_ioc_state = "scsi task terminated";
4180 break;
4181 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4182 desc_ioc_state = "scsi residual mismatch";
4183 break;
4184 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4185 desc_ioc_state = "scsi task mgmt failed";
4186 break;
4187 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4188 desc_ioc_state = "scsi ioc terminated";
4189 break;
4190 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4191 desc_ioc_state = "scsi ext terminated";
4192 break;
3c621b3e
EM
4193 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4194 desc_ioc_state = "eedp guard error";
4195 break;
4196 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4197 desc_ioc_state = "eedp ref tag error";
4198 break;
4199 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4200 desc_ioc_state = "eedp app tag error";
4201 break;
635374e7
EM
4202 default:
4203 desc_ioc_state = "unknown";
4204 break;
4205 }
4206
4207 switch (scsi_status) {
4208 case MPI2_SCSI_STATUS_GOOD:
4209 desc_scsi_status = "good";
4210 break;
4211 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4212 desc_scsi_status = "check condition";
4213 break;
4214 case MPI2_SCSI_STATUS_CONDITION_MET:
4215 desc_scsi_status = "condition met";
4216 break;
4217 case MPI2_SCSI_STATUS_BUSY:
4218 desc_scsi_status = "busy";
4219 break;
4220 case MPI2_SCSI_STATUS_INTERMEDIATE:
4221 desc_scsi_status = "intermediate";
4222 break;
4223 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4224 desc_scsi_status = "intermediate condmet";
4225 break;
4226 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4227 desc_scsi_status = "reservation conflict";
4228 break;
4229 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4230 desc_scsi_status = "command terminated";
4231 break;
4232 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4233 desc_scsi_status = "task set full";
4234 break;
4235 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4236 desc_scsi_status = "aca active";
4237 break;
4238 case MPI2_SCSI_STATUS_TASK_ABORTED:
4239 desc_scsi_status = "task aborted";
4240 break;
4241 default:
4242 desc_scsi_status = "unknown";
4243 break;
4244 }
4245
4246 desc_scsi_state[0] = '\0';
4247 if (!scsi_state)
4248 desc_scsi_state = " ";
4249 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4250 strcat(desc_scsi_state, "response info ");
4251 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4252 strcat(desc_scsi_state, "state terminated ");
4253 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4254 strcat(desc_scsi_state, "no status ");
4255 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4256 strcat(desc_scsi_state, "autosense failed ");
4257 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4258 strcat(desc_scsi_state, "autosense valid ");
4259
4260 scsi_print_command(scmd);
7fbae67a 4261
8e864a81 4262 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
0bdccdb0
KD
4263 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4264 device_str, (unsigned long long)priv_target->sas_address);
8e864a81
KD
4265 } else {
4266 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4267 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4268 priv_target->sas_address);
4269 if (sas_device) {
4270 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4271 "phy(%d)\n", ioc->name, sas_device->sas_address,
4272 sas_device->phy);
4273 printk(MPT2SAS_WARN_FMT
4274 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4275 ioc->name, sas_device->enclosure_logical_id,
4276 sas_device->slot);
4277 }
4278 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7fbae67a 4279 }
7fbae67a 4280
8e864a81
KD
4281 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4282 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4283 desc_ioc_state, ioc_status, smid);
635374e7
EM
4284 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4285 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4286 scsi_get_resid(scmd));
4287 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4288 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4289 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4290 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4291 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4292 scsi_status, desc_scsi_state, scsi_state);
4293
4294 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4295 struct sense_info data;
4296 _scsih_normalize_sense(scmd->sense_buffer, &data);
4297 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
e94f6747
KD
4298 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4299 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
635374e7
EM
4300 }
4301
4302 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4303 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4304 response_bytes = (u8 *)&response_info;
9982f594 4305 _scsih_response_code(ioc, response_bytes[0]);
635374e7
EM
4306 }
4307}
4308#endif
4309
4310/**
3ace8e05 4311 * _scsih_turn_on_fault_led - illuminate Fault LED
635374e7
EM
4312 * @ioc: per adapter object
4313 * @handle: device handle
3ace8e05 4314 * Context: process
635374e7
EM
4315 *
4316 * Return nothing.
4317 */
4318static void
3ace8e05 4319_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
635374e7
EM
4320{
4321 Mpi2SepReply_t mpi_reply;
4322 Mpi2SepRequest_t mpi_request;
3ace8e05
KD
4323
4324 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4325 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4326 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4327 mpi_request.SlotStatus =
4328 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4329 mpi_request.DevHandle = cpu_to_le16(handle);
4330 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4331 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4332 &mpi_request)) != 0) {
4333 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4334 __FILE__, __LINE__, __func__);
4335 return;
4336 }
4337
4338 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4339 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4340 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4341 le16_to_cpu(mpi_reply.IOCStatus),
4342 le32_to_cpu(mpi_reply.IOCLogInfo)));
4343 return;
4344 }
4345}
4346
4347/**
4348 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4349 * @ioc: per adapter object
4350 * @handle: device handle
4351 * Context: interrupt.
4352 *
4353 * Return nothing.
4354 */
4355static void
4356_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4357{
4358 struct fw_event_work *fw_event;
4359
4360 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4361 if (!fw_event)
4362 return;
4363 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4364 fw_event->device_handle = handle;
4365 fw_event->ioc = ioc;
4366 _scsih_fw_event_add(ioc, fw_event);
4367}
4368
4369/**
4370 * _scsih_smart_predicted_fault - process smart errors
4371 * @ioc: per adapter object
4372 * @handle: device handle
4373 * Context: interrupt.
4374 *
4375 * Return nothing.
4376 */
4377static void
4378_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4379{
635374e7
EM
4380 struct scsi_target *starget;
4381 struct MPT2SAS_TARGET *sas_target_priv_data;
4382 Mpi2EventNotificationReply_t *event_reply;
4383 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4384 struct _sas_device *sas_device;
4385 ssize_t sz;
4386 unsigned long flags;
4387
4388 /* only handle non-raid devices */
4389 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4390 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4391 if (!sas_device) {
4392 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4393 return;
4394 }
4395 starget = sas_device->starget;
4396 sas_target_priv_data = starget->hostdata;
4397
4398 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4399 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4401 return;
4402 }
4403 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4404 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4405
3ace8e05
KD
4406 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4407 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
635374e7
EM
4408
4409 /* insert into event log */
4410 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4411 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
f6a290b4 4412 event_reply = kzalloc(sz, GFP_ATOMIC);
635374e7
EM
4413 if (!event_reply) {
4414 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4415 ioc->name, __FILE__, __LINE__, __func__);
4416 return;
4417 }
4418
4419 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4420 event_reply->Event =
4421 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4422 event_reply->MsgLength = sz/4;
4423 event_reply->EventDataLength =
4424 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4425 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4426 event_reply->EventData;
4427 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4428 event_data->ASC = 0x5D;
4429 event_data->DevHandle = cpu_to_le16(handle);
4430 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4431 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4432 kfree(event_reply);
4433}
4434
4435/**
d5d135b3 4436 * _scsih_io_done - scsi request callback
635374e7
EM
4437 * @ioc: per adapter object
4438 * @smid: system request message index
7b936b02 4439 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
4440 * @reply: reply message frame(lower 32bit addr)
4441 *
77e63ed4 4442 * Callback handler when using _scsih_qcmd.
635374e7 4443 *
77e63ed4
KD
4444 * Return 1 meaning mf should be freed from _base_interrupt
4445 * 0 means the mf is freed from this function.
635374e7 4446 */
77e63ed4 4447static u8
7b936b02 4448_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
4449{
4450 Mpi2SCSIIORequest_t *mpi_request;
4451 Mpi2SCSIIOReply_t *mpi_reply;
4452 struct scsi_cmnd *scmd;
4453 u16 ioc_status;
4454 u32 xfer_cnt;
4455 u8 scsi_state;
4456 u8 scsi_status;
4457 u32 log_info;
4458 struct MPT2SAS_DEVICE *sas_device_priv_data;
9982f594 4459 u32 response_code = 0;
82a45258 4460 unsigned long flags;
635374e7
EM
4461
4462 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
ec07a053 4463 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
635374e7 4464 if (scmd == NULL)
77e63ed4 4465 return 1;
635374e7
EM
4466
4467 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4468
4469 if (mpi_reply == NULL) {
4470 scmd->result = DID_OK << 16;
4471 goto out;
4472 }
4473
4474 sas_device_priv_data = scmd->device->hostdata;
4475 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4476 sas_device_priv_data->sas_target->deleted) {
4477 scmd->result = DID_NO_CONNECT << 16;
4478 goto out;
4479 }
4da7af94 4480 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
0bdccdb0
KD
4481 /*
4482 * WARPDRIVE: If direct_io is set then it is directIO,
4483 * the failed direct I/O should be redirected to volume
4484 */
4da7af94 4485 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4486 ((ioc_status & MPI2_IOCSTATUS_MASK)
4487 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
82a45258
KD
4488 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4489 ioc->scsi_lookup[smid - 1].scmd = scmd;
0bdccdb0 4490 _scsih_scsi_direct_io_set(ioc, smid, 0);
09da0b32 4491 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
0bdccdb0
KD
4492 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4493 mpi_request->DevHandle =
4494 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4495 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4496 sas_device_priv_data->sas_target->handle);
4497 return 0;
4498 }
4499
635374e7
EM
4500
4501 /* turning off TLR */
9982f594
KD
4502 scsi_state = mpi_reply->SCSIState;
4503 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4504 response_code =
4505 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
635374e7
EM
4506 if (!sas_device_priv_data->tlr_snoop_check) {
4507 sas_device_priv_data->tlr_snoop_check++;
0bdccdb0
KD
4508 /* Make sure Device is not raid volume.
4509 * We do not expose raid functionality to upper layer for warpdrive.
4510 */
4511 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
3ed21525 4512 sas_is_tlr_enabled(scmd->device) &&
84f0b04a
KD
4513 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4514 sas_disable_tlr(scmd->device);
4515 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4516 }
635374e7
EM
4517 }
4518
4519 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4520 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
635374e7
EM
4521 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4522 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4523 else
4524 log_info = 0;
4525 ioc_status &= MPI2_IOCSTATUS_MASK;
635374e7
EM
4526 scsi_status = mpi_reply->SCSIStatus;
4527
4528 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4529 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4530 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4531 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4532 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4533 }
4534
4535 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4536 struct sense_info data;
4537 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4538 smid);
0d04df9b 4539 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
635374e7 4540 le32_to_cpu(mpi_reply->SenseCount));
0d04df9b 4541 memcpy(scmd->sense_buffer, sense_data, sz);
635374e7
EM
4542 _scsih_normalize_sense(scmd->sense_buffer, &data);
4543 /* failure prediction threshold exceeded */
4544 if (data.asc == 0x5D)
4545 _scsih_smart_predicted_fault(ioc,
4546 le16_to_cpu(mpi_reply->DevHandle));
4547 }
4548
4549 switch (ioc_status) {
4550 case MPI2_IOCSTATUS_BUSY:
4551 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4552 scmd->result = SAM_STAT_BUSY;
4553 break;
4554
4555 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4556 scmd->result = DID_NO_CONNECT << 16;
4557 break;
4558
4559 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4560 if (sas_device_priv_data->block) {
e4e7c7ed
KD
4561 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4562 goto out;
635374e7 4563 }
3ade1ca7 4564 scmd->result = DID_SOFT_ERROR << 16;
4565 break;
635374e7
EM
4566 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4567 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4568 scmd->result = DID_RESET << 16;
4569 break;
4570
4571 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4572 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4573 scmd->result = DID_SOFT_ERROR << 16;
4574 else
4575 scmd->result = (DID_OK << 16) | scsi_status;
4576 break;
4577
4578 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4579 scmd->result = (DID_OK << 16) | scsi_status;
4580
4581 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4582 break;
4583
4584 if (xfer_cnt < scmd->underflow) {
4585 if (scsi_status == SAM_STAT_BUSY)
4586 scmd->result = SAM_STAT_BUSY;
4587 else
4588 scmd->result = DID_SOFT_ERROR << 16;
4589 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4590 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4591 scmd->result = DID_SOFT_ERROR << 16;
4592 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4593 scmd->result = DID_RESET << 16;
4594 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4595 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4596 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4597 scmd->result = (DRIVER_SENSE << 24) |
4598 SAM_STAT_CHECK_CONDITION;
4599 scmd->sense_buffer[0] = 0x70;
4600 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4601 scmd->sense_buffer[12] = 0x20;
4602 scmd->sense_buffer[13] = 0;
4603 }
4604 break;
4605
4606 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4607 scsi_set_resid(scmd, 0);
4608 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4609 case MPI2_IOCSTATUS_SUCCESS:
4610 scmd->result = (DID_OK << 16) | scsi_status;
9982f594
KD
4611 if (response_code ==
4612 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4613 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4614 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
635374e7
EM
4615 scmd->result = DID_SOFT_ERROR << 16;
4616 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4617 scmd->result = DID_RESET << 16;
4618 break;
4619
3c621b3e
EM
4620 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4621 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4622 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4623 _scsih_eedp_error_handling(scmd, ioc_status);
4624 break;
635374e7
EM
4625 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4626 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4627 case MPI2_IOCSTATUS_INVALID_SGL:
4628 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4629 case MPI2_IOCSTATUS_INVALID_FIELD:
4630 case MPI2_IOCSTATUS_INVALID_STATE:
4631 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4632 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4633 default:
4634 scmd->result = DID_SOFT_ERROR << 16;
4635 break;
4636
4637 }
4638
4639#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4640 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4641 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4642#endif
4643
4644 out:
4645 scsi_dma_unmap(scmd);
4646 scmd->scsi_done(scmd);
77e63ed4 4647 return 1;
635374e7
EM
4648}
4649
635374e7
EM
4650/**
4651 * _scsih_sas_host_refresh - refreshing sas host object contents
4652 * @ioc: per adapter object
635374e7
EM
4653 * Context: user
4654 *
4655 * During port enable, fw will send topology events for every device. Its
4656 * possible that the handles may change from the previous setting, so this
4657 * code keeping handles updating if changed.
4658 *
4659 * Return nothing.
4660 */
4661static void
c5e039be 4662_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
635374e7
EM
4663{
4664 u16 sz;
4665 u16 ioc_status;
4666 int i;
4667 Mpi2ConfigReply_t mpi_reply;
4668 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
c5e039be 4669 u16 attached_handle;
7f6f794d 4670 u8 link_rate;
635374e7
EM
4671
4672 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4673 "updating handles for sas_host(0x%016llx)\n",
4674 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4675
4676 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4677 * sizeof(Mpi2SasIOUnit0PhyData_t));
4678 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4679 if (!sas_iounit_pg0) {
4680 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4681 ioc->name, __FILE__, __LINE__, __func__);
4682 return;
4683 }
635374e7 4684
c5e039be
KD
4685 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4686 sas_iounit_pg0, sz)) != 0)
4687 goto out;
4688 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4689 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4690 goto out;
4691 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
7f6f794d 4692 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
c5e039be
KD
4693 if (i == 0)
4694 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4695 PhyData[0].ControllerDevHandle);
4696 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4697 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4698 AttachedDevHandle);
7f6f794d
KD
4699 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4700 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
c5e039be 4701 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
7f6f794d 4702 attached_handle, i, link_rate);
c5e039be 4703 }
635374e7
EM
4704 out:
4705 kfree(sas_iounit_pg0);
4706}
4707
4708/**
4709 * _scsih_sas_host_add - create sas host object
4710 * @ioc: per adapter object
4711 *
4712 * Creating host side data object, stored in ioc->sas_hba
4713 *
4714 * Return nothing.
4715 */
4716static void
4717_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4718{
4719 int i;
4720 Mpi2ConfigReply_t mpi_reply;
4721 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4722 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4723 Mpi2SasPhyPage0_t phy_pg0;
4724 Mpi2SasDevicePage0_t sas_device_pg0;
4725 Mpi2SasEnclosurePage0_t enclosure_pg0;
4726 u16 ioc_status;
4727 u16 sz;
4728 u16 device_missing_delay;
4729
4730 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4731 if (!ioc->sas_hba.num_phys) {
4732 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4733 ioc->name, __FILE__, __LINE__, __func__);
4734 return;
4735 }
4736
4737 /* sas_iounit page 0 */
4738 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4739 sizeof(Mpi2SasIOUnit0PhyData_t));
4740 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4741 if (!sas_iounit_pg0) {
4742 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4743 ioc->name, __FILE__, __LINE__, __func__);
4744 return;
4745 }
4746 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4747 sas_iounit_pg0, sz))) {
4748 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4749 ioc->name, __FILE__, __LINE__, __func__);
4750 goto out;
4751 }
4752 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4753 MPI2_IOCSTATUS_MASK;
4754 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4755 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4756 ioc->name, __FILE__, __LINE__, __func__);
4757 goto out;
4758 }
4759
4760 /* sas_iounit page 1 */
4761 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4762 sizeof(Mpi2SasIOUnit1PhyData_t));
4763 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4764 if (!sas_iounit_pg1) {
4765 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4766 ioc->name, __FILE__, __LINE__, __func__);
4767 goto out;
4768 }
4769 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4770 sas_iounit_pg1, sz))) {
4771 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4772 ioc->name, __FILE__, __LINE__, __func__);
4773 goto out;
4774 }
4775 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4776 MPI2_IOCSTATUS_MASK;
4777 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4778 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4779 ioc->name, __FILE__, __LINE__, __func__);
4780 goto out;
4781 }
4782
4783 ioc->io_missing_delay =
4784 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4785 device_missing_delay =
4786 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4787 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4788 ioc->device_missing_delay = (device_missing_delay &
4789 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4790 else
4791 ioc->device_missing_delay = device_missing_delay &
4792 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4793
4794 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4795 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4796 sizeof(struct _sas_phy), GFP_KERNEL);
4797 if (!ioc->sas_hba.phy) {
4798 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4799 ioc->name, __FILE__, __LINE__, __func__);
4800 goto out;
4801 }
4802 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4803 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4804 i))) {
4805 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4806 ioc->name, __FILE__, __LINE__, __func__);
4807 goto out;
4808 }
4809 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4810 MPI2_IOCSTATUS_MASK;
4811 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4812 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4813 ioc->name, __FILE__, __LINE__, __func__);
4814 goto out;
4815 }
c5e039be
KD
4816
4817 if (i == 0)
4818 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4819 PhyData[0].ControllerDevHandle);
4820 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
635374e7
EM
4821 ioc->sas_hba.phy[i].phy_id = i;
4822 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4823 phy_pg0, ioc->sas_hba.parent_dev);
4824 }
4825 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
c5e039be 4826 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
635374e7
EM
4827 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4828 ioc->name, __FILE__, __LINE__, __func__);
4829 goto out;
4830 }
635374e7
EM
4831 ioc->sas_hba.enclosure_handle =
4832 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4833 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4834 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4835 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4836 (unsigned long long) ioc->sas_hba.sas_address,
4837 ioc->sas_hba.num_phys) ;
4838
4839 if (ioc->sas_hba.enclosure_handle) {
4840 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4841 &enclosure_pg0,
4842 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4843 ioc->sas_hba.enclosure_handle))) {
4844 ioc->sas_hba.enclosure_logical_id =
4845 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4846 }
4847 }
4848
4849 out:
4850 kfree(sas_iounit_pg1);
4851 kfree(sas_iounit_pg0);
4852}
4853
4854/**
4855 * _scsih_expander_add - creating expander object
4856 * @ioc: per adapter object
4857 * @handle: expander handle
4858 *
4859 * Creating expander object, stored in ioc->sas_expander_list.
4860 *
4861 * Return 0 for success, else error.
4862 */
4863static int
4864_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4865{
4866 struct _sas_node *sas_expander;
4867 Mpi2ConfigReply_t mpi_reply;
4868 Mpi2ExpanderPage0_t expander_pg0;
4869 Mpi2ExpanderPage1_t expander_pg1;
4870 Mpi2SasEnclosurePage0_t enclosure_pg0;
4871 u32 ioc_status;
4872 u16 parent_handle;
c97951ec 4873 u64 sas_address, sas_address_parent = 0;
635374e7
EM
4874 int i;
4875 unsigned long flags;
20f5895d 4876 struct _sas_port *mpt2sas_port = NULL;
635374e7
EM
4877 int rc = 0;
4878
4879 if (!handle)
4880 return -1;
4881
3cb5469a 4882 if (ioc->shost_recovery || ioc->pci_error_recovery)
155dd4c7
KD
4883 return -1;
4884
635374e7
EM
4885 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4886 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4887 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4888 ioc->name, __FILE__, __LINE__, __func__);
4889 return -1;
4890 }
4891
4892 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4893 MPI2_IOCSTATUS_MASK;
4894 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4895 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4896 ioc->name, __FILE__, __LINE__, __func__);
4897 return -1;
4898 }
4899
4900 /* handle out of order topology events */
4901 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
c5e039be
KD
4902 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4903 != 0) {
4904 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4905 ioc->name, __FILE__, __LINE__, __func__);
4906 return -1;
4907 }
4908 if (sas_address_parent != ioc->sas_hba.sas_address) {
635374e7 4909 spin_lock_irqsave(&ioc->sas_node_lock, flags);
c5e039be
KD
4910 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4911 sas_address_parent);
635374e7
EM
4912 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4913 if (!sas_expander) {
4914 rc = _scsih_expander_add(ioc, parent_handle);
4915 if (rc != 0)
4916 return rc;
4917 }
4918 }
4919
635374e7 4920 spin_lock_irqsave(&ioc->sas_node_lock, flags);
595bb0bd 4921 sas_address = le64_to_cpu(expander_pg0.SASAddress);
635374e7
EM
4922 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4923 sas_address);
4924 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4925
4926 if (sas_expander)
4927 return 0;
4928
4929 sas_expander = kzalloc(sizeof(struct _sas_node),
4930 GFP_KERNEL);
4931 if (!sas_expander) {
4932 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4933 ioc->name, __FILE__, __LINE__, __func__);
4934 return -1;
4935 }
4936
4937 sas_expander->handle = handle;
4938 sas_expander->num_phys = expander_pg0.NumPhys;
c5e039be 4939 sas_expander->sas_address_parent = sas_address_parent;
635374e7
EM
4940 sas_expander->sas_address = sas_address;
4941
4942 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4943 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
c5e039be 4944 handle, parent_handle, (unsigned long long)
635374e7
EM
4945 sas_expander->sas_address, sas_expander->num_phys);
4946
4947 if (!sas_expander->num_phys)
4948 goto out_fail;
4949 sas_expander->phy = kcalloc(sas_expander->num_phys,
4950 sizeof(struct _sas_phy), GFP_KERNEL);
4951 if (!sas_expander->phy) {
4952 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4953 ioc->name, __FILE__, __LINE__, __func__);
4954 rc = -1;
4955 goto out_fail;
4956 }
4957
4958 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4959 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
c5e039be 4960 sas_address_parent);
635374e7
EM
4961 if (!mpt2sas_port) {
4962 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4963 ioc->name, __FILE__, __LINE__, __func__);
4964 rc = -1;
4965 goto out_fail;
4966 }
4967 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4968
4969 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4970 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4971 &expander_pg1, i, handle))) {
4972 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4973 ioc->name, __FILE__, __LINE__, __func__);
20f5895d
KD
4974 rc = -1;
4975 goto out_fail;
635374e7
EM
4976 }
4977 sas_expander->phy[i].handle = handle;
4978 sas_expander->phy[i].phy_id = i;
20f5895d
KD
4979
4980 if ((mpt2sas_transport_add_expander_phy(ioc,
4981 &sas_expander->phy[i], expander_pg1,
4982 sas_expander->parent_dev))) {
4983 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4984 ioc->name, __FILE__, __LINE__, __func__);
4985 rc = -1;
4986 goto out_fail;
4987 }
635374e7
EM
4988 }
4989
4990 if (sas_expander->enclosure_handle) {
4991 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4992 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4993 sas_expander->enclosure_handle))) {
4994 sas_expander->enclosure_logical_id =
4995 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4996 }
4997 }
4998
4999 _scsih_expander_node_add(ioc, sas_expander);
5000 return 0;
5001
5002 out_fail:
5003
20f5895d
KD
5004 if (mpt2sas_port)
5005 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
c5e039be 5006 sas_address_parent);
635374e7
EM
5007 kfree(sas_expander);
5008 return rc;
5009}
5010
744090d3
KD
5011/**
5012 * _scsih_done - scsih callback handler.
5013 * @ioc: per adapter object
5014 * @smid: system request message index
5015 * @msix_index: MSIX table index supplied by the OS
5016 * @reply: reply message frame(lower 32bit addr)
5017 *
5018 * Callback handler when sending internal generated message frames.
5019 * The callback index passed is `ioc->scsih_cb_idx`
5020 *
5021 * Return 1 meaning mf should be freed from _base_interrupt
5022 * 0 means the mf is freed from this function.
5023 */
5024static u8
5025_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5026{
5027 MPI2DefaultReply_t *mpi_reply;
5028
5029 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5030 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5031 return 1;
5032 if (ioc->scsih_cmds.smid != smid)
5033 return 1;
5034 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5035 if (mpi_reply) {
5036 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5037 mpi_reply->MsgLength*4);
5038 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5039 }
5040 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5041 complete(&ioc->scsih_cmds.done);
5042 return 1;
5043}
5044
635374e7 5045/**
7f6f794d 5046 * mpt2sas_expander_remove - removing expander object
635374e7 5047 * @ioc: per adapter object
c5e039be 5048 * @sas_address: expander sas_address
635374e7
EM
5049 *
5050 * Return nothing.
5051 */
7f6f794d
KD
5052void
5053mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
635374e7
EM
5054{
5055 struct _sas_node *sas_expander;
5056 unsigned long flags;
5057
155dd4c7
KD
5058 if (ioc->shost_recovery)
5059 return;
5060
635374e7 5061 spin_lock_irqsave(&ioc->sas_node_lock, flags);
c5e039be
KD
5062 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5063 sas_address);
09da0b32 5064 if (sas_expander)
5065 list_del(&sas_expander->list);
635374e7 5066 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
09da0b32 5067 if (sas_expander)
5068 _scsih_expander_node_remove(ioc, sas_expander);
635374e7
EM
5069}
5070
b4344276
KD
5071/**
5072 * _scsih_check_access_status - check access flags
5073 * @ioc: per adapter object
5074 * @sas_address: sas address
5075 * @handle: sas device handle
5076 * @access_flags: errors returned during discovery of the device
5077 *
5078 * Return 0 for success, else failure
5079 */
5080static u8
5081_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5082 u16 handle, u8 access_status)
5083{
5084 u8 rc = 1;
5085 char *desc = NULL;
5086
5087 switch (access_status) {
5088 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5089 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5090 rc = 0;
5091 break;
5092 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5093 desc = "sata capability failed";
5094 break;
5095 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5096 desc = "sata affiliation conflict";
5097 break;
5098 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5099 desc = "route not addressable";
5100 break;
5101 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5102 desc = "smp error not addressable";
5103 break;
5104 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5105 desc = "device blocked";
5106 break;
5107 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5108 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5109 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5110 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5111 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5112 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5113 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5114 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5115 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5116 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5117 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5118 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5119 desc = "sata initialization failed";
5120 break;
5121 default:
5122 desc = "unknown";
5123 break;
5124 }
5125
5126 if (!rc)
5127 return 0;
5128
5129 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5130 "handle(0x%04x)\n", ioc->name, desc,
5131 (unsigned long long)sas_address, handle);
5132 return rc;
5133}
5134
5135static void
5136_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5137{
5138 Mpi2ConfigReply_t mpi_reply;
5139 Mpi2SasDevicePage0_t sas_device_pg0;
5140 struct _sas_device *sas_device;
5141 u32 ioc_status;
5142 unsigned long flags;
5143 u64 sas_address;
5144 struct scsi_target *starget;
5145 struct MPT2SAS_TARGET *sas_target_priv_data;
5146 u32 device_info;
5147
09da0b32 5148
b4344276
KD
5149 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5150 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5151 return;
5152
5153 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5154 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5155 return;
5156
5157 /* check if this is end device */
5158 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5159 if (!(_scsih_is_end_device(device_info)))
5160 return;
5161
5162 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5163 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5164 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5165 sas_address);
5166
5167 if (!sas_device) {
5168 printk(MPT2SAS_ERR_FMT "device is not present "
5169 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5170 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5171 return;
5172 }
5173
5174 if (unlikely(sas_device->handle != handle)) {
5175 starget = sas_device->starget;
5176 sas_target_priv_data = starget->hostdata;
5177 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5178 " to (0x%04x)!!!\n", sas_device->handle, handle);
5179 sas_target_priv_data->handle = handle;
5180 sas_device->handle = handle;
5181 }
b4344276
KD
5182
5183 /* check if device is present */
5184 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5185 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5186 printk(MPT2SAS_ERR_FMT "device is not present "
5187 "handle(0x%04x), flags!!!\n", ioc->name, handle);
09da0b32 5188 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
b4344276
KD
5189 return;
5190 }
5191
5192 /* check if there were any issues with discovery */
5193 if (_scsih_check_access_status(ioc, sas_address, handle,
09da0b32 5194 sas_device_pg0.AccessStatus)) {
5195 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
b4344276 5196 return;
09da0b32 5197 }
5198 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
39af7a98 5199 _scsih_ublock_io_device(ioc, sas_address);
b4344276
KD
5200
5201}
5202
635374e7
EM
5203/**
5204 * _scsih_add_device - creating sas device object
5205 * @ioc: per adapter object
5206 * @handle: sas device handle
5207 * @phy_num: phy number end device attached to
5208 * @is_pd: is this hidden raid component
5209 *
5210 * Creating end device object, stored in ioc->sas_device_list.
5211 *
5212 * Returns 0 for success, non-zero for failure.
5213 */
5214static int
5215_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5216{
5217 Mpi2ConfigReply_t mpi_reply;
5218 Mpi2SasDevicePage0_t sas_device_pg0;
5219 Mpi2SasEnclosurePage0_t enclosure_pg0;
5220 struct _sas_device *sas_device;
5221 u32 ioc_status;
5222 __le64 sas_address;
5223 u32 device_info;
5224 unsigned long flags;
5225
5226 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5227 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5228 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5229 ioc->name, __FILE__, __LINE__, __func__);
5230 return -1;
5231 }
5232
5233 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5234 MPI2_IOCSTATUS_MASK;
5235 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5236 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5237 ioc->name, __FILE__, __LINE__, __func__);
5238 return -1;
5239 }
5240
b4344276
KD
5241 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5242
635374e7
EM
5243 /* check if device is present */
5244 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5245 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5246 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5247 ioc->name, __FILE__, __LINE__, __func__);
5248 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5249 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5250 return -1;
5251 }
5252
b4344276
KD
5253 /* check if there were any issues with discovery */
5254 if (_scsih_check_access_status(ioc, sas_address, handle,
5255 sas_device_pg0.AccessStatus))
635374e7 5256 return -1;
635374e7
EM
5257
5258 /* check if this is end device */
5259 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5260 if (!(_scsih_is_end_device(device_info))) {
5261 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5262 ioc->name, __FILE__, __LINE__, __func__);
5263 return -1;
5264 }
5265
635374e7
EM
5266
5267 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5268 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5269 sas_address);
5270 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5271
b4344276 5272 if (sas_device)
635374e7 5273 return 0;
635374e7
EM
5274
5275 sas_device = kzalloc(sizeof(struct _sas_device),
5276 GFP_KERNEL);
5277 if (!sas_device) {
5278 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5279 ioc->name, __FILE__, __LINE__, __func__);
5280 return -1;
5281 }
5282
5283 sas_device->handle = handle;
c5e039be
KD
5284 if (_scsih_get_sas_address(ioc, le16_to_cpu
5285 (sas_device_pg0.ParentDevHandle),
5286 &sas_device->sas_address_parent) != 0)
5287 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5288 ioc->name, __FILE__, __LINE__, __func__);
635374e7
EM
5289 sas_device->enclosure_handle =
5290 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5291 sas_device->slot =
5292 le16_to_cpu(sas_device_pg0.Slot);
5293 sas_device->device_info = device_info;
5294 sas_device->sas_address = sas_address;
7fbae67a 5295 sas_device->phy = sas_device_pg0.PhyNum;
635374e7
EM
5296
5297 /* get enclosure_logical_id */
15052c9e
KD
5298 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5299 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5300 sas_device->enclosure_handle)))
635374e7
EM
5301 sas_device->enclosure_logical_id =
5302 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
635374e7
EM
5303
5304 /* get device name */
5305 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5306
921cd802 5307 if (ioc->wait_for_discovery_to_complete)
635374e7
EM
5308 _scsih_sas_device_init_add(ioc, sas_device);
5309 else
5310 _scsih_sas_device_add(ioc, sas_device);
5311
5312 return 0;
5313}
5314
1278b11f
KD
5315/**
5316 * _scsih_remove_device - removing sas device object
5317 * @ioc: per adapter object
5318 * @sas_device_delete: the sas_device object
5319 *
5320 * Return nothing.
5321 */
5322static void
5323_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5324 struct _sas_device *sas_device)
5325{
1278b11f 5326 struct MPT2SAS_TARGET *sas_target_priv_data;
34a03bef 5327
1278b11f
KD
5328 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5329 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
09da0b32 5330 sas_device->handle, (unsigned long long)
5331 sas_device->sas_address));
1278b11f 5332
09da0b32 5333 if (sas_device->starget && sas_device->starget->hostdata) {
5334 sas_target_priv_data = sas_device->starget->hostdata;
1278b11f 5335 sas_target_priv_data->deleted = 1;
39af7a98 5336 _scsih_ublock_io_device(ioc, sas_device->sas_address);
918134ef 5337 sas_target_priv_data->handle =
5338 MPT2SAS_INVALID_DEVICE_HANDLE;
1278b11f
KD
5339 }
5340
0bdccdb0
KD
5341 if (!ioc->hide_drives)
5342 mpt2sas_transport_port_remove(ioc,
09da0b32 5343 sas_device->sas_address,
5344 sas_device->sas_address_parent);
635374e7
EM
5345
5346 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
09da0b32 5347 "(0x%016llx)\n", ioc->name, sas_device->handle,
5348 (unsigned long long) sas_device->sas_address);
635374e7 5349
1278b11f
KD
5350 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5351 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
09da0b32 5352 sas_device->handle, (unsigned long long)
5353 sas_device->sas_address));
5354 kfree(sas_device);
5355}
5356/**
5357 * _scsih_device_remove_by_handle - removing device object by handle
5358 * @ioc: per adapter object
5359 * @handle: device handle
5360 *
5361 * Return nothing.
5362 */
5363static void
5364_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5365{
5366 struct _sas_device *sas_device;
5367 unsigned long flags;
5368
5369 if (ioc->shost_recovery)
5370 return;
5371
5372 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5373 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5374 if (sas_device)
5375 list_del(&sas_device->list);
5376 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5377 if (sas_device)
5378 _scsih_remove_device(ioc, sas_device);
635374e7
EM
5379}
5380
7f6f794d 5381/**
09da0b32 5382 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
7f6f794d 5383 * @ioc: per adapter object
09da0b32 5384 * @sas_address: device sas_address
7f6f794d
KD
5385 *
5386 * Return nothing.
5387 */
5388void
09da0b32 5389mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5390 u64 sas_address)
7f6f794d
KD
5391{
5392 struct _sas_device *sas_device;
5393 unsigned long flags;
5394
5395 if (ioc->shost_recovery)
5396 return;
5397
5398 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5399 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5400 sas_address);
09da0b32 5401 if (sas_device)
5402 list_del(&sas_device->list);
7f6f794d 5403 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
09da0b32 5404 if (sas_device)
5405 _scsih_remove_device(ioc, sas_device);
7f6f794d 5406}
635374e7
EM
5407#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5408/**
5409 * _scsih_sas_topology_change_event_debug - debug for topology event
5410 * @ioc: per adapter object
5411 * @event_data: event data payload
5412 * Context: user.
5413 */
5414static void
5415_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5416 Mpi2EventDataSasTopologyChangeList_t *event_data)
5417{
5418 int i;
5419 u16 handle;
5420 u16 reason_code;
5421 u8 phy_number;
5422 char *status_str = NULL;
e7d59c17 5423 u8 link_rate, prev_link_rate;
635374e7
EM
5424
5425 switch (event_data->ExpStatus) {
5426 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5427 status_str = "add";
5428 break;
5429 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5430 status_str = "remove";
5431 break;
5432 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
e7d59c17 5433 case 0:
635374e7
EM
5434 status_str = "responding";
5435 break;
5436 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5437 status_str = "remove delay";
5438 break;
5439 default:
5440 status_str = "unknown status";
5441 break;
5442 }
eabb08ad 5443 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
635374e7 5444 ioc->name, status_str);
eabb08ad 5445 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
635374e7
EM
5446 "start_phy(%02d), count(%d)\n",
5447 le16_to_cpu(event_data->ExpanderDevHandle),
5448 le16_to_cpu(event_data->EnclosureHandle),
5449 event_data->StartPhyNum, event_data->NumEntries);
5450 for (i = 0; i < event_data->NumEntries; i++) {
5451 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5452 if (!handle)
5453 continue;
5454 phy_number = event_data->StartPhyNum + i;
5455 reason_code = event_data->PHY[i].PhyStatus &
5456 MPI2_EVENT_SAS_TOPO_RC_MASK;
5457 switch (reason_code) {
5458 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
e7d59c17 5459 status_str = "target add";
635374e7
EM
5460 break;
5461 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
e7d59c17 5462 status_str = "target remove";
635374e7
EM
5463 break;
5464 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
e7d59c17 5465 status_str = "delay target remove";
635374e7
EM
5466 break;
5467 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
e7d59c17 5468 status_str = "link rate change";
635374e7
EM
5469 break;
5470 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
e7d59c17 5471 status_str = "target responding";
635374e7
EM
5472 break;
5473 default:
e7d59c17 5474 status_str = "unknown";
635374e7
EM
5475 break;
5476 }
e7d59c17
KD
5477 link_rate = event_data->PHY[i].LinkRate >> 4;
5478 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
eabb08ad 5479 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
e7d59c17
KD
5480 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5481 handle, status_str, link_rate, prev_link_rate);
5482
635374e7
EM
5483 }
5484}
5485#endif
5486
5487/**
5488 * _scsih_sas_topology_change_event - handle topology changes
5489 * @ioc: per adapter object
7b936b02 5490 * @fw_event: The fw_event_work object
635374e7
EM
5491 * Context: user.
5492 *
5493 */
5494static void
7b936b02 5495_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
5496 struct fw_event_work *fw_event)
5497{
5498 int i;
5499 u16 parent_handle, handle;
5500 u16 reason_code;
b41c09d1 5501 u8 phy_number, max_phys;
635374e7 5502 struct _sas_node *sas_expander;
c5e039be 5503 u64 sas_address;
635374e7 5504 unsigned long flags;
e7d59c17 5505 u8 link_rate, prev_link_rate;
00713ad7
JL
5506 Mpi2EventDataSasTopologyChangeList_t *event_data =
5507 (Mpi2EventDataSasTopologyChangeList_t *)
5508 fw_event->event_data;
635374e7
EM
5509
5510#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5511 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5512 _scsih_sas_topology_change_event_debug(ioc, event_data);
5513#endif
5514
918134ef 5515 if (ioc->remove_host || ioc->pci_error_recovery)
c5e039be
KD
5516 return;
5517
635374e7
EM
5518 if (!ioc->sas_hba.num_phys)
5519 _scsih_sas_host_add(ioc);
5520 else
c5e039be 5521 _scsih_sas_host_refresh(ioc);
635374e7
EM
5522
5523 if (fw_event->ignore) {
eabb08ad 5524 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
635374e7
EM
5525 "event\n", ioc->name));
5526 return;
5527 }
5528
5529 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5530
5531 /* handle expander add */
5532 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5533 if (_scsih_expander_add(ioc, parent_handle) != 0)
5534 return;
5535
c5e039be
KD
5536 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5537 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5538 parent_handle);
b41c09d1 5539 if (sas_expander) {
c5e039be 5540 sas_address = sas_expander->sas_address;
b41c09d1
KD
5541 max_phys = sas_expander->num_phys;
5542 } else if (parent_handle < ioc->sas_hba.num_phys) {
c5e039be 5543 sas_address = ioc->sas_hba.sas_address;
b41c09d1 5544 max_phys = ioc->sas_hba.num_phys;
09da0b32 5545 } else {
5546 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
c5e039be 5547 return;
09da0b32 5548 }
5549 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
c5e039be 5550
635374e7
EM
5551 /* handle siblings events */
5552 for (i = 0; i < event_data->NumEntries; i++) {
5553 if (fw_event->ignore) {
eabb08ad 5554 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
635374e7
EM
5555 "expander event\n", ioc->name));
5556 return;
5557 }
3cb5469a
EM
5558 if (ioc->shost_recovery || ioc->remove_host ||
5559 ioc->pci_error_recovery)
155dd4c7 5560 return;
308609c6 5561 phy_number = event_data->StartPhyNum + i;
b41c09d1
KD
5562 if (phy_number >= max_phys)
5563 continue;
308609c6
KD
5564 reason_code = event_data->PHY[i].PhyStatus &
5565 MPI2_EVENT_SAS_TOPO_RC_MASK;
5566 if ((event_data->PHY[i].PhyStatus &
5567 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5568 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
635374e7
EM
5569 continue;
5570 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5571 if (!handle)
5572 continue;
c5e039be 5573 link_rate = event_data->PHY[i].LinkRate >> 4;
e7d59c17 5574 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
635374e7
EM
5575 switch (reason_code) {
5576 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
e7d59c17 5577
918134ef 5578 if (ioc->shost_recovery)
5579 break;
5580
e7d59c17
KD
5581 if (link_rate == prev_link_rate)
5582 break;
5583
5584 mpt2sas_transport_update_links(ioc, sas_address,
5585 handle, phy_number, link_rate);
5586
b4344276
KD
5587 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5588 break;
5589
5590 _scsih_check_device(ioc, handle);
e7d59c17 5591 break;
635374e7 5592 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
c5e039be 5593
918134ef 5594 if (ioc->shost_recovery)
5595 break;
5596
c5e039be
KD
5597 mpt2sas_transport_update_links(ioc, sas_address,
5598 handle, phy_number, link_rate);
5599
e7d59c17 5600 _scsih_add_device(ioc, handle, phy_number, 0);
635374e7
EM
5601 break;
5602 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
c5e039be 5603
09da0b32 5604 _scsih_device_remove_by_handle(ioc, handle);
635374e7
EM
5605 break;
5606 }
5607 }
5608
5609 /* handle expander removal */
c5e039be
KD
5610 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5611 sas_expander)
7f6f794d 5612 mpt2sas_expander_remove(ioc, sas_address);
635374e7
EM
5613
5614}
5615
5616#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5617/**
5618 * _scsih_sas_device_status_change_event_debug - debug for device event
5619 * @event_data: event data payload
5620 * Context: user.
5621 *
5622 * Return nothing.
5623 */
5624static void
5625_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5626 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5627{
5628 char *reason_str = NULL;
5629
5630 switch (event_data->ReasonCode) {
5631 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5632 reason_str = "smart data";
5633 break;
5634 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5635 reason_str = "unsupported device discovered";
5636 break;
5637 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5638 reason_str = "internal device reset";
5639 break;
5640 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5641 reason_str = "internal task abort";
5642 break;
5643 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5644 reason_str = "internal task abort set";
5645 break;
5646 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5647 reason_str = "internal clear task set";
5648 break;
5649 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5650 reason_str = "internal query task";
5651 break;
5652 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5653 reason_str = "sata init failure";
5654 break;
5655 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5656 reason_str = "internal device reset complete";
5657 break;
5658 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5659 reason_str = "internal task abort complete";
5660 break;
5661 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5662 reason_str = "internal async notification";
5663 break;
ec6c2b43
KD
5664 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5665 reason_str = "expander reduced functionality";
5666 break;
5667 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5668 reason_str = "expander reduced functionality complete";
5669 break;
635374e7
EM
5670 default:
5671 reason_str = "unknown reason";
5672 break;
5673 }
eabb08ad 5674 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
f93213de
KD
5675 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5676 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5677 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5678 le16_to_cpu(event_data->TaskTag));
635374e7 5679 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
eabb08ad 5680 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
635374e7
EM
5681 event_data->ASC, event_data->ASCQ);
5682 printk(KERN_INFO "\n");
5683}
5684#endif
5685
5686/**
5687 * _scsih_sas_device_status_change_event - handle device status change
5688 * @ioc: per adapter object
7b936b02 5689 * @fw_event: The fw_event_work object
635374e7
EM
5690 * Context: user.
5691 *
5692 * Return nothing.
5693 */
5694static void
7b936b02
KD
5695_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5696 struct fw_event_work *fw_event)
635374e7 5697{
8ffc457e
KD
5698 struct MPT2SAS_TARGET *target_priv_data;
5699 struct _sas_device *sas_device;
c97951ec 5700 u64 sas_address;
8ffc457e
KD
5701 unsigned long flags;
5702 Mpi2EventDataSasDeviceStatusChange_t *event_data =
00713ad7
JL
5703 (Mpi2EventDataSasDeviceStatusChange_t *)
5704 fw_event->event_data;
8ffc457e 5705
635374e7
EM
5706#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5707 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
7b936b02 5708 _scsih_sas_device_status_change_event_debug(ioc,
8ffc457e 5709 event_data);
635374e7 5710#endif
8ffc457e 5711
efe82a16
KD
5712 /* In MPI Revision K (0xC), the internal device reset complete was
5713 * implemented, so avoid setting tm_busy flag for older firmware.
5714 */
5715 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5716 return;
5717
f891dcfd 5718 if (event_data->ReasonCode !=
8ffc457e 5719 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
f891dcfd
KD
5720 event_data->ReasonCode !=
5721 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
8ffc457e
KD
5722 return;
5723
5724 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5725 sas_address = le64_to_cpu(event_data->SASAddress);
5726 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5727 sas_address);
8ffc457e 5728
09da0b32 5729 if (!sas_device || !sas_device->starget) {
5730 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8ffc457e 5731 return;
09da0b32 5732 }
8ffc457e
KD
5733
5734 target_priv_data = sas_device->starget->hostdata;
09da0b32 5735 if (!target_priv_data) {
5736 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8ffc457e 5737 return;
09da0b32 5738 }
8ffc457e
KD
5739
5740 if (event_data->ReasonCode ==
5741 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5742 target_priv_data->tm_busy = 1;
5743 else
5744 target_priv_data->tm_busy = 0;
09da0b32 5745 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
635374e7
EM
5746}
5747
5748#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5749/**
5750 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5751 * @ioc: per adapter object
5752 * @event_data: event data payload
5753 * Context: user.
5754 *
5755 * Return nothing.
5756 */
5757static void
5758_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5759 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5760{
5761 char *reason_str = NULL;
5762
5763 switch (event_data->ReasonCode) {
5764 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5765 reason_str = "enclosure add";
5766 break;
5767 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5768 reason_str = "enclosure remove";
5769 break;
5770 default:
5771 reason_str = "unknown reason";
5772 break;
5773 }
5774
eabb08ad 5775 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
635374e7
EM
5776 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5777 " number slots(%d)\n", ioc->name, reason_str,
5778 le16_to_cpu(event_data->EnclosureHandle),
5779 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5780 le16_to_cpu(event_data->StartSlot));
5781}
5782#endif
5783
5784/**
5785 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5786 * @ioc: per adapter object
7b936b02 5787 * @fw_event: The fw_event_work object
635374e7
EM
5788 * Context: user.
5789 *
5790 * Return nothing.
5791 */
5792static void
5793_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
7b936b02 5794 struct fw_event_work *fw_event)
635374e7
EM
5795{
5796#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5797 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5798 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
00713ad7 5799 (Mpi2EventDataSasEnclDevStatusChange_t *)
7b936b02 5800 fw_event->event_data);
635374e7
EM
5801#endif
5802}
5803
5804/**
a78e21dc 5805 * _scsih_sas_broadcast_primitive_event - handle broadcast events
635374e7 5806 * @ioc: per adapter object
7b936b02 5807 * @fw_event: The fw_event_work object
635374e7
EM
5808 * Context: user.
5809 *
5810 * Return nothing.
5811 */
5812static void
a78e21dc 5813_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
7b936b02 5814 struct fw_event_work *fw_event)
635374e7
EM
5815{
5816 struct scsi_cmnd *scmd;
ec07a053 5817 struct scsi_device *sdev;
635374e7
EM
5818 u16 smid, handle;
5819 u32 lun;
5820 struct MPT2SAS_DEVICE *sas_device_priv_data;
5821 u32 termination_count;
5822 u32 query_count;
5823 Mpi2SCSITaskManagementReply_t *mpi_reply;
00713ad7
JL
5824 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5825 (Mpi2EventDataSasBroadcastPrimitive_t *)
5826 fw_event->event_data;
463217bf 5827 u16 ioc_status;
ec07a053
KD
5828 unsigned long flags;
5829 int r;
f93213de
KD
5830 u8 max_retries = 0;
5831 u8 task_abort_retries;
ec07a053 5832
f93213de 5833 mutex_lock(&ioc->tm_cmds.mutex);
c3a634bf
RS
5834 pr_info(MPT2SAS_FMT
5835 "%s: enter: phy number(%d), width(%d)\n",
5836 ioc->name, __func__, event_data->PhyNum,
5837 event_data->PortWidth);
f93213de
KD
5838
5839 _scsih_block_io_all_device(ioc);
635374e7 5840
ec07a053 5841 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
f93213de
KD
5842 mpi_reply = ioc->tm_cmds.reply;
5843broadcast_aen_retry:
5844
5845 /* sanity checks for retrying this loop */
5846 if (max_retries++ == 5) {
5847 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5848 ioc->name, __func__));
5849 goto out;
5850 } else if (max_retries > 1)
5851 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5852 ioc->name, __func__, max_retries - 1));
5853
635374e7
EM
5854 termination_count = 0;
5855 query_count = 0;
595bb0bd 5856 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
921cd802 5857 if (ioc->shost_recovery)
f93213de 5858 goto out;
635374e7
EM
5859 scmd = _scsih_scsi_lookup_get(ioc, smid);
5860 if (!scmd)
5861 continue;
ec07a053
KD
5862 sdev = scmd->device;
5863 sas_device_priv_data = sdev->hostdata;
635374e7
EM
5864 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5865 continue;
5866 /* skip hidden raid components */
5867 if (sas_device_priv_data->sas_target->flags &
5868 MPT_TARGET_FLAGS_RAID_COMPONENT)
5869 continue;
5870 /* skip volumes */
5871 if (sas_device_priv_data->sas_target->flags &
5872 MPT_TARGET_FLAGS_VOLUME)
5873 continue;
5874
5875 handle = sas_device_priv_data->sas_target->handle;
5876 lun = sas_device_priv_data->lun;
5877 query_count++;
5878
921cd802 5879 if (ioc->shost_recovery)
f93213de
KD
5880 goto out;
5881
ec07a053 5882 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
f93213de 5883 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
2e45c8b7 5884 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
f93213de
KD
5885 TM_MUTEX_OFF);
5886 if (r == FAILED) {
5887 sdev_printk(KERN_WARNING, sdev,
5888 "mpt2sas_scsih_issue_tm: FAILED when sending "
5889 "QUERY_TASK: scmd(%p)\n", scmd);
5890 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5891 goto broadcast_aen_retry;
5892 }
463217bf
KD
5893 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5894 & MPI2_IOCSTATUS_MASK;
f93213de
KD
5895 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5896 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5897 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5898 scmd);
5899 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5900 goto broadcast_aen_retry;
5901 }
5902
5903 /* see if IO is still owned by IOC and target */
5904 if (mpi_reply->ResponseCode ==
635374e7
EM
5905 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5906 mpi_reply->ResponseCode ==
f93213de 5907 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
ec07a053 5908 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
635374e7 5909 continue;
ec07a053 5910 }
f93213de
KD
5911 task_abort_retries = 0;
5912 tm_retry:
5913 if (task_abort_retries++ == 60) {
5914 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5915 "%s: ABORT_TASK: giving up\n", ioc->name,
5916 __func__));
5917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5918 goto broadcast_aen_retry;
5919 }
5920
921cd802 5921 if (ioc->shost_recovery)
f93213de
KD
5922 goto out_no_lock;
5923
ec07a053
KD
5924 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5925 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2e45c8b7 5926 TM_MUTEX_OFF);
f93213de
KD
5927 if (r == FAILED) {
5928 sdev_printk(KERN_WARNING, sdev,
5929 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
ec07a053 5930 "scmd(%p)\n", scmd);
f93213de
KD
5931 goto tm_retry;
5932 }
5933
5934 if (task_abort_retries > 1)
5935 sdev_printk(KERN_WARNING, sdev,
5936 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5937 " scmd(%p)\n",
5938 task_abort_retries - 1, scmd);
5939
635374e7 5940 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
ec07a053 5941 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
635374e7 5942 }
f93213de
KD
5943
5944 if (ioc->broadcast_aen_pending) {
5945 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5946 " pending AEN\n", ioc->name, __func__));
5947 ioc->broadcast_aen_pending = 0;
5948 goto broadcast_aen_retry;
5949 }
5950
5951 out:
ec07a053 5952 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
f93213de 5953 out_no_lock:
635374e7 5954
f93213de 5955 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
5956 "%s - exit, query_count = %d termination_count = %d\n",
5957 ioc->name, __func__, query_count, termination_count));
f93213de
KD
5958
5959 ioc->broadcast_aen_busy = 0;
921cd802 5960 if (!ioc->shost_recovery)
f93213de
KD
5961 _scsih_ublock_io_all_device(ioc);
5962 mutex_unlock(&ioc->tm_cmds.mutex);
635374e7
EM
5963}
5964
5965/**
5966 * _scsih_sas_discovery_event - handle discovery events
5967 * @ioc: per adapter object
7b936b02 5968 * @fw_event: The fw_event_work object
635374e7
EM
5969 * Context: user.
5970 *
5971 * Return nothing.
5972 */
5973static void
7b936b02
KD
5974_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5975 struct fw_event_work *fw_event)
635374e7 5976{
00713ad7
JL
5977 Mpi2EventDataSasDiscovery_t *event_data =
5978 (Mpi2EventDataSasDiscovery_t *)
5979 fw_event->event_data;
7b936b02 5980
635374e7
EM
5981#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5982 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
eabb08ad 5983 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
635374e7
EM
5984 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5985 "start" : "stop");
5986 if (event_data->DiscoveryStatus)
595bb0bd
KD
5987 printk("discovery_status(0x%08x)",
5988 le32_to_cpu(event_data->DiscoveryStatus));
635374e7
EM
5989 printk("\n");
5990 }
5991#endif
5992
5993 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4b193175 5994 !ioc->sas_hba.num_phys) {
5995 if (disable_discovery > 0 && ioc->shost_recovery) {
5996 /* Wait for the reset to complete */
5997 while (ioc->shost_recovery)
5998 ssleep(1);
5999 }
635374e7 6000 _scsih_sas_host_add(ioc);
4b193175 6001 }
635374e7
EM
6002}
6003
6004/**
6005 * _scsih_reprobe_lun - reprobing lun
6006 * @sdev: scsi device struct
6007 * @no_uld_attach: sdev->no_uld_attach flag setting
6008 *
6009 **/
6010static void
6011_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6012{
6013 int rc;
6014
6015 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6016 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6017 sdev->no_uld_attach ? "hidding" : "exposing");
6018 rc = scsi_device_reprobe(sdev);
6019}
6020
635374e7
EM
6021/**
6022 * _scsih_sas_volume_add - add new volume
6023 * @ioc: per adapter object
6024 * @element: IR config element data
6025 * Context: user.
6026 *
6027 * Return nothing.
6028 */
6029static void
6030_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6031 Mpi2EventIrConfigElement_t *element)
6032{
6033 struct _raid_device *raid_device;
6034 unsigned long flags;
6035 u64 wwid;
6036 u16 handle = le16_to_cpu(element->VolDevHandle);
6037 int rc;
6038
635374e7
EM
6039 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6040 if (!wwid) {
6041 printk(MPT2SAS_ERR_FMT
6042 "failure at %s:%d/%s()!\n", ioc->name,
6043 __FILE__, __LINE__, __func__);
6044 return;
6045 }
6046
6047 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6048 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6049 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6050
6051 if (raid_device)
6052 return;
6053
6054 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6055 if (!raid_device) {
6056 printk(MPT2SAS_ERR_FMT
6057 "failure at %s:%d/%s()!\n", ioc->name,
6058 __FILE__, __LINE__, __func__);
6059 return;
6060 }
6061
6062 raid_device->id = ioc->sas_id++;
6063 raid_device->channel = RAID_CHANNEL;
6064 raid_device->handle = handle;
6065 raid_device->wwid = wwid;
6066 _scsih_raid_device_add(ioc, raid_device);
921cd802 6067 if (!ioc->wait_for_discovery_to_complete) {
635374e7
EM
6068 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6069 raid_device->id, 0);
6070 if (rc)
6071 _scsih_raid_device_remove(ioc, raid_device);
09da0b32 6072 } else {
6073 spin_lock_irqsave(&ioc->raid_device_lock, flags);
635374e7 6074 _scsih_determine_boot_device(ioc, raid_device, 1);
09da0b32 6075 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6076 }
635374e7
EM
6077}
6078
6079/**
6080 * _scsih_sas_volume_delete - delete volume
6081 * @ioc: per adapter object
f3eedd69 6082 * @handle: volume device handle
635374e7
EM
6083 * Context: user.
6084 *
6085 * Return nothing.
6086 */
6087static void
f3eedd69 6088_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
635374e7
EM
6089{
6090 struct _raid_device *raid_device;
635374e7
EM
6091 unsigned long flags;
6092 struct MPT2SAS_TARGET *sas_target_priv_data;
09da0b32 6093 struct scsi_target *starget = NULL;
635374e7 6094
635374e7
EM
6095 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6096 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
09da0b32 6097 if (raid_device) {
6098 if (raid_device->starget) {
6099 starget = raid_device->starget;
6100 sas_target_priv_data = starget->hostdata;
6101 sas_target_priv_data->deleted = 1;
6102 }
6103 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6104 "(0x%016llx)\n", ioc->name, raid_device->handle,
6105 (unsigned long long) raid_device->wwid);
6106 list_del(&raid_device->list);
6107 kfree(raid_device);
635374e7 6108 }
09da0b32 6109 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6110 if (starget)
6111 scsi_remove_target(&starget->dev);
635374e7
EM
6112}
6113
6114/**
6115 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6116 * @ioc: per adapter object
6117 * @element: IR config element data
6118 * Context: user.
6119 *
6120 * Return nothing.
6121 */
6122static void
6123_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6124 Mpi2EventIrConfigElement_t *element)
6125{
6126 struct _sas_device *sas_device;
09da0b32 6127 struct scsi_target *starget = NULL;
6128 struct MPT2SAS_TARGET *sas_target_priv_data;
635374e7
EM
6129 unsigned long flags;
6130 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6131
6132 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6133 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
09da0b32 6134 if (sas_device) {
6135 sas_device->volume_handle = 0;
6136 sas_device->volume_wwid = 0;
6137 clear_bit(handle, ioc->pd_handles);
6138 if (sas_device->starget && sas_device->starget->hostdata) {
6139 starget = sas_device->starget;
6140 sas_target_priv_data = starget->hostdata;
6141 sas_target_priv_data->flags &=
6142 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6143 }
6144 }
635374e7
EM
6145 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6146 if (!sas_device)
6147 return;
6148
6149 /* exposing raid component */
09da0b32 6150 if (starget)
6151 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
635374e7
EM
6152}
6153
6154/**
6155 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6156 * @ioc: per adapter object
6157 * @element: IR config element data
6158 * Context: user.
6159 *
6160 * Return nothing.
6161 */
6162static void
6163_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6164 Mpi2EventIrConfigElement_t *element)
6165{
6166 struct _sas_device *sas_device;
09da0b32 6167 struct scsi_target *starget = NULL;
6168 struct MPT2SAS_TARGET *sas_target_priv_data;
635374e7
EM
6169 unsigned long flags;
6170 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
09da0b32 6171 u16 volume_handle = 0;
6172 u64 volume_wwid = 0;
6173
6174 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6175 if (volume_handle)
6176 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6177 &volume_wwid);
635374e7
EM
6178
6179 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6180 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
09da0b32 6181 if (sas_device) {
6182 set_bit(handle, ioc->pd_handles);
6183 if (sas_device->starget && sas_device->starget->hostdata) {
6184 starget = sas_device->starget;
6185 sas_target_priv_data = starget->hostdata;
6186 sas_target_priv_data->flags |=
6187 MPT_TARGET_FLAGS_RAID_COMPONENT;
6188 sas_device->volume_handle = volume_handle;
6189 sas_device->volume_wwid = volume_wwid;
6190 }
6191 }
635374e7
EM
6192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6193 if (!sas_device)
6194 return;
6195
6196 /* hiding raid component */
09da0b32 6197 if (starget)
6198 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
635374e7
EM
6199}
6200
6201/**
6202 * _scsih_sas_pd_delete - delete pd component
6203 * @ioc: per adapter object
6204 * @element: IR config element data
6205 * Context: user.
6206 *
6207 * Return nothing.
6208 */
6209static void
6210_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6211 Mpi2EventIrConfigElement_t *element)
6212{
635374e7
EM
6213 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6214
09da0b32 6215 _scsih_device_remove_by_handle(ioc, handle);
635374e7
EM
6216}
6217
6218/**
6219 * _scsih_sas_pd_add - remove pd component
6220 * @ioc: per adapter object
6221 * @element: IR config element data
6222 * Context: user.
6223 *
6224 * Return nothing.
6225 */
6226static void
6227_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6228 Mpi2EventIrConfigElement_t *element)
6229{
6230 struct _sas_device *sas_device;
6231 unsigned long flags;
6232 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
62727a7b
KD
6233 Mpi2ConfigReply_t mpi_reply;
6234 Mpi2SasDevicePage0_t sas_device_pg0;
6235 u32 ioc_status;
c5e039be
KD
6236 u64 sas_address;
6237 u16 parent_handle;
635374e7 6238
f3eedd69
KD
6239 set_bit(handle, ioc->pd_handles);
6240
635374e7
EM
6241 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6242 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6243 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
f3eedd69 6244 if (sas_device)
62727a7b 6245 return;
62727a7b
KD
6246
6247 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6248 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6249 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6250 ioc->name, __FILE__, __LINE__, __func__);
6251 return;
6252 }
6253
6254 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6255 MPI2_IOCSTATUS_MASK;
6256 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6257 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6258 ioc->name, __FILE__, __LINE__, __func__);
6259 return;
6260 }
6261
c5e039be
KD
6262 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6263 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6264 mpt2sas_transport_update_links(ioc, sas_address, handle,
6265 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
62727a7b
KD
6266
6267 _scsih_add_device(ioc, handle, 0, 1);
635374e7
EM
6268}
6269
6270#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6271/**
6272 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6273 * @ioc: per adapter object
6274 * @event_data: event data payload
6275 * Context: user.
6276 *
6277 * Return nothing.
6278 */
6279static void
6280_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6281 Mpi2EventDataIrConfigChangeList_t *event_data)
6282{
6283 Mpi2EventIrConfigElement_t *element;
6284 u8 element_type;
6285 int i;
6286 char *reason_str = NULL, *element_str = NULL;
6287
6288 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6289
eabb08ad 6290 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
635374e7
EM
6291 ioc->name, (le32_to_cpu(event_data->Flags) &
6292 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6293 "foreign" : "native", event_data->NumElements);
6294 for (i = 0; i < event_data->NumElements; i++, element++) {
6295 switch (element->ReasonCode) {
6296 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6297 reason_str = "add";
6298 break;
6299 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6300 reason_str = "remove";
6301 break;
6302 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6303 reason_str = "no change";
6304 break;
6305 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6306 reason_str = "hide";
6307 break;
6308 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6309 reason_str = "unhide";
6310 break;
6311 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6312 reason_str = "volume_created";
6313 break;
6314 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6315 reason_str = "volume_deleted";
6316 break;
6317 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6318 reason_str = "pd_created";
6319 break;
6320 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6321 reason_str = "pd_deleted";
6322 break;
6323 default:
6324 reason_str = "unknown reason";
6325 break;
6326 }
6327 element_type = le16_to_cpu(element->ElementFlags) &
6328 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6329 switch (element_type) {
6330 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6331 element_str = "volume";
6332 break;
6333 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6334 element_str = "phys disk";
6335 break;
6336 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6337 element_str = "hot spare";
6338 break;
6339 default:
6340 element_str = "unknown element";
6341 break;
6342 }
eabb08ad 6343 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
635374e7
EM
6344 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6345 reason_str, le16_to_cpu(element->VolDevHandle),
6346 le16_to_cpu(element->PhysDiskDevHandle),
6347 element->PhysDiskNum);
6348 }
6349}
6350#endif
6351
6352/**
6353 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6354 * @ioc: per adapter object
7b936b02 6355 * @fw_event: The fw_event_work object
635374e7
EM
6356 * Context: user.
6357 *
6358 * Return nothing.
6359 */
6360static void
7b936b02
KD
6361_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6362 struct fw_event_work *fw_event)
635374e7
EM
6363{
6364 Mpi2EventIrConfigElement_t *element;
6365 int i;
62727a7b 6366 u8 foreign_config;
00713ad7
JL
6367 Mpi2EventDataIrConfigChangeList_t *event_data =
6368 (Mpi2EventDataIrConfigChangeList_t *)
6369 fw_event->event_data;
635374e7
EM
6370
6371#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
0bdccdb0
KD
6372 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6373 && !ioc->hide_ir_msg)
635374e7
EM
6374 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6375
6376#endif
921cd802 6377
6378 if (ioc->shost_recovery)
6379 return;
6380
62727a7b
KD
6381 foreign_config = (le32_to_cpu(event_data->Flags) &
6382 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
635374e7
EM
6383
6384 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6385 for (i = 0; i < event_data->NumElements; i++, element++) {
6386
6387 switch (element->ReasonCode) {
6388 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6389 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
62727a7b
KD
6390 if (!foreign_config)
6391 _scsih_sas_volume_add(ioc, element);
635374e7
EM
6392 break;
6393 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6394 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
62727a7b 6395 if (!foreign_config)
f3eedd69
KD
6396 _scsih_sas_volume_delete(ioc,
6397 le16_to_cpu(element->VolDevHandle));
635374e7
EM
6398 break;
6399 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
0bdccdb0
KD
6400 if (!ioc->is_warpdrive)
6401 _scsih_sas_pd_hide(ioc, element);
635374e7
EM
6402 break;
6403 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
0bdccdb0
KD
6404 if (!ioc->is_warpdrive)
6405 _scsih_sas_pd_expose(ioc, element);
635374e7
EM
6406 break;
6407 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
0bdccdb0
KD
6408 if (!ioc->is_warpdrive)
6409 _scsih_sas_pd_add(ioc, element);
635374e7
EM
6410 break;
6411 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
0bdccdb0
KD
6412 if (!ioc->is_warpdrive)
6413 _scsih_sas_pd_delete(ioc, element);
635374e7
EM
6414 break;
6415 }
6416 }
6417}
6418
6419/**
6420 * _scsih_sas_ir_volume_event - IR volume event
6421 * @ioc: per adapter object
7b936b02 6422 * @fw_event: The fw_event_work object
635374e7
EM
6423 * Context: user.
6424 *
6425 * Return nothing.
6426 */
6427static void
7b936b02
KD
6428_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6429 struct fw_event_work *fw_event)
635374e7
EM
6430{
6431 u64 wwid;
6432 unsigned long flags;
6433 struct _raid_device *raid_device;
6434 u16 handle;
6435 u32 state;
6436 int rc;
00713ad7
JL
6437 Mpi2EventDataIrVolume_t *event_data =
6438 (Mpi2EventDataIrVolume_t *)
6439 fw_event->event_data;
635374e7 6440
921cd802 6441 if (ioc->shost_recovery)
6442 return;
6443
635374e7
EM
6444 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6445 return;
6446
6447 handle = le16_to_cpu(event_data->VolDevHandle);
6448 state = le32_to_cpu(event_data->NewValue);
0bdccdb0
KD
6449 if (!ioc->hide_ir_msg)
6450 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6451 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6452 le32_to_cpu(event_data->PreviousValue), state));
635374e7 6453
635374e7
EM
6454 switch (state) {
6455 case MPI2_RAID_VOL_STATE_MISSING:
6456 case MPI2_RAID_VOL_STATE_FAILED:
f3eedd69 6457 _scsih_sas_volume_delete(ioc, handle);
635374e7
EM
6458 break;
6459
6460 case MPI2_RAID_VOL_STATE_ONLINE:
6461 case MPI2_RAID_VOL_STATE_DEGRADED:
6462 case MPI2_RAID_VOL_STATE_OPTIMAL:
f3eedd69
KD
6463
6464 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6465 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6466 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6467
635374e7
EM
6468 if (raid_device)
6469 break;
6470
6471 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6472 if (!wwid) {
6473 printk(MPT2SAS_ERR_FMT
6474 "failure at %s:%d/%s()!\n", ioc->name,
6475 __FILE__, __LINE__, __func__);
6476 break;
6477 }
6478
6479 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6480 if (!raid_device) {
6481 printk(MPT2SAS_ERR_FMT
6482 "failure at %s:%d/%s()!\n", ioc->name,
6483 __FILE__, __LINE__, __func__);
6484 break;
6485 }
6486
6487 raid_device->id = ioc->sas_id++;
6488 raid_device->channel = RAID_CHANNEL;
6489 raid_device->handle = handle;
6490 raid_device->wwid = wwid;
6491 _scsih_raid_device_add(ioc, raid_device);
6492 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6493 raid_device->id, 0);
6494 if (rc)
6495 _scsih_raid_device_remove(ioc, raid_device);
6496 break;
6497
6498 case MPI2_RAID_VOL_STATE_INITIALIZING:
6499 default:
6500 break;
6501 }
6502}
6503
6504/**
6505 * _scsih_sas_ir_physical_disk_event - PD event
6506 * @ioc: per adapter object
7b936b02 6507 * @fw_event: The fw_event_work object
635374e7
EM
6508 * Context: user.
6509 *
6510 * Return nothing.
6511 */
6512static void
7b936b02
KD
6513_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6514 struct fw_event_work *fw_event)
635374e7 6515{
c5e039be 6516 u16 handle, parent_handle;
635374e7
EM
6517 u32 state;
6518 struct _sas_device *sas_device;
6519 unsigned long flags;
62727a7b
KD
6520 Mpi2ConfigReply_t mpi_reply;
6521 Mpi2SasDevicePage0_t sas_device_pg0;
6522 u32 ioc_status;
00713ad7
JL
6523 Mpi2EventDataIrPhysicalDisk_t *event_data =
6524 (Mpi2EventDataIrPhysicalDisk_t *)
6525 fw_event->event_data;
c5e039be 6526 u64 sas_address;
635374e7 6527
921cd802 6528 if (ioc->shost_recovery)
6529 return;
6530
635374e7
EM
6531 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6532 return;
6533
6534 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6535 state = le32_to_cpu(event_data->NewValue);
6536
0bdccdb0
KD
6537 if (!ioc->hide_ir_msg)
6538 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6539 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6540 le32_to_cpu(event_data->PreviousValue), state));
635374e7 6541
635374e7 6542 switch (state) {
635374e7
EM
6543 case MPI2_RAID_PD_STATE_ONLINE:
6544 case MPI2_RAID_PD_STATE_DEGRADED:
6545 case MPI2_RAID_PD_STATE_REBUILDING:
6546 case MPI2_RAID_PD_STATE_OPTIMAL:
f3eedd69
KD
6547 case MPI2_RAID_PD_STATE_HOT_SPARE:
6548
0bdccdb0
KD
6549 if (!ioc->is_warpdrive)
6550 set_bit(handle, ioc->pd_handles);
f3eedd69
KD
6551
6552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6553 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6555
6556 if (sas_device)
62727a7b 6557 return;
62727a7b
KD
6558
6559 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6560 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6561 handle))) {
6562 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6563 ioc->name, __FILE__, __LINE__, __func__);
6564 return;
6565 }
6566
6567 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6568 MPI2_IOCSTATUS_MASK;
6569 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6570 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6571 ioc->name, __FILE__, __LINE__, __func__);
6572 return;
6573 }
6574
c5e039be
KD
6575 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6576 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6577 mpt2sas_transport_update_links(ioc, sas_address, handle,
6578 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
62727a7b
KD
6579
6580 _scsih_add_device(ioc, handle, 0, 1);
6581
635374e7
EM
6582 break;
6583
62727a7b 6584 case MPI2_RAID_PD_STATE_OFFLINE:
635374e7
EM
6585 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6586 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
635374e7
EM
6587 default:
6588 break;
6589 }
6590}
6591
6592#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6593/**
6594 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6595 * @ioc: per adapter object
6596 * @event_data: event data payload
6597 * Context: user.
6598 *
6599 * Return nothing.
6600 */
6601static void
6602_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6603 Mpi2EventDataIrOperationStatus_t *event_data)
6604{
6605 char *reason_str = NULL;
6606
6607 switch (event_data->RAIDOperation) {
6608 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6609 reason_str = "resync";
6610 break;
6611 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6612 reason_str = "online capacity expansion";
6613 break;
6614 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6615 reason_str = "consistency check";
6616 break;
ec6c2b43
KD
6617 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6618 reason_str = "background init";
6619 break;
6620 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6621 reason_str = "make data consistent";
635374e7
EM
6622 break;
6623 }
6624
ec6c2b43
KD
6625 if (!reason_str)
6626 return;
6627
635374e7
EM
6628 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6629 "\thandle(0x%04x), percent complete(%d)\n",
6630 ioc->name, reason_str,
6631 le16_to_cpu(event_data->VolDevHandle),
6632 event_data->PercentComplete);
6633}
6634#endif
6635
6636/**
6637 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6638 * @ioc: per adapter object
7b936b02 6639 * @fw_event: The fw_event_work object
635374e7
EM
6640 * Context: user.
6641 *
6642 * Return nothing.
6643 */
6644static void
7b936b02
KD
6645_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6646 struct fw_event_work *fw_event)
635374e7 6647{
00713ad7
JL
6648 Mpi2EventDataIrOperationStatus_t *event_data =
6649 (Mpi2EventDataIrOperationStatus_t *)
6650 fw_event->event_data;
f7c95ef0
KD
6651 static struct _raid_device *raid_device;
6652 unsigned long flags;
6653 u16 handle;
6654
635374e7 6655#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
0bdccdb0
KD
6656 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6657 && !ioc->hide_ir_msg)
7b936b02 6658 _scsih_sas_ir_operation_status_event_debug(ioc,
f7c95ef0 6659 event_data);
635374e7 6660#endif
f7c95ef0
KD
6661
6662 /* code added for raid transport support */
6663 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6664
f7c95ef0 6665 spin_lock_irqsave(&ioc->raid_device_lock, flags);
09da0b32 6666 handle = le16_to_cpu(event_data->VolDevHandle);
f7c95ef0 6667 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
09da0b32 6668 if (raid_device)
f7c95ef0
KD
6669 raid_device->percent_complete =
6670 event_data->PercentComplete;
09da0b32 6671 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
f7c95ef0 6672 }
635374e7
EM
6673}
6674
14695853
KD
6675/**
6676 * _scsih_prep_device_scan - initialize parameters prior to device scan
6677 * @ioc: per adapter object
6678 *
6679 * Set the deleted flag prior to device scan. If the device is found during
6680 * the scan, then we clear the deleted flag.
6681 */
6682static void
6683_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6684{
6685 struct MPT2SAS_DEVICE *sas_device_priv_data;
6686 struct scsi_device *sdev;
6687
6688 shost_for_each_device(sdev, ioc->shost) {
6689 sas_device_priv_data = sdev->hostdata;
6690 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6691 sas_device_priv_data->sas_target->deleted = 1;
6692 }
6693}
6694
635374e7
EM
6695/**
6696 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6697 * @ioc: per adapter object
6698 * @sas_address: sas address
6699 * @slot: enclosure slot id
6700 * @handle: device handle
6701 *
6702 * After host reset, find out whether devices are still responding.
6703 * Used in _scsi_remove_unresponsive_sas_devices.
6704 *
6705 * Return nothing.
6706 */
6707static void
6708_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6709 u16 slot, u16 handle)
6710{
0bdccdb0 6711 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
635374e7
EM
6712 struct scsi_target *starget;
6713 struct _sas_device *sas_device;
6714 unsigned long flags;
6715
6716 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6717 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6718 if (sas_device->sas_address == sas_address &&
0bdccdb0 6719 sas_device->slot == slot) {
635374e7 6720 sas_device->responding = 1;
77e63ed4 6721 starget = sas_device->starget;
14695853
KD
6722 if (starget && starget->hostdata) {
6723 sas_target_priv_data = starget->hostdata;
6724 sas_target_priv_data->tm_busy = 0;
6725 sas_target_priv_data->deleted = 0;
6726 } else
6727 sas_target_priv_data = NULL;
0bdccdb0
KD
6728 if (starget)
6729 starget_printk(KERN_INFO, starget,
6730 "handle(0x%04x), sas_addr(0x%016llx), "
6731 "enclosure logical id(0x%016llx), "
6732 "slot(%d)\n", handle,
6733 (unsigned long long)sas_device->sas_address,
6734 (unsigned long long)
6735 sas_device->enclosure_logical_id,
6736 sas_device->slot);
635374e7
EM
6737 if (sas_device->handle == handle)
6738 goto out;
6739 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6740 sas_device->handle);
6741 sas_device->handle = handle;
14695853
KD
6742 if (sas_target_priv_data)
6743 sas_target_priv_data->handle = handle;
635374e7
EM
6744 goto out;
6745 }
6746 }
6747 out:
6748 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6749}
6750
6751/**
6752 * _scsih_search_responding_sas_devices -
6753 * @ioc: per adapter object
6754 *
6755 * After host reset, find out whether devices are still responding.
6756 * If not remove.
6757 *
6758 * Return nothing.
6759 */
6760static void
6761_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6762{
6763 Mpi2SasDevicePage0_t sas_device_pg0;
6764 Mpi2ConfigReply_t mpi_reply;
6765 u16 ioc_status;
6766 __le64 sas_address;
6767 u16 handle;
6768 u32 device_info;
6769 u16 slot;
6770
921cd802 6771 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
635374e7
EM
6772
6773 if (list_empty(&ioc->sas_device_list))
921cd802 6774 goto out;
635374e7
EM
6775
6776 handle = 0xFFFF;
6777 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6778 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6779 handle))) {
6780 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6781 MPI2_IOCSTATUS_MASK;
1c50e8d2 6782 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
635374e7
EM
6783 break;
6784 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6785 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6786 if (!(_scsih_is_end_device(device_info)))
6787 continue;
6788 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6789 slot = le16_to_cpu(sas_device_pg0.Slot);
6790 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6791 handle);
6792 }
921cd802 6793out:
6794 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6795 ioc->name);
635374e7
EM
6796}
6797
6798/**
6799 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6800 * @ioc: per adapter object
6801 * @wwid: world wide identifier for raid volume
6802 * @handle: device handle
6803 *
6804 * After host reset, find out whether devices are still responding.
6805 * Used in _scsi_remove_unresponsive_raid_devices.
6806 *
6807 * Return nothing.
6808 */
6809static void
6810_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6811 u16 handle)
6812{
6813 struct MPT2SAS_TARGET *sas_target_priv_data;
6814 struct scsi_target *starget;
6815 struct _raid_device *raid_device;
6816 unsigned long flags;
6817
6818 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6819 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6820 if (raid_device->wwid == wwid && raid_device->starget) {
14695853
KD
6821 starget = raid_device->starget;
6822 if (starget && starget->hostdata) {
6823 sas_target_priv_data = starget->hostdata;
6824 sas_target_priv_data->deleted = 0;
6825 } else
6826 sas_target_priv_data = NULL;
635374e7 6827 raid_device->responding = 1;
30c43282 6828 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
635374e7
EM
6829 starget_printk(KERN_INFO, raid_device->starget,
6830 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6831 (unsigned long long)raid_device->wwid);
0bdccdb0
KD
6832 /*
6833 * WARPDRIVE: The handles of the PDs might have changed
6834 * across the host reset so re-initialize the
6835 * required data for Direct IO
6836 */
6837 _scsih_init_warpdrive_properties(ioc, raid_device);
09da0b32 6838 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6839 if (raid_device->handle == handle) {
6840 spin_unlock_irqrestore(&ioc->raid_device_lock,
6841 flags);
30c43282 6842 return;
09da0b32 6843 }
635374e7
EM
6844 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6845 raid_device->handle);
6846 raid_device->handle = handle;
14695853
KD
6847 if (sas_target_priv_data)
6848 sas_target_priv_data->handle = handle;
09da0b32 6849 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
30c43282 6850 return;
635374e7
EM
6851 }
6852 }
30c43282 6853
635374e7
EM
6854 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6855}
6856
6857/**
6858 * _scsih_search_responding_raid_devices -
6859 * @ioc: per adapter object
6860 *
6861 * After host reset, find out whether devices are still responding.
6862 * If not remove.
6863 *
6864 * Return nothing.
6865 */
6866static void
6867_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6868{
6869 Mpi2RaidVolPage1_t volume_pg1;
d417d1c3 6870 Mpi2RaidVolPage0_t volume_pg0;
f3eedd69 6871 Mpi2RaidPhysDiskPage0_t pd_pg0;
635374e7
EM
6872 Mpi2ConfigReply_t mpi_reply;
6873 u16 ioc_status;
6874 u16 handle;
f3eedd69 6875 u8 phys_disk_num;
635374e7 6876
921cd802 6877 if (!ioc->ir_firmware)
6878 return;
6879
6880 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6881 ioc->name);
635374e7
EM
6882
6883 if (list_empty(&ioc->raid_device_list))
921cd802 6884 goto out;
635374e7
EM
6885
6886 handle = 0xFFFF;
6887 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6888 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6889 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6890 MPI2_IOCSTATUS_MASK;
1c50e8d2 6891 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
635374e7
EM
6892 break;
6893 handle = le16_to_cpu(volume_pg1.DevHandle);
d417d1c3
KD
6894
6895 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6896 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6897 sizeof(Mpi2RaidVolPage0_t)))
6898 continue;
6899
6900 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6901 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6902 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6903 _scsih_mark_responding_raid_device(ioc,
6904 le64_to_cpu(volume_pg1.WWID), handle);
635374e7 6905 }
f3eedd69
KD
6906
6907 /* refresh the pd_handles */
0bdccdb0
KD
6908 if (!ioc->is_warpdrive) {
6909 phys_disk_num = 0xFF;
6910 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6911 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6912 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6913 phys_disk_num))) {
6914 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6915 MPI2_IOCSTATUS_MASK;
1c50e8d2 6916 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
0bdccdb0
KD
6917 break;
6918 phys_disk_num = pd_pg0.PhysDiskNum;
6919 handle = le16_to_cpu(pd_pg0.DevHandle);
6920 set_bit(handle, ioc->pd_handles);
6921 }
f3eedd69 6922 }
921cd802 6923out:
6924 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6925 "complete\n", ioc->name);
635374e7
EM
6926}
6927
6928/**
6929 * _scsih_mark_responding_expander - mark a expander as responding
6930 * @ioc: per adapter object
6931 * @sas_address: sas address
6932 * @handle:
6933 *
6934 * After host reset, find out whether devices are still responding.
6935 * Used in _scsi_remove_unresponsive_expanders.
6936 *
6937 * Return nothing.
6938 */
6939static void
6940_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6941 u16 handle)
6942{
6943 struct _sas_node *sas_expander;
6944 unsigned long flags;
c5e039be 6945 int i;
635374e7
EM
6946
6947 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6948 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
c5e039be
KD
6949 if (sas_expander->sas_address != sas_address)
6950 continue;
6951 sas_expander->responding = 1;
6952 if (sas_expander->handle == handle)
635374e7 6953 goto out;
c5e039be
KD
6954 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6955 " from(0x%04x) to (0x%04x)!!!\n",
6956 (unsigned long long)sas_expander->sas_address,
6957 sas_expander->handle, handle);
6958 sas_expander->handle = handle;
6959 for (i = 0 ; i < sas_expander->num_phys ; i++)
6960 sas_expander->phy[i].handle = handle;
6961 goto out;
635374e7
EM
6962 }
6963 out:
6964 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6965}
6966
6967/**
6968 * _scsih_search_responding_expanders -
6969 * @ioc: per adapter object
6970 *
6971 * After host reset, find out whether devices are still responding.
6972 * If not remove.
6973 *
6974 * Return nothing.
6975 */
6976static void
6977_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6978{
6979 Mpi2ExpanderPage0_t expander_pg0;
6980 Mpi2ConfigReply_t mpi_reply;
6981 u16 ioc_status;
c97951ec 6982 u64 sas_address;
635374e7
EM
6983 u16 handle;
6984
921cd802 6985 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
635374e7
EM
6986
6987 if (list_empty(&ioc->sas_expander_list))
921cd802 6988 goto out;
635374e7
EM
6989
6990 handle = 0xFFFF;
6991 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6992 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6993
6994 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6995 MPI2_IOCSTATUS_MASK;
1c50e8d2 6996 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
635374e7
EM
6997 break;
6998
6999 handle = le16_to_cpu(expander_pg0.DevHandle);
7000 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7001 printk(KERN_INFO "\texpander present: handle(0x%04x), "
7002 "sas_addr(0x%016llx)\n", handle,
7003 (unsigned long long)sas_address);
7004 _scsih_mark_responding_expander(ioc, sas_address, handle);
7005 }
7006
921cd802 7007 out:
7008 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
635374e7
EM
7009}
7010
7011/**
f1c35e6a 7012 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
635374e7
EM
7013 * @ioc: per adapter object
7014 *
7015 * Return nothing.
7016 */
7017static void
f1c35e6a 7018_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
635374e7
EM
7019{
7020 struct _sas_device *sas_device, *sas_device_next;
09da0b32 7021 struct _sas_node *sas_expander, *sas_expander_next;
635374e7 7022 struct _raid_device *raid_device, *raid_device_next;
09da0b32 7023 struct list_head tmp_list;
7024 unsigned long flags;
635374e7 7025
921cd802 7026 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7027 ioc->name);
635374e7 7028
09da0b32 7029 /* removing unresponding end devices */
7030 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7031 ioc->name);
635374e7
EM
7032 list_for_each_entry_safe(sas_device, sas_device_next,
7033 &ioc->sas_device_list, list) {
09da0b32 7034 if (!sas_device->responding)
39af7a98 7035 mpt2sas_device_remove_by_sas_address(ioc,
7036 sas_device->sas_address);
09da0b32 7037 else
635374e7 7038 sas_device->responding = 0;
635374e7
EM
7039 }
7040
09da0b32 7041 /* removing unresponding volumes */
7042 if (ioc->ir_firmware) {
7043 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7044 "volumes\n", ioc->name);
7045 list_for_each_entry_safe(raid_device, raid_device_next,
7046 &ioc->raid_device_list, list) {
7047 if (!raid_device->responding)
7048 _scsih_sas_volume_delete(ioc,
7049 raid_device->handle);
7050 else
7051 raid_device->responding = 0;
635374e7 7052 }
635374e7 7053 }
09da0b32 7054 /* removing unresponding expanders */
7055 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7056 ioc->name);
7057 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7058 INIT_LIST_HEAD(&tmp_list);
7059 list_for_each_entry_safe(sas_expander, sas_expander_next,
7060 &ioc->sas_expander_list, list) {
7061 if (!sas_expander->responding)
7062 list_move_tail(&sas_expander->list, &tmp_list);
7063 else
635374e7 7064 sas_expander->responding = 0;
09da0b32 7065 }
7066 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7067 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7068 list) {
7069 list_del(&sas_expander->list);
7070 _scsih_expander_node_remove(ioc, sas_expander);
cd4e12e8 7071 }
921cd802 7072 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7073 ioc->name);
7074 /* unblock devices */
7075 _scsih_ublock_io_all_device(ioc);
7076}
7077
7078static void
7079_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7080 struct _sas_node *sas_expander, u16 handle)
7081{
7082 Mpi2ExpanderPage1_t expander_pg1;
7083 Mpi2ConfigReply_t mpi_reply;
7084 int i;
7085
7086 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7087 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7088 &expander_pg1, i, handle))) {
7089 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7090 ioc->name, __FILE__, __LINE__, __func__);
7091 return;
7092 }
7093
7094 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7095 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7096 expander_pg1.NegotiatedLinkRate >> 4);
7097 }
cd4e12e8
KD
7098}
7099
0bdccdb0 7100/**
921cd802 7101 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
0bdccdb0
KD
7102 * @ioc: per adapter object
7103 *
7104 * Return nothing.
7105 */
7106static void
921cd802 7107_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
0bdccdb0 7108{
921cd802 7109 Mpi2ExpanderPage0_t expander_pg0;
7110 Mpi2SasDevicePage0_t sas_device_pg0;
7111 Mpi2RaidVolPage1_t volume_pg1;
7112 Mpi2RaidVolPage0_t volume_pg0;
7113 Mpi2RaidPhysDiskPage0_t pd_pg0;
7114 Mpi2EventIrConfigElement_t element;
7115 Mpi2ConfigReply_t mpi_reply;
7116 u8 phys_disk_num;
7117 u16 ioc_status;
7118 u16 handle, parent_handle;
7119 u64 sas_address;
7120 struct _sas_device *sas_device;
7121 struct _sas_node *expander_device;
7122 static struct _raid_device *raid_device;
6241f22c 7123 u8 retry_count;
09da0b32 7124 unsigned long flags;
0bdccdb0 7125
921cd802 7126 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
0bdccdb0 7127
921cd802 7128 _scsih_sas_host_refresh(ioc);
7129
6241f22c
SR
7130 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders start\n",
7131 ioc->name);
921cd802 7132 /* expanders */
7133 handle = 0xFFFF;
7134 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7135 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7136 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7137 MPI2_IOCSTATUS_MASK;
6241f22c
SR
7138 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7139 printk(MPT2SAS_INFO_FMT "\tbreak from expander scan: "
7140 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7141 ioc->name, ioc_status,
7142 le32_to_cpu(mpi_reply.IOCLogInfo));
7143 break;
7144 }
921cd802 7145 handle = le16_to_cpu(expander_pg0.DevHandle);
09da0b32 7146 spin_lock_irqsave(&ioc->sas_node_lock, flags);
921cd802 7147 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7148 ioc, le64_to_cpu(expander_pg0.SASAddress));
09da0b32 7149 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
921cd802 7150 if (expander_device)
7151 _scsih_refresh_expander_links(ioc, expander_device,
7152 handle);
6241f22c
SR
7153 else {
7154 printk(MPT2SAS_INFO_FMT "\tBEFORE adding expander: "
7155 "handle (0x%04x), sas_addr(0x%016llx)\n",
7156 ioc->name, handle, (unsigned long long)
7157 le64_to_cpu(expander_pg0.SASAddress));
921cd802 7158 _scsih_expander_add(ioc, handle);
6241f22c
SR
7159 printk(MPT2SAS_INFO_FMT "\tAFTER adding expander: "
7160 "handle (0x%04x), sas_addr(0x%016llx)\n",
7161 ioc->name, handle, (unsigned long long)
7162 le64_to_cpu(expander_pg0.SASAddress));
7163 }
921cd802 7164 }
7165
6241f22c
SR
7166 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders complete\n",
7167 ioc->name);
7168
921cd802 7169 if (!ioc->ir_firmware)
7170 goto skip_to_sas;
7171
6241f22c 7172 printk(MPT2SAS_INFO_FMT "\tscan devices phys disk start\n", ioc->name);
921cd802 7173 /* phys disk */
7174 phys_disk_num = 0xFF;
7175 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7176 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7177 phys_disk_num))) {
7178 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7179 MPI2_IOCSTATUS_MASK;
6241f22c
SR
7180 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7181 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan:"
7182 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7183 ioc->name, ioc_status,
7184 le32_to_cpu(mpi_reply.IOCLogInfo));
7185 break;
7186 }
921cd802 7187 phys_disk_num = pd_pg0.PhysDiskNum;
7188 handle = le16_to_cpu(pd_pg0.DevHandle);
09da0b32 7189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
921cd802 7190 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
09da0b32 7191 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
921cd802 7192 if (sas_device)
7193 continue;
7194 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7195 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7196 handle) != 0)
7197 continue;
6241f22c
SR
7198 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7199 MPI2_IOCSTATUS_MASK;
7200 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7201 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan "
7202 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7203 ioc->name, ioc_status,
7204 le32_to_cpu(mpi_reply.IOCLogInfo));
7205 break;
7206 }
921cd802 7207 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7208 if (!_scsih_get_sas_address(ioc, parent_handle,
7209 &sas_address)) {
6241f22c
SR
7210 printk(MPT2SAS_INFO_FMT "\tBEFORE adding phys disk: "
7211 " handle (0x%04x), sas_addr(0x%016llx)\n",
7212 ioc->name, handle, (unsigned long long)
7213 le64_to_cpu(sas_device_pg0.SASAddress));
921cd802 7214 mpt2sas_transport_update_links(ioc, sas_address,
7215 handle, sas_device_pg0.PhyNum,
7216 MPI2_SAS_NEG_LINK_RATE_1_5);
7217 set_bit(handle, ioc->pd_handles);
6241f22c
SR
7218 retry_count = 0;
7219 /* This will retry adding the end device.
7220 * _scsih_add_device() will decide on retries and
7221 * return "1" when it should be retried
7222 */
7223 while (_scsih_add_device(ioc, handle, retry_count++,
7224 1)) {
7225 ssleep(1);
7226 }
7227 printk(MPT2SAS_INFO_FMT "\tAFTER adding phys disk: "
7228 " handle (0x%04x), sas_addr(0x%016llx)\n",
7229 ioc->name, handle, (unsigned long long)
7230 le64_to_cpu(sas_device_pg0.SASAddress));
0bdccdb0 7231 }
921cd802 7232 }
7233
6241f22c
SR
7234 printk(MPT2SAS_INFO_FMT "\tscan devices: phys disk complete\n",
7235 ioc->name);
7236
7237 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes start\n", ioc->name);
921cd802 7238 /* volumes */
7239 handle = 0xFFFF;
7240 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7241 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7242 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7243 MPI2_IOCSTATUS_MASK;
6241f22c
SR
7244 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7245 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7246 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7247 ioc->name, ioc_status,
7248 le32_to_cpu(mpi_reply.IOCLogInfo));
7249 break;
7250 }
921cd802 7251 handle = le16_to_cpu(volume_pg1.DevHandle);
09da0b32 7252 spin_lock_irqsave(&ioc->raid_device_lock, flags);
921cd802 7253 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7254 le64_to_cpu(volume_pg1.WWID));
09da0b32 7255 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
921cd802 7256 if (raid_device)
7257 continue;
7258 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7259 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7260 sizeof(Mpi2RaidVolPage0_t)))
7261 continue;
6241f22c
SR
7262 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7263 MPI2_IOCSTATUS_MASK;
7264 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7265 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7266 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7267 ioc->name, ioc_status,
7268 le32_to_cpu(mpi_reply.IOCLogInfo));
7269 break;
7270 }
921cd802 7271 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7272 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7273 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7274 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7275 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7276 element.VolDevHandle = volume_pg1.DevHandle;
6241f22c
SR
7277 printk(MPT2SAS_INFO_FMT "\tBEFORE adding volume: "
7278 " handle (0x%04x)\n", ioc->name,
7279 volume_pg1.DevHandle);
921cd802 7280 _scsih_sas_volume_add(ioc, &element);
6241f22c
SR
7281 printk(MPT2SAS_INFO_FMT "\tAFTER adding volume: "
7282 " handle (0x%04x)\n", ioc->name,
7283 volume_pg1.DevHandle);
921cd802 7284 }
7285 }
7286
6241f22c
SR
7287 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes complete\n",
7288 ioc->name);
7289
921cd802 7290 skip_to_sas:
7291
6241f22c
SR
7292 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices start\n",
7293 ioc->name);
921cd802 7294 /* sas devices */
7295 handle = 0xFFFF;
7296 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7297 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7298 handle))) {
7299 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7300 MPI2_IOCSTATUS_MASK;
6241f22c
SR
7301 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7302 printk(MPT2SAS_INFO_FMT "\tbreak from end device scan:"
7303 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7304 ioc->name, ioc_status,
7305 le32_to_cpu(mpi_reply.IOCLogInfo));
7306 break;
7307 }
921cd802 7308 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7309 if (!(_scsih_is_end_device(
7310 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7311 continue;
09da0b32 7312 spin_lock_irqsave(&ioc->sas_device_lock, flags);
921cd802 7313 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7314 le64_to_cpu(sas_device_pg0.SASAddress));
09da0b32 7315 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
921cd802 7316 if (sas_device)
7317 continue;
7318 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7319 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
6241f22c
SR
7320 printk(MPT2SAS_INFO_FMT "\tBEFORE adding end device: "
7321 "handle (0x%04x), sas_addr(0x%016llx)\n",
7322 ioc->name, handle, (unsigned long long)
7323 le64_to_cpu(sas_device_pg0.SASAddress));
921cd802 7324 mpt2sas_transport_update_links(ioc, sas_address, handle,
7325 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6241f22c
SR
7326 retry_count = 0;
7327 /* This will retry adding the end device.
7328 * _scsih_add_device() will decide on retries and
7329 * return "1" when it should be retried
7330 */
7331 while (_scsih_add_device(ioc, handle, retry_count++,
7332 0)) {
7333 ssleep(1);
7334 }
7335 printk(MPT2SAS_INFO_FMT "\tAFTER adding end device: "
7336 "handle (0x%04x), sas_addr(0x%016llx)\n",
7337 ioc->name, handle, (unsigned long long)
7338 le64_to_cpu(sas_device_pg0.SASAddress));
0bdccdb0
KD
7339 }
7340 }
921cd802 7341
6241f22c
SR
7342 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices complete\n",
7343 ioc->name);
7344
921cd802 7345 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
0bdccdb0
KD
7346}
7347
921cd802 7348
cd4e12e8
KD
7349/**
7350 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7351 * @ioc: per adapter object
7352 * @reset_phase: phase
7353 *
7354 * The handler for doing any required cleanup or initialization.
7355 *
7356 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7357 * MPT2_IOC_DONE_RESET
7358 *
7359 * Return nothing.
7360 */
7361void
7362mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7363{
7364 switch (reset_phase) {
7365 case MPT2_IOC_PRE_RESET:
eabb08ad 7366 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
cd4e12e8 7367 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
cd4e12e8
KD
7368 break;
7369 case MPT2_IOC_AFTER_RESET:
eabb08ad 7370 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
cd4e12e8 7371 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
f1c35e6a
KD
7372 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7373 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7374 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7375 complete(&ioc->scsih_cmds.done);
7376 }
cd4e12e8
KD
7377 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7378 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7379 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7380 complete(&ioc->tm_cmds.done);
7381 }
f1c35e6a 7382 _scsih_fw_event_cleanup_queue(ioc);
cd4e12e8
KD
7383 _scsih_flush_running_cmds(ioc);
7384 break;
7385 case MPT2_IOC_DONE_RESET:
eabb08ad 7386 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
cd4e12e8 7387 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
c5e039be 7388 _scsih_sas_host_refresh(ioc);
14695853
KD
7389 _scsih_prep_device_scan(ioc);
7390 _scsih_search_responding_sas_devices(ioc);
7391 _scsih_search_responding_raid_devices(ioc);
7392 _scsih_search_responding_expanders(ioc);
14aa7f7e 7393 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7394 !ioc->sas_hba.num_phys)) {
918134ef 7395 _scsih_prep_device_scan(ioc);
7396 _scsih_search_responding_sas_devices(ioc);
7397 _scsih_search_responding_raid_devices(ioc);
7398 _scsih_search_responding_expanders(ioc);
921cd802 7399 _scsih_error_recovery_delete_devices(ioc);
918134ef 7400 }
cd4e12e8 7401 break;
635374e7
EM
7402 }
7403}
7404
7405/**
7406 * _firmware_event_work - delayed task for processing firmware events
7407 * @ioc: per adapter object
7408 * @work: equal to the fw_event_work object
7409 * Context: user.
7410 *
7411 * Return nothing.
7412 */
7413static void
7414_firmware_event_work(struct work_struct *work)
7415{
7416 struct fw_event_work *fw_event = container_of(work,
f1c35e6a 7417 struct fw_event_work, delayed_work.work);
635374e7
EM
7418 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7419
635374e7 7420 /* the queue is being flushed so ignore this event */
02b77081 7421 if (ioc->remove_host ||
3cb5469a 7422 ioc->pci_error_recovery) {
635374e7
EM
7423 _scsih_fw_event_free(ioc, fw_event);
7424 return;
7425 }
635374e7 7426
921cd802 7427 switch (fw_event->event) {
7428 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
09da0b32 7429 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
921cd802 7430 ssleep(1);
f1c35e6a 7431 _scsih_remove_unresponding_sas_devices(ioc);
921cd802 7432 _scsih_scan_for_devices_after_reset(ioc);
7433 break;
7434 case MPT2SAS_PORT_ENABLE_COMPLETE:
918134ef 7435 ioc->start_scan = 0;
921cd802 7436
b0df96a0
RS
7437 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7438 mpt2sas_base_update_missing_delay(ioc, missing_delay[0],
7439 missing_delay[1]);
921cd802 7440
7441 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7442 "from worker thread\n", ioc->name));
7443 break;
3ace8e05
KD
7444 case MPT2SAS_TURN_ON_FAULT_LED:
7445 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7446 break;
635374e7 7447 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7b936b02 7448 _scsih_sas_topology_change_event(ioc, fw_event);
635374e7
EM
7449 break;
7450 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7b936b02
KD
7451 _scsih_sas_device_status_change_event(ioc,
7452 fw_event);
635374e7
EM
7453 break;
7454 case MPI2_EVENT_SAS_DISCOVERY:
7b936b02
KD
7455 _scsih_sas_discovery_event(ioc,
7456 fw_event);
635374e7
EM
7457 break;
7458 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
a78e21dc 7459 _scsih_sas_broadcast_primitive_event(ioc,
7b936b02 7460 fw_event);
635374e7
EM
7461 break;
7462 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7463 _scsih_sas_enclosure_dev_status_change_event(ioc,
7b936b02 7464 fw_event);
635374e7
EM
7465 break;
7466 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7b936b02 7467 _scsih_sas_ir_config_change_event(ioc, fw_event);
635374e7
EM
7468 break;
7469 case MPI2_EVENT_IR_VOLUME:
7b936b02 7470 _scsih_sas_ir_volume_event(ioc, fw_event);
635374e7
EM
7471 break;
7472 case MPI2_EVENT_IR_PHYSICAL_DISK:
7b936b02 7473 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
635374e7
EM
7474 break;
7475 case MPI2_EVENT_IR_OPERATION_STATUS:
7b936b02 7476 _scsih_sas_ir_operation_status_event(ioc, fw_event);
635374e7 7477 break;
635374e7
EM
7478 }
7479 _scsih_fw_event_free(ioc, fw_event);
7480}
7481
7482/**
7483 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7484 * @ioc: per adapter object
7b936b02 7485 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
7486 * @reply: reply message frame(lower 32bit addr)
7487 * Context: interrupt.
7488 *
7489 * This function merely adds a new work task into ioc->firmware_event_thread.
7490 * The tasks are worked from _firmware_event_work in user context.
7491 *
6409a7d0 7492 * Returns void.
635374e7 7493 */
6409a7d0 7494void
7b936b02
KD
7495mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7496 u32 reply)
635374e7
EM
7497{
7498 struct fw_event_work *fw_event;
7499 Mpi2EventNotificationReply_t *mpi_reply;
635374e7 7500 u16 event;
e94f6747 7501 u16 sz;
635374e7
EM
7502
7503 /* events turned off due to host reset or driver unloading */
3cb5469a 7504 if (ioc->remove_host || ioc->pci_error_recovery)
6409a7d0 7505 return;
635374e7 7506
77e63ed4 7507 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
298c794d 7508
7509 if (unlikely(!mpi_reply)) {
7510 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7511 ioc->name, __FILE__, __LINE__, __func__);
6409a7d0 7512 return;
298c794d 7513 }
7514
635374e7
EM
7515 event = le16_to_cpu(mpi_reply->Event);
7516
7517 switch (event) {
7518 /* handle these */
7519 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7520 {
7521 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7522 (Mpi2EventDataSasBroadcastPrimitive_t *)
7523 mpi_reply->EventData;
7524
7525 if (baen_data->Primitive !=
f93213de 7526 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
6409a7d0 7527 return;
f93213de
KD
7528
7529 if (ioc->broadcast_aen_busy) {
7530 ioc->broadcast_aen_pending++;
6409a7d0 7531 return;
f93213de
KD
7532 } else
7533 ioc->broadcast_aen_busy = 1;
635374e7
EM
7534 break;
7535 }
7536
7537 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7538 _scsih_check_topo_delete_events(ioc,
7539 (Mpi2EventDataSasTopologyChangeList_t *)
7540 mpi_reply->EventData);
7541 break;
f3eedd69
KD
7542 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7543 _scsih_check_ir_config_unhide_events(ioc,
7544 (Mpi2EventDataIrConfigChangeList_t *)
7545 mpi_reply->EventData);
7546 break;
7547 case MPI2_EVENT_IR_VOLUME:
7548 _scsih_check_volume_delete_events(ioc,
7549 (Mpi2EventDataIrVolume_t *)
7550 mpi_reply->EventData);
7551 break;
0bdccdb0
KD
7552 case MPI2_EVENT_LOG_ENTRY_ADDED:
7553 {
7554 Mpi2EventDataLogEntryAdded_t *log_entry;
d838c36c 7555 __le32 *log_code;
0bdccdb0
KD
7556
7557 if (!ioc->is_warpdrive)
7558 break;
7559
7560 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7561 mpi_reply->EventData;
d838c36c 7562 log_code = (__le32 *)log_entry->LogData;
0bdccdb0
KD
7563
7564 if (le16_to_cpu(log_entry->LogEntryQualifier)
7565 != MPT2_WARPDRIVE_LOGENTRY)
7566 break;
7567
7568 switch (le32_to_cpu(*log_code)) {
7569 case MPT2_WARPDRIVE_LC_SSDT:
7570 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7571 "IO Throttling has occurred in the WarpDrive "
7572 "subsystem. Check WarpDrive documentation for "
7573 "additional details.\n", ioc->name);
7574 break;
7575 case MPT2_WARPDRIVE_LC_SSDLW:
7576 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7577 "Program/Erase Cycles for the WarpDrive subsystem "
7578 "in degraded range. Check WarpDrive documentation "
7579 "for additional details.\n", ioc->name);
7580 break;
7581 case MPT2_WARPDRIVE_LC_SSDLF:
7582 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7583 "There are no Program/Erase Cycles for the "
7584 "WarpDrive subsystem. The storage device will be "
7585 "in read-only mode. Check WarpDrive documentation "
7586 "for additional details.\n", ioc->name);
7587 break;
7588 case MPT2_WARPDRIVE_LC_BRMF:
7589 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7590 "The Backup Rail Monitor has failed on the "
7591 "WarpDrive subsystem. Check WarpDrive "
7592 "documentation for additional details.\n",
7593 ioc->name);
7594 break;
7595 }
7596
7597 break;
7598 }
635374e7
EM
7599 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7600 case MPI2_EVENT_IR_OPERATION_STATUS:
7601 case MPI2_EVENT_SAS_DISCOVERY:
7602 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
635374e7 7603 case MPI2_EVENT_IR_PHYSICAL_DISK:
635374e7
EM
7604 break;
7605
7606 default: /* ignore the rest */
6409a7d0 7607 return;
635374e7
EM
7608 }
7609
e94f6747 7610 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
00713ad7
JL
7611 fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
7612 if (!fw_event) {
635374e7
EM
7613 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7614 ioc->name, __FILE__, __LINE__, __func__);
6409a7d0 7615 return;
635374e7
EM
7616 }
7617
00713ad7 7618 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
635374e7 7619 fw_event->ioc = ioc;
7b936b02
KD
7620 fw_event->VF_ID = mpi_reply->VF_ID;
7621 fw_event->VP_ID = mpi_reply->VP_ID;
635374e7
EM
7622 fw_event->event = event;
7623 _scsih_fw_event_add(ioc, fw_event);
6409a7d0 7624 return;
635374e7
EM
7625}
7626
7627/* shost template */
7628static struct scsi_host_template scsih_driver_template = {
7629 .module = THIS_MODULE,
7630 .name = "Fusion MPT SAS Host",
7631 .proc_name = MPT2SAS_DRIVER_NAME,
d5d135b3
EM
7632 .queuecommand = _scsih_qcmd,
7633 .target_alloc = _scsih_target_alloc,
7634 .slave_alloc = _scsih_slave_alloc,
7635 .slave_configure = _scsih_slave_configure,
7636 .target_destroy = _scsih_target_destroy,
7637 .slave_destroy = _scsih_slave_destroy,
921cd802 7638 .scan_finished = _scsih_scan_finished,
7639 .scan_start = _scsih_scan_start,
d5d135b3
EM
7640 .change_queue_depth = _scsih_change_queue_depth,
7641 .change_queue_type = _scsih_change_queue_type,
7642 .eh_abort_handler = _scsih_abort,
7643 .eh_device_reset_handler = _scsih_dev_reset,
7644 .eh_target_reset_handler = _scsih_target_reset,
7645 .eh_host_reset_handler = _scsih_host_reset,
7646 .bios_param = _scsih_bios_param,
635374e7
EM
7647 .can_queue = 1,
7648 .this_id = -1,
7649 .sg_tablesize = MPT2SAS_SG_DEPTH,
9ac49d3a 7650 .max_sectors = 32767,
635374e7
EM
7651 .cmd_per_lun = 7,
7652 .use_clustering = ENABLE_CLUSTERING,
7653 .shost_attrs = mpt2sas_host_attrs,
7654 .sdev_attrs = mpt2sas_dev_attrs,
7655};
7656
7657/**
7658 * _scsih_expander_node_remove - removing expander device from list.
7659 * @ioc: per adapter object
7660 * @sas_expander: the sas_device object
7661 * Context: Calling function should acquire ioc->sas_node_lock.
7662 *
7663 * Removing object and freeing associated memory from the
7664 * ioc->sas_expander_list.
7665 *
7666 * Return nothing.
7667 */
7668static void
7669_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7670 struct _sas_node *sas_expander)
7671{
7f6f794d 7672 struct _sas_port *mpt2sas_port, *next;
635374e7
EM
7673
7674 /* remove sibling ports attached to this expander */
7f6f794d 7675 list_for_each_entry_safe(mpt2sas_port, next,
635374e7 7676 &sas_expander->sas_port_list, port_list) {
7f6f794d
KD
7677 if (ioc->shost_recovery)
7678 return;
635374e7 7679 if (mpt2sas_port->remote_identify.device_type ==
7f6f794d 7680 SAS_END_DEVICE)
09da0b32 7681 mpt2sas_device_remove_by_sas_address(ioc,
7f6f794d
KD
7682 mpt2sas_port->remote_identify.sas_address);
7683 else if (mpt2sas_port->remote_identify.device_type ==
7684 SAS_EDGE_EXPANDER_DEVICE ||
635374e7 7685 mpt2sas_port->remote_identify.device_type ==
7f6f794d
KD
7686 SAS_FANOUT_EXPANDER_DEVICE)
7687 mpt2sas_expander_remove(ioc,
7688 mpt2sas_port->remote_identify.sas_address);
635374e7
EM
7689 }
7690
7691 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
c5e039be 7692 sas_expander->sas_address_parent);
635374e7
EM
7693
7694 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7695 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7696 sas_expander->handle, (unsigned long long)
7697 sas_expander->sas_address);
7698
635374e7
EM
7699 kfree(sas_expander->phy);
7700 kfree(sas_expander);
7701}
7702
744090d3
KD
7703/**
7704 * _scsih_ir_shutdown - IR shutdown notification
7705 * @ioc: per adapter object
7706 *
7707 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7708 * the host system is shutting down.
7709 *
7710 * Return nothing.
7711 */
7712static void
7713_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7714{
7715 Mpi2RaidActionRequest_t *mpi_request;
7716 Mpi2RaidActionReply_t *mpi_reply;
7717 u16 smid;
7718
7719 /* is IR firmware build loaded ? */
7720 if (!ioc->ir_firmware)
7721 return;
7722
744090d3
KD
7723 mutex_lock(&ioc->scsih_cmds.mutex);
7724
7725 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7726 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7727 ioc->name, __func__);
7728 goto out;
7729 }
7730 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7731
7732 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7733 if (!smid) {
7734 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7735 ioc->name, __func__);
7736 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7737 goto out;
7738 }
7739
7740 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7741 ioc->scsih_cmds.smid = smid;
7742 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7743
7744 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7745 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7746
0bdccdb0
KD
7747 if (!ioc->hide_ir_msg)
7748 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
744090d3
KD
7749 init_completion(&ioc->scsih_cmds.done);
7750 mpt2sas_base_put_smid_default(ioc, smid);
7751 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7752
7753 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7754 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7755 ioc->name, __func__);
7756 goto out;
7757 }
7758
7759 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7760 mpi_reply = ioc->scsih_cmds.reply;
7761
0bdccdb0
KD
7762 if (!ioc->hide_ir_msg)
7763 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7764 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7765 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7766 le32_to_cpu(mpi_reply->IOCLogInfo));
744090d3
KD
7767 }
7768
7769 out:
7770 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7771 mutex_unlock(&ioc->scsih_cmds.mutex);
7772}
7773
7774/**
7775 * _scsih_shutdown - routine call during system shutdown
7776 * @pdev: PCI device struct
7777 *
7778 * Return nothing.
7779 */
7780static void
7781_scsih_shutdown(struct pci_dev *pdev)
7782{
7783 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7784 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
f1c35e6a
KD
7785 struct workqueue_struct *wq;
7786 unsigned long flags;
7787
7788 ioc->remove_host = 1;
7789 _scsih_fw_event_cleanup_queue(ioc);
7790
7791 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7792 wq = ioc->firmware_event_thread;
7793 ioc->firmware_event_thread = NULL;
7794 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7795 if (wq)
7796 destroy_workqueue(wq);
744090d3
KD
7797
7798 _scsih_ir_shutdown(ioc);
7799 mpt2sas_base_detach(ioc);
7800}
7801
635374e7 7802/**
d5d135b3 7803 * _scsih_remove - detach and remove add host
635374e7
EM
7804 * @pdev: PCI device struct
7805 *
744090d3 7806 * Routine called when unloading the driver.
635374e7
EM
7807 * Return nothing.
7808 */
6f039790 7809static void
d5d135b3 7810_scsih_remove(struct pci_dev *pdev)
635374e7
EM
7811{
7812 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7813 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7f6f794d 7814 struct _sas_port *mpt2sas_port, *next_port;
d7384b28
KD
7815 struct _raid_device *raid_device, *next;
7816 struct MPT2SAS_TARGET *sas_target_priv_data;
635374e7
EM
7817 struct workqueue_struct *wq;
7818 unsigned long flags;
7819
7820 ioc->remove_host = 1;
f1c35e6a 7821 _scsih_fw_event_cleanup_queue(ioc);
635374e7
EM
7822
7823 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7824 wq = ioc->firmware_event_thread;
7825 ioc->firmware_event_thread = NULL;
7826 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7827 if (wq)
7828 destroy_workqueue(wq);
7829
d7384b28 7830 /* release all the volumes */
3a9c913a 7831 _scsih_ir_shutdown(ioc);
d7384b28
KD
7832 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7833 list) {
7834 if (raid_device->starget) {
7835 sas_target_priv_data =
7836 raid_device->starget->hostdata;
7837 sas_target_priv_data->deleted = 1;
7838 scsi_remove_target(&raid_device->starget->dev);
7839 }
7840 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7841 "(0x%016llx)\n", ioc->name, raid_device->handle,
7842 (unsigned long long) raid_device->wwid);
7843 _scsih_raid_device_remove(ioc, raid_device);
7844 }
7845
635374e7 7846 /* free ports attached to the sas_host */
7f6f794d 7847 list_for_each_entry_safe(mpt2sas_port, next_port,
635374e7
EM
7848 &ioc->sas_hba.sas_port_list, port_list) {
7849 if (mpt2sas_port->remote_identify.device_type ==
7f6f794d 7850 SAS_END_DEVICE)
09da0b32 7851 mpt2sas_device_remove_by_sas_address(ioc,
7f6f794d
KD
7852 mpt2sas_port->remote_identify.sas_address);
7853 else if (mpt2sas_port->remote_identify.device_type ==
7854 SAS_EDGE_EXPANDER_DEVICE ||
7855 mpt2sas_port->remote_identify.device_type ==
7856 SAS_FANOUT_EXPANDER_DEVICE)
7857 mpt2sas_expander_remove(ioc,
635374e7 7858 mpt2sas_port->remote_identify.sas_address);
635374e7
EM
7859 }
7860
7861 /* free phys attached to the sas_host */
7862 if (ioc->sas_hba.num_phys) {
7863 kfree(ioc->sas_hba.phy);
7864 ioc->sas_hba.phy = NULL;
7865 ioc->sas_hba.num_phys = 0;
7866 }
7867
7868 sas_remove_host(shost);
02b77081 7869 scsi_remove_host(shost);
9ae89b02 7870 mpt2sas_base_detach(ioc);
635374e7 7871 list_del(&ioc->list);
635374e7
EM
7872 scsi_host_put(shost);
7873}
7874
7875/**
7876 * _scsih_probe_boot_devices - reports 1st device
7877 * @ioc: per adapter object
7878 *
7879 * If specified in bios page 2, this routine reports the 1st
7880 * device scsi-ml or sas transport for persistent boot device
7881 * purposes. Please refer to function _scsih_determine_boot_device()
7882 */
7883static void
7884_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7885{
7886 u8 is_raid;
7887 void *device;
7888 struct _sas_device *sas_device;
7889 struct _raid_device *raid_device;
c5e039be
KD
7890 u16 handle;
7891 u64 sas_address_parent;
635374e7
EM
7892 u64 sas_address;
7893 unsigned long flags;
7894 int rc;
7895
921cd802 7896 /* no Bios, return immediately */
7897 if (!ioc->bios_pg3.BiosVersion)
7898 return;
7899
635374e7 7900 device = NULL;
921cd802 7901 is_raid = 0;
635374e7
EM
7902 if (ioc->req_boot_device.device) {
7903 device = ioc->req_boot_device.device;
7904 is_raid = ioc->req_boot_device.is_raid;
7905 } else if (ioc->req_alt_boot_device.device) {
7906 device = ioc->req_alt_boot_device.device;
7907 is_raid = ioc->req_alt_boot_device.is_raid;
7908 } else if (ioc->current_boot_device.device) {
7909 device = ioc->current_boot_device.device;
7910 is_raid = ioc->current_boot_device.is_raid;
7911 }
7912
7913 if (!device)
7914 return;
7915
7916 if (is_raid) {
7917 raid_device = device;
7918 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7919 raid_device->id, 0);
7920 if (rc)
7921 _scsih_raid_device_remove(ioc, raid_device);
7922 } else {
09da0b32 7923 spin_lock_irqsave(&ioc->sas_device_lock, flags);
635374e7
EM
7924 sas_device = device;
7925 handle = sas_device->handle;
c5e039be 7926 sas_address_parent = sas_device->sas_address_parent;
635374e7 7927 sas_address = sas_device->sas_address;
635374e7
EM
7928 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7929 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
0bdccdb0
KD
7930
7931 if (ioc->hide_drives)
7932 return;
635374e7 7933 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
c5e039be 7934 sas_device->sas_address_parent)) {
635374e7
EM
7935 _scsih_sas_device_remove(ioc, sas_device);
7936 } else if (!sas_device->starget) {
bc6d4c3b
SR
7937 if (!ioc->is_driver_loading) {
7938 mpt2sas_transport_port_remove(ioc,
7939 sas_address,
35116db9 7940 sas_address_parent);
bc6d4c3b
SR
7941 _scsih_sas_device_remove(ioc, sas_device);
7942 }
635374e7
EM
7943 }
7944 }
7945}
7946
7947/**
7948 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7949 * @ioc: per adapter object
7950 *
7951 * Called during initial loading of the driver.
7952 */
7953static void
7954_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7955{
7956 struct _raid_device *raid_device, *raid_next;
7957 int rc;
7958
7959 list_for_each_entry_safe(raid_device, raid_next,
7960 &ioc->raid_device_list, list) {
7961 if (raid_device->starget)
7962 continue;
7963 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7964 raid_device->id, 0);
7965 if (rc)
7966 _scsih_raid_device_remove(ioc, raid_device);
7967 }
7968}
7969
7970/**
77e63ed4 7971 * _scsih_probe_sas - reporting sas devices to sas transport
635374e7
EM
7972 * @ioc: per adapter object
7973 *
7974 * Called during initial loading of the driver.
7975 */
7976static void
7977_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7978{
7979 struct _sas_device *sas_device, *next;
7980 unsigned long flags;
635374e7
EM
7981
7982 /* SAS Device List */
7983 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7984 list) {
635374e7 7985
0bdccdb0
KD
7986 if (ioc->hide_drives)
7987 continue;
7988
c5e039be
KD
7989 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7990 sas_device->sas_address_parent)) {
0167ac67 7991 list_del(&sas_device->list);
7992 kfree(sas_device);
7993 continue;
635374e7 7994 } else if (!sas_device->starget) {
bc6d4c3b 7995 if (!ioc->is_driver_loading) {
35116db9 7996 mpt2sas_transport_port_remove(ioc,
7997 sas_device->sas_address,
7998 sas_device->sas_address_parent);
bc6d4c3b
SR
7999 list_del(&sas_device->list);
8000 kfree(sas_device);
8001 continue;
8002 }
635374e7 8003 }
0167ac67 8004 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8005 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8006 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
635374e7
EM
8007 }
8008}
8009
8010/**
8011 * _scsih_probe_devices - probing for devices
8012 * @ioc: per adapter object
8013 *
8014 * Called during initial loading of the driver.
8015 */
8016static void
8017_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
8018{
921cd802 8019 u16 volume_mapping_flags;
635374e7
EM
8020
8021 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8022 return; /* return when IOC doesn't support initiator mode */
8023
8024 _scsih_probe_boot_devices(ioc);
8025
8026 if (ioc->ir_firmware) {
921cd802 8027 volume_mapping_flags =
8028 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8029 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8030 if (volume_mapping_flags ==
8031 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
635374e7 8032 _scsih_probe_raid(ioc);
921cd802 8033 _scsih_probe_sas(ioc);
635374e7 8034 } else {
635374e7 8035 _scsih_probe_sas(ioc);
921cd802 8036 _scsih_probe_raid(ioc);
635374e7
EM
8037 }
8038 } else
8039 _scsih_probe_sas(ioc);
8040}
8041
921cd802 8042
8043/**
8044 * _scsih_scan_start - scsi lld callback for .scan_start
8045 * @shost: SCSI host pointer
8046 *
8047 * The shost has the ability to discover targets on its own instead
8048 * of scanning the entire bus. In our implemention, we will kick off
8049 * firmware discovery.
8050 */
8051static void
8052_scsih_scan_start(struct Scsi_Host *shost)
8053{
8054 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8055 int rc;
8056
8057 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8058 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
8059
4b193175 8060 if (disable_discovery > 0)
8061 return;
8062
921cd802 8063 ioc->start_scan = 1;
8064 rc = mpt2sas_port_enable(ioc);
8065
8066 if (rc != 0)
8067 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
8068}
8069
8070/**
8071 * _scsih_scan_finished - scsi lld callback for .scan_finished
8072 * @shost: SCSI host pointer
8073 * @time: elapsed time of the scan in jiffies
8074 *
8075 * This function will be called periodically until it returns 1 with the
8076 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8077 * we wait for firmware discovery to complete, then return 1.
8078 */
8079static int
8080_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8081{
8082 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8083
4b193175 8084 if (disable_discovery > 0) {
8085 ioc->is_driver_loading = 0;
8086 ioc->wait_for_discovery_to_complete = 0;
8087 return 1;
8088 }
8089
921cd802 8090 if (time >= (300 * HZ)) {
8091 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8092 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
8093 "(timeout=300s)\n", ioc->name);
8094 ioc->is_driver_loading = 0;
8095 return 1;
8096 }
8097
8098 if (ioc->start_scan)
8099 return 0;
8100
8101 if (ioc->start_scan_failed) {
8102 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
8103 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
8104 ioc->is_driver_loading = 0;
8105 ioc->wait_for_discovery_to_complete = 0;
8106 ioc->remove_host = 1;
8107 return 1;
8108 }
8109
8110 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
8111 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8112
8113 if (ioc->wait_for_discovery_to_complete) {
8114 ioc->wait_for_discovery_to_complete = 0;
8115 _scsih_probe_devices(ioc);
8116 }
8117 mpt2sas_base_start_watchdog(ioc);
8118 ioc->is_driver_loading = 0;
8119 return 1;
8120}
8121
8122
635374e7 8123/**
d5d135b3 8124 * _scsih_probe - attach and add scsi host
635374e7
EM
8125 * @pdev: PCI device struct
8126 * @id: pci device id
8127 *
8128 * Returns 0 success, anything else error.
8129 */
8130static int
d5d135b3 8131_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
635374e7
EM
8132{
8133 struct MPT2SAS_ADAPTER *ioc;
8134 struct Scsi_Host *shost;
8135
8136 shost = scsi_host_alloc(&scsih_driver_template,
8137 sizeof(struct MPT2SAS_ADAPTER));
8138 if (!shost)
8139 return -ENODEV;
8140
8141 /* init local params */
8142 ioc = shost_priv(shost);
8143 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8144 INIT_LIST_HEAD(&ioc->list);
ba33fadf 8145 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
635374e7
EM
8146 ioc->shost = shost;
8147 ioc->id = mpt_ids++;
8148 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8149 ioc->pdev = pdev;
0bdccdb0
KD
8150 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8151 ioc->is_warpdrive = 1;
8152 ioc->hide_ir_msg = 1;
8153 } else
8154 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
635374e7
EM
8155 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8156 ioc->tm_cb_idx = tm_cb_idx;
8157 ioc->ctl_cb_idx = ctl_cb_idx;
8158 ioc->base_cb_idx = base_cb_idx;
921cd802 8159 ioc->port_enable_cb_idx = port_enable_cb_idx;
635374e7 8160 ioc->transport_cb_idx = transport_cb_idx;
744090d3 8161 ioc->scsih_cb_idx = scsih_cb_idx;
635374e7 8162 ioc->config_cb_idx = config_cb_idx;
77e63ed4 8163 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
f3eedd69 8164 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
77e63ed4 8165 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
635374e7 8166 ioc->logging_level = logging_level;
845a0e40 8167 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
635374e7 8168 /* misc semaphores and spin locks */
d274213a 8169 mutex_init(&ioc->reset_in_progress_mutex);
635374e7
EM
8170 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8171 spin_lock_init(&ioc->scsi_lookup_lock);
8172 spin_lock_init(&ioc->sas_device_lock);
8173 spin_lock_init(&ioc->sas_node_lock);
8174 spin_lock_init(&ioc->fw_event_lock);
8175 spin_lock_init(&ioc->raid_device_lock);
8176
8177 INIT_LIST_HEAD(&ioc->sas_device_list);
8178 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8179 INIT_LIST_HEAD(&ioc->sas_expander_list);
8180 INIT_LIST_HEAD(&ioc->fw_event_list);
8181 INIT_LIST_HEAD(&ioc->raid_device_list);
8182 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
77e63ed4 8183 INIT_LIST_HEAD(&ioc->delayed_tr_list);
f3eedd69 8184 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
3b3e6f8d 8185 INIT_LIST_HEAD(&ioc->reply_queue_list);
635374e7
EM
8186
8187 /* init shost parameters */
d334aa79 8188 shost->max_cmd_len = 32;
635374e7
EM
8189 shost->max_lun = max_lun;
8190 shost->transportt = mpt2sas_transport_template;
8191 shost->unique_id = ioc->id;
8192
a3e1e55e
KD
8193 if (max_sectors != 0xFFFF) {
8194 if (max_sectors < 64) {
8195 shost->max_sectors = 64;
6241f22c
SR
8196 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8197 "for max_sectors, range is 64 to 32767. Assigning "
a3e1e55e 8198 "value of 64.\n", ioc->name, max_sectors);
9ac49d3a 8199 } else if (max_sectors > 32767) {
8200 shost->max_sectors = 32767;
a3e1e55e
KD
8201 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8202 "for max_sectors, range is 64 to 8192. Assigning "
9ac49d3a 8203 "default value of 32767.\n", ioc->name,
a3e1e55e
KD
8204 max_sectors);
8205 } else {
8206 shost->max_sectors = max_sectors & 0xFFFE;
8207 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8208 "set to %d\n", ioc->name, shost->max_sectors);
8209 }
8210 }
8211
5e95e731
MP
8212 /* register EEDP capabilities with SCSI layer */
8213 if (prot_mask)
8214 scsi_host_set_prot(shost, prot_mask);
8215 else
8216 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8217 | SHOST_DIF_TYPE2_PROTECTION
8218 | SHOST_DIF_TYPE3_PROTECTION);
8219
77e63ed4 8220 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
3c621b3e 8221
635374e7
EM
8222 /* event thread */
8223 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8224 "fw_event%d", ioc->id);
8225 ioc->firmware_event_thread = create_singlethread_workqueue(
8226 ioc->firmware_event_name);
8227 if (!ioc->firmware_event_thread) {
8228 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8229 ioc->name, __FILE__, __LINE__, __func__);
8230 goto out_thread_fail;
8231 }
8232
921cd802 8233 ioc->is_driver_loading = 1;
635374e7
EM
8234 if ((mpt2sas_base_attach(ioc))) {
8235 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8236 ioc->name, __FILE__, __LINE__, __func__);
8237 goto out_attach_fail;
8238 }
8239
0bdccdb0
KD
8240 if (ioc->is_warpdrive) {
8241 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8242 ioc->hide_drives = 0;
8243 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8244 ioc->hide_drives = 1;
8245 else {
8246 if (_scsih_get_num_volumes(ioc))
8247 ioc->hide_drives = 1;
8248 else
8249 ioc->hide_drives = 0;
8250 }
8251 } else
8252 ioc->hide_drives = 0;
02b77081
RS
8253
8254 if ((scsi_add_host(shost, &pdev->dev))) {
8255 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8256 ioc->name, __FILE__, __LINE__, __func__);
8257 goto out_add_shost_fail;
8258 }
8259
2cb6fc8c 8260 scsi_scan_host(shost);
0bdccdb0 8261
635374e7
EM
8262 return 0;
8263
02b77081
RS
8264 out_add_shost_fail:
8265 mpt2sas_base_detach(ioc);
635374e7
EM
8266 out_attach_fail:
8267 destroy_workqueue(ioc->firmware_event_thread);
8268 out_thread_fail:
8269 list_del(&ioc->list);
f555e052 8270 scsi_host_put(shost);
635374e7
EM
8271 return -ENODEV;
8272}
8273
8274#ifdef CONFIG_PM
8275/**
d5d135b3 8276 * _scsih_suspend - power management suspend main entry point
635374e7
EM
8277 * @pdev: PCI device struct
8278 * @state: PM state change to (usually PCI_D3)
8279 *
8280 * Returns 0 success, anything else error.
8281 */
8282static int
d5d135b3 8283_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
635374e7
EM
8284{
8285 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8286 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
c97951ec 8287 pci_power_t device_state;
635374e7 8288
e4750c98 8289 mpt2sas_base_stop_watchdog(ioc);
635374e7 8290 scsi_block_requests(shost);
3627dba5 8291 _scsih_ir_shutdown(ioc);
635374e7
EM
8292 device_state = pci_choose_state(pdev, state);
8293 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8294 "operating state [D%d]\n", ioc->name, pdev,
8295 pci_name(pdev), device_state);
8296
8297 mpt2sas_base_free_resources(ioc);
8298 pci_save_state(pdev);
635374e7
EM
8299 pci_set_power_state(pdev, device_state);
8300 return 0;
8301}
8302
8303/**
d5d135b3 8304 * _scsih_resume - power management resume main entry point
635374e7
EM
8305 * @pdev: PCI device struct
8306 *
8307 * Returns 0 success, anything else error.
8308 */
8309static int
d5d135b3 8310_scsih_resume(struct pci_dev *pdev)
635374e7
EM
8311{
8312 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8313 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
c97951ec 8314 pci_power_t device_state = pdev->current_state;
635374e7
EM
8315 int r;
8316
8317 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8318 "operating state [D%d]\n", ioc->name, pdev,
8319 pci_name(pdev), device_state);
8320
8321 pci_set_power_state(pdev, PCI_D0);
8322 pci_enable_wake(pdev, PCI_D0, 0);
8323 pci_restore_state(pdev);
8324 ioc->pdev = pdev;
8325 r = mpt2sas_base_map_resources(ioc);
8326 if (r)
8327 return r;
8328
8329 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8330 scsi_unblock_requests(shost);
e4750c98 8331 mpt2sas_base_start_watchdog(ioc);
635374e7
EM
8332 return 0;
8333}
8334#endif /* CONFIG_PM */
8335
ef7c80c1
KD
8336/**
8337 * _scsih_pci_error_detected - Called when a PCI error is detected.
8338 * @pdev: PCI device struct
8339 * @state: PCI channel state
8340 *
8341 * Description: Called when a PCI error is detected.
8342 *
8343 * Return value:
8344 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8345 */
8346static pci_ers_result_t
8347_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8348{
8349 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8350 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8351
8352 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8353 ioc->name, state);
8354
8355 switch (state) {
8356 case pci_channel_io_normal:
8357 return PCI_ERS_RESULT_CAN_RECOVER;
8358 case pci_channel_io_frozen:
3cb5469a
EM
8359 /* Fatal error, prepare for slot reset */
8360 ioc->pci_error_recovery = 1;
ef7c80c1
KD
8361 scsi_block_requests(ioc->shost);
8362 mpt2sas_base_stop_watchdog(ioc);
8363 mpt2sas_base_free_resources(ioc);
8364 return PCI_ERS_RESULT_NEED_RESET;
8365 case pci_channel_io_perm_failure:
3cb5469a
EM
8366 /* Permanent error, prepare for device removal */
8367 ioc->pci_error_recovery = 1;
8368 mpt2sas_base_stop_watchdog(ioc);
8369 _scsih_flush_running_cmds(ioc);
ef7c80c1
KD
8370 return PCI_ERS_RESULT_DISCONNECT;
8371 }
8372 return PCI_ERS_RESULT_NEED_RESET;
8373}
8374
8375/**
8376 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8377 * @pdev: PCI device struct
8378 *
8379 * Description: This routine is called by the pci error recovery
8380 * code after the PCI slot has been reset, just before we
8381 * should resume normal operations.
8382 */
8383static pci_ers_result_t
8384_scsih_pci_slot_reset(struct pci_dev *pdev)
8385{
8386 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8387 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8388 int rc;
8389
8390 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8391 ioc->name);
8392
3cb5469a 8393 ioc->pci_error_recovery = 0;
ef7c80c1 8394 ioc->pdev = pdev;
3cb5469a 8395 pci_restore_state(pdev);
ef7c80c1
KD
8396 rc = mpt2sas_base_map_resources(ioc);
8397 if (rc)
8398 return PCI_ERS_RESULT_DISCONNECT;
8399
8400
8401 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8402 FORCE_BIG_HAMMER);
8403
8404 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8405 (rc == 0) ? "success" : "failed");
8406
8407 if (!rc)
8408 return PCI_ERS_RESULT_RECOVERED;
8409 else
8410 return PCI_ERS_RESULT_DISCONNECT;
8411}
8412
8413/**
8414 * _scsih_pci_resume() - resume normal ops after PCI reset
8415 * @pdev: pointer to PCI device
8416 *
8417 * Called when the error recovery driver tells us that its
8418 * OK to resume normal operation. Use completion to allow
8419 * halted scsi ops to resume.
8420 */
8421static void
8422_scsih_pci_resume(struct pci_dev *pdev)
8423{
8424 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8425 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8426
8427 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8428
8429 pci_cleanup_aer_uncorrect_error_status(pdev);
8430 mpt2sas_base_start_watchdog(ioc);
8431 scsi_unblock_requests(ioc->shost);
8432}
8433
8434/**
8435 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8436 * @pdev: pointer to PCI device
8437 */
8438static pci_ers_result_t
8439_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8440{
8441 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8442 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8443
8444 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8445 ioc->name);
8446
8447 /* TODO - dump whatever for debugging purposes */
8448
8449 /* Request a slot reset. */
8450 return PCI_ERS_RESULT_NEED_RESET;
8451}
8452
a55b2d21 8453static const struct pci_error_handlers _scsih_err_handler = {
ef7c80c1
KD
8454 .error_detected = _scsih_pci_error_detected,
8455 .mmio_enabled = _scsih_pci_mmio_enabled,
8456 .slot_reset = _scsih_pci_slot_reset,
8457 .resume = _scsih_pci_resume,
8458};
635374e7
EM
8459
8460static struct pci_driver scsih_driver = {
8461 .name = MPT2SAS_DRIVER_NAME,
8462 .id_table = scsih_pci_table,
d5d135b3 8463 .probe = _scsih_probe,
6f039790 8464 .remove = _scsih_remove,
744090d3 8465 .shutdown = _scsih_shutdown,
ef7c80c1 8466 .err_handler = &_scsih_err_handler,
635374e7 8467#ifdef CONFIG_PM
d5d135b3
EM
8468 .suspend = _scsih_suspend,
8469 .resume = _scsih_resume,
635374e7
EM
8470#endif
8471};
8472
f7c95ef0
KD
8473/* raid transport support */
8474static struct raid_function_template mpt2sas_raid_functions = {
8475 .cookie = &scsih_driver_template,
8476 .is_raid = _scsih_is_raid,
8477 .get_resync = _scsih_get_resync,
8478 .get_state = _scsih_get_state,
8479};
635374e7
EM
8480
8481/**
d5d135b3 8482 * _scsih_init - main entry point for this driver.
635374e7
EM
8483 *
8484 * Returns 0 success, anything else error.
8485 */
8486static int __init
d5d135b3 8487_scsih_init(void)
635374e7
EM
8488{
8489 int error;
8490
8491 mpt_ids = 0;
8492 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8493 MPT2SAS_DRIVER_VERSION);
8494
8495 mpt2sas_transport_template =
8496 sas_attach_transport(&mpt2sas_transport_functions);
8497 if (!mpt2sas_transport_template)
8498 return -ENODEV;
f7c95ef0
KD
8499 /* raid transport support */
8500 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8501 if (!mpt2sas_raid_template) {
8502 sas_release_transport(mpt2sas_transport_template);
8503 return -ENODEV;
8504 }
635374e7
EM
8505
8506 mpt2sas_base_initialize_callback_handler();
8507
8508 /* queuecommand callback hander */
d5d135b3 8509 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
635374e7 8510
65155b37 8511 /* task management callback handler */
d5d135b3 8512 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
635374e7
EM
8513
8514 /* base internal commands callback handler */
8515 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
921cd802 8516 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8517 mpt2sas_port_enable_done);
635374e7
EM
8518
8519 /* transport internal commands callback handler */
8520 transport_cb_idx = mpt2sas_base_register_callback_handler(
8521 mpt2sas_transport_done);
8522
744090d3
KD
8523 /* scsih internal commands callback handler */
8524 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8525
635374e7
EM
8526 /* configuration page API internal commands callback handler */
8527 config_cb_idx = mpt2sas_base_register_callback_handler(
8528 mpt2sas_config_done);
8529
8530 /* ctl module callback handler */
8531 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8532
77e63ed4
KD
8533 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8534 _scsih_tm_tr_complete);
f3eedd69
KD
8535
8536 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8537 _scsih_tm_volume_tr_complete);
8538
77e63ed4
KD
8539 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8540 _scsih_sas_control_complete);
8541
635374e7
EM
8542 mpt2sas_ctl_init();
8543
8544 error = pci_register_driver(&scsih_driver);
f7c95ef0
KD
8545 if (error) {
8546 /* raid transport support */
8547 raid_class_release(mpt2sas_raid_template);
635374e7 8548 sas_release_transport(mpt2sas_transport_template);
f7c95ef0 8549 }
635374e7
EM
8550
8551 return error;
8552}
8553
8554/**
d5d135b3 8555 * _scsih_exit - exit point for this driver (when it is a module).
635374e7
EM
8556 *
8557 * Returns 0 success, anything else error.
8558 */
8559static void __exit
d5d135b3 8560_scsih_exit(void)
635374e7
EM
8561{
8562 printk(KERN_INFO "mpt2sas version %s unloading\n",
8563 MPT2SAS_DRIVER_VERSION);
8564
8565 pci_unregister_driver(&scsih_driver);
8566
f7c95ef0
KD
8567 mpt2sas_ctl_exit();
8568
635374e7
EM
8569 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8570 mpt2sas_base_release_callback_handler(tm_cb_idx);
8571 mpt2sas_base_release_callback_handler(base_cb_idx);
921cd802 8572 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
635374e7 8573 mpt2sas_base_release_callback_handler(transport_cb_idx);
744090d3 8574 mpt2sas_base_release_callback_handler(scsih_cb_idx);
635374e7
EM
8575 mpt2sas_base_release_callback_handler(config_cb_idx);
8576 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8577
77e63ed4 8578 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
f3eedd69 8579 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
77e63ed4
KD
8580 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8581
f7c95ef0
KD
8582 /* raid transport support */
8583 raid_class_release(mpt2sas_raid_template);
8584 sas_release_transport(mpt2sas_transport_template);
8585
635374e7
EM
8586}
8587
d5d135b3
EM
8588module_init(_scsih_init);
8589module_exit(_scsih_exit);