blob: 5202de3f3d3fd2ec547c1e9836252e7ac13e9d24 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (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
Eric Moore635374e2009-03-09 01:21:12 -060044#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>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053054#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053055#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060057
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
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053072static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
73
Eric Moore635374e2009-03-09 01:21:12 -060074/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060075LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060076
77/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060078static u8 scsi_io_cb_idx = -1;
79static u8 tm_cb_idx = -1;
80static u8 ctl_cb_idx = -1;
81static u8 base_cb_idx = -1;
82static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053083static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060084static u8 config_cb_idx = -1;
85static int mpt_ids;
86
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053087static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053088static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053089static u8 tm_sas_control_cb_idx = -1;
90
Eric Moore635374e2009-03-09 01:21:12 -060091/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060092static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060093MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
94 "(default=0)");
95
Kashyap, Desaia3e1e552011-06-14 10:56:12 +053096static ushort max_sectors = 0xFFFF;
97module_param(max_sectors, ushort, 0);
98MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 8192 default=8192");
99
Eric Moore635374e2009-03-09 01:21:12 -0600100/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
101#define MPT2SAS_MAX_LUN (16895)
102static int max_lun = MPT2SAS_MAX_LUN;
103module_param(max_lun, int, 0);
104MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
105
106/**
107 * struct sense_info - common structure for obtaining sense keys
108 * @skey: sense key
109 * @asc: additional sense code
110 * @ascq: additional sense code qualifier
111 */
112struct sense_info {
113 u8 skey;
114 u8 asc;
115 u8 ascq;
116};
117
118
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530119#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530120#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
121
Eric Moore635374e2009-03-09 01:21:12 -0600122/**
123 * struct fw_event_work - firmware event struct
124 * @list: link list framework
125 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530126 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600127 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530128 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600129 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530130 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600131 * @ignore: flag meaning this event has been marked to ignore
132 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
133 * @event_data: reply event data payload follows
134 *
135 * This object stored on ioc->fw_event_list.
136 */
137struct fw_event_work {
138 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530139 u8 cancel_pending_work;
140 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600141 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530142 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600143 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530144 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600145 u8 ignore;
146 u16 event;
147 void *event_data;
148};
149
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530150/* raid transport support */
151static struct raid_template *mpt2sas_raid_template;
152
Eric Moore635374e2009-03-09 01:21:12 -0600153/**
154 * struct _scsi_io_transfer - scsi io transfer
155 * @handle: sas device handle (assigned by firmware)
156 * @is_raid: flag set for hidden raid components
157 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
158 * @data_length: data transfer length
159 * @data_dma: dma pointer to data
160 * @sense: sense data
161 * @lun: lun number
162 * @cdb_length: cdb length
163 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600164 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530165 * @VF_ID: virtual function id
166 * @VP_ID: virtual port id
167 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600168 * @sense_length: sense length
169 * @ioc_status: ioc status
170 * @scsi_state: scsi state
171 * @scsi_status: scsi staus
172 * @log_info: log information
173 * @transfer_length: data length transfer when there is a reply message
174 *
175 * Used for sending internal scsi commands to devices within this module.
176 * Refer to _scsi_send_scsi_io().
177 */
178struct _scsi_io_transfer {
179 u16 handle;
180 u8 is_raid;
181 enum dma_data_direction dir;
182 u32 data_length;
183 dma_addr_t data_dma;
184 u8 sense[SCSI_SENSE_BUFFERSIZE];
185 u32 lun;
186 u8 cdb_length;
187 u8 cdb[32];
188 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530189 u8 VF_ID;
190 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600191 u8 valid_reply;
192 /* the following bits are only valid when 'valid_reply = 1' */
193 u32 sense_length;
194 u16 ioc_status;
195 u8 scsi_state;
196 u8 scsi_status;
197 u32 log_info;
198 u32 transfer_length;
199};
200
201/*
202 * The pci device ids are defined in mpi/mpi2_cnfg.h.
203 */
204static struct pci_device_id scsih_pci_table[] = {
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
206 PCI_ANY_ID, PCI_ANY_ID },
207 /* Falcon ~ 2008*/
208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
209 PCI_ANY_ID, PCI_ANY_ID },
210 /* Liberator ~ 2108 */
211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
212 PCI_ANY_ID, PCI_ANY_ID },
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
216 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530217 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600218 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
219 PCI_ANY_ID, PCI_ANY_ID },
220 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
221 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530222 /* Thunderbolt ~ 2208 */
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
228 PCI_ANY_ID, PCI_ANY_ID },
229 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
230 PCI_ANY_ID, PCI_ANY_ID },
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
232 PCI_ANY_ID, PCI_ANY_ID },
233 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
234 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530235 /* Mustang ~ 2308 */
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530237 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530238 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
239 PCI_ANY_ID, PCI_ANY_ID },
240 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530241 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530242 /* SSS6200 */
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
244 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600245 {0} /* Terminating entry */
246};
247MODULE_DEVICE_TABLE(pci, scsih_pci_table);
248
249/**
Eric Moored5d135b2009-05-18 13:02:08 -0600250 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600251 *
252 * Note: The logging levels are defined in mpt2sas_debug.h.
253 */
254static int
Eric Moored5d135b2009-05-18 13:02:08 -0600255_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600256{
257 int ret = param_set_int(val, kp);
258 struct MPT2SAS_ADAPTER *ioc;
259
260 if (ret)
261 return ret;
262
263 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600264 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600265 ioc->logging_level = logging_level;
266 return 0;
267}
Eric Moored5d135b2009-05-18 13:02:08 -0600268module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600269 &logging_level, 0644);
270
271/**
272 * _scsih_srch_boot_sas_address - search based on sas_address
273 * @sas_address: sas address
274 * @boot_device: boot device object from bios page 2
275 *
276 * Returns 1 when there's a match, 0 means no match.
277 */
278static inline int
279_scsih_srch_boot_sas_address(u64 sas_address,
280 Mpi2BootDeviceSasWwid_t *boot_device)
281{
282 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
283}
284
285/**
286 * _scsih_srch_boot_device_name - search based on device name
287 * @device_name: device name specified in INDENTIFY fram
288 * @boot_device: boot device object from bios page 2
289 *
290 * Returns 1 when there's a match, 0 means no match.
291 */
292static inline int
293_scsih_srch_boot_device_name(u64 device_name,
294 Mpi2BootDeviceDeviceName_t *boot_device)
295{
296 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
297}
298
299/**
300 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
301 * @enclosure_logical_id: enclosure logical id
302 * @slot_number: slot number
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_encl_slot(u64 enclosure_logical_id, u16 slot_number,
309 Mpi2BootDeviceEnclosureSlot_t *boot_device)
310{
311 return (enclosure_logical_id == le64_to_cpu(boot_device->
312 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
313 SlotNumber)) ? 1 : 0;
314}
315
316/**
317 * _scsih_is_boot_device - search for matching boot device.
318 * @sas_address: sas address
319 * @device_name: device name specified in INDENTIFY fram
320 * @enclosure_logical_id: enclosure logical id
321 * @slot_number: slot number
322 * @form: specifies boot device form
323 * @boot_device: boot device object from bios page 2
324 *
325 * Returns 1 when there's a match, 0 means no match.
326 */
327static int
328_scsih_is_boot_device(u64 sas_address, u64 device_name,
329 u64 enclosure_logical_id, u16 slot, u8 form,
330 Mpi2BiosPage2BootDevice_t *boot_device)
331{
332 int rc = 0;
333
334 switch (form) {
335 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
336 if (!sas_address)
337 break;
338 rc = _scsih_srch_boot_sas_address(
339 sas_address, &boot_device->SasWwid);
340 break;
341 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
342 if (!enclosure_logical_id)
343 break;
344 rc = _scsih_srch_boot_encl_slot(
345 enclosure_logical_id,
346 slot, &boot_device->EnclosureSlot);
347 break;
348 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
349 if (!device_name)
350 break;
351 rc = _scsih_srch_boot_device_name(
352 device_name, &boot_device->DeviceName);
353 break;
354 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
355 break;
356 }
357
358 return rc;
359}
360
361/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530362 * _scsih_get_sas_address - set the sas_address for given device handle
363 * @handle: device handle
364 * @sas_address: sas address
365 *
366 * Returns 0 success, non-zero when failure
367 */
368static int
369_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
370 u64 *sas_address)
371{
372 Mpi2SasDevicePage0_t sas_device_pg0;
373 Mpi2ConfigReply_t mpi_reply;
374 u32 ioc_status;
375
376 if (handle <= ioc->sas_hba.num_phys) {
377 *sas_address = ioc->sas_hba.sas_address;
378 return 0;
379 } else
380 *sas_address = 0;
381
382 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
383 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
384 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
385 ioc->name, __FILE__, __LINE__, __func__);
386 return -ENXIO;
387 }
388
389 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
390 MPI2_IOCSTATUS_MASK;
391 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
392 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
393 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
394 __FILE__, __LINE__, __func__);
395 return -EIO;
396 }
397
398 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
399 return 0;
400}
401
402/**
Eric Moore635374e2009-03-09 01:21:12 -0600403 * _scsih_determine_boot_device - determine boot device.
404 * @ioc: per adapter object
405 * @device: either sas_device or raid_device object
406 * @is_raid: [flag] 1 = raid object, 0 = sas object
407 *
408 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300409 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600410 * There are primary, alternate, and current entries in bios page 2. The order
411 * priority is primary, alternate, then current. This routine saves
412 * the corresponding device object and is_raid flag in the ioc object.
413 * The saved data to be used later in _scsih_probe_boot_devices().
414 */
415static void
416_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
417 void *device, u8 is_raid)
418{
419 struct _sas_device *sas_device;
420 struct _raid_device *raid_device;
421 u64 sas_address;
422 u64 device_name;
423 u64 enclosure_logical_id;
424 u16 slot;
425
426 /* only process this function when driver loads */
427 if (!ioc->wait_for_port_enable_to_complete)
428 return;
429
430 if (!is_raid) {
431 sas_device = device;
432 sas_address = sas_device->sas_address;
433 device_name = sas_device->device_name;
434 enclosure_logical_id = sas_device->enclosure_logical_id;
435 slot = sas_device->slot;
436 } else {
437 raid_device = device;
438 sas_address = raid_device->wwid;
439 device_name = 0;
440 enclosure_logical_id = 0;
441 slot = 0;
442 }
443
444 if (!ioc->req_boot_device.device) {
445 if (_scsih_is_boot_device(sas_address, device_name,
446 enclosure_logical_id, slot,
447 (ioc->bios_pg2.ReqBootDeviceForm &
448 MPI2_BIOSPAGE2_FORM_MASK),
449 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530450 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600451 "%s: req_boot_device(0x%016llx)\n",
452 ioc->name, __func__,
453 (unsigned long long)sas_address));
454 ioc->req_boot_device.device = device;
455 ioc->req_boot_device.is_raid = is_raid;
456 }
457 }
458
459 if (!ioc->req_alt_boot_device.device) {
460 if (_scsih_is_boot_device(sas_address, device_name,
461 enclosure_logical_id, slot,
462 (ioc->bios_pg2.ReqAltBootDeviceForm &
463 MPI2_BIOSPAGE2_FORM_MASK),
464 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530465 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600466 "%s: req_alt_boot_device(0x%016llx)\n",
467 ioc->name, __func__,
468 (unsigned long long)sas_address));
469 ioc->req_alt_boot_device.device = device;
470 ioc->req_alt_boot_device.is_raid = is_raid;
471 }
472 }
473
474 if (!ioc->current_boot_device.device) {
475 if (_scsih_is_boot_device(sas_address, device_name,
476 enclosure_logical_id, slot,
477 (ioc->bios_pg2.CurrentBootDeviceForm &
478 MPI2_BIOSPAGE2_FORM_MASK),
479 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530480 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600481 "%s: current_boot_device(0x%016llx)\n",
482 ioc->name, __func__,
483 (unsigned long long)sas_address));
484 ioc->current_boot_device.device = device;
485 ioc->current_boot_device.is_raid = is_raid;
486 }
487 }
488}
489
490/**
491 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
492 * @ioc: per adapter object
493 * @sas_address: sas address
494 * Context: Calling function should acquire ioc->sas_device_lock
495 *
496 * This searches for sas_device based on sas_address, then return sas_device
497 * object.
498 */
499struct _sas_device *
500mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
501 u64 sas_address)
502{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530503 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600504
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530505 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
506 if (sas_device->sas_address == sas_address)
507 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600508
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530509 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
510 if (sas_device->sas_address == sas_address)
511 return sas_device;
512
513 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600514}
515
516/**
517 * _scsih_sas_device_find_by_handle - sas device search
518 * @ioc: per adapter object
519 * @handle: sas device handle (assigned by firmware)
520 * Context: Calling function should acquire ioc->sas_device_lock
521 *
522 * This searches for sas_device based on sas_address, then return sas_device
523 * object.
524 */
525static struct _sas_device *
526_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
527{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530528 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600529
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530530 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
531 if (sas_device->handle == handle)
532 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600533
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530534 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
535 if (sas_device->handle == handle)
536 return sas_device;
537
538 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600539}
540
541/**
542 * _scsih_sas_device_remove - remove sas_device from list.
543 * @ioc: per adapter object
544 * @sas_device: the sas_device object
545 * Context: This function will acquire ioc->sas_device_lock.
546 *
547 * Removing object and freeing associated memory from the ioc->sas_device_list.
548 */
549static void
550_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
551 struct _sas_device *sas_device)
552{
553 unsigned long flags;
554
Kashyap, Desai980ead32010-04-08 17:55:22 +0530555 if (!sas_device)
556 return;
557
Eric Moore635374e2009-03-09 01:21:12 -0600558 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530559 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
560 sas_device->sas_address)) {
561 list_del(&sas_device->list);
562 kfree(sas_device);
563 }
Eric Moore635374e2009-03-09 01:21:12 -0600564 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
565}
566
567/**
568 * _scsih_sas_device_add - insert sas_device to the list.
569 * @ioc: per adapter object
570 * @sas_device: the sas_device object
571 * Context: This function will acquire ioc->sas_device_lock.
572 *
573 * Adding new object to the ioc->sas_device_list.
574 */
575static void
576_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
577 struct _sas_device *sas_device)
578{
579 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600580
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530581 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600582 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
583 sas_device->handle, (unsigned long long)sas_device->sas_address));
584
585 spin_lock_irqsave(&ioc->sas_device_lock, flags);
586 list_add_tail(&sas_device->list, &ioc->sas_device_list);
587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530589 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
590 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600591 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600592}
593
594/**
595 * _scsih_sas_device_init_add - insert sas_device to the list.
596 * @ioc: per adapter object
597 * @sas_device: the sas_device object
598 * Context: This function will acquire ioc->sas_device_lock.
599 *
600 * Adding new object at driver load time to the ioc->sas_device_init_list.
601 */
602static void
603_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
604 struct _sas_device *sas_device)
605{
606 unsigned long flags;
607
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530608 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600609 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
610 sas_device->handle, (unsigned long long)sas_device->sas_address));
611
612 spin_lock_irqsave(&ioc->sas_device_lock, flags);
613 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
614 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
615 _scsih_determine_boot_device(ioc, sas_device, 0);
616}
617
618/**
Eric Moore635374e2009-03-09 01:21:12 -0600619 * _scsih_raid_device_find_by_id - raid device search
620 * @ioc: per adapter object
621 * @id: sas device target id
622 * @channel: sas device channel
623 * Context: Calling function should acquire ioc->raid_device_lock
624 *
625 * This searches for raid_device based on target id, then return raid_device
626 * object.
627 */
628static struct _raid_device *
629_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
630{
631 struct _raid_device *raid_device, *r;
632
633 r = NULL;
634 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
635 if (raid_device->id == id && raid_device->channel == channel) {
636 r = raid_device;
637 goto out;
638 }
639 }
640
641 out:
642 return r;
643}
644
645/**
646 * _scsih_raid_device_find_by_handle - raid device search
647 * @ioc: per adapter object
648 * @handle: sas device handle (assigned by firmware)
649 * Context: Calling function should acquire ioc->raid_device_lock
650 *
651 * This searches for raid_device based on handle, then return raid_device
652 * object.
653 */
654static struct _raid_device *
655_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
656{
657 struct _raid_device *raid_device, *r;
658
659 r = NULL;
660 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
661 if (raid_device->handle != handle)
662 continue;
663 r = raid_device;
664 goto out;
665 }
666
667 out:
668 return r;
669}
670
671/**
672 * _scsih_raid_device_find_by_wwid - raid device search
673 * @ioc: per adapter object
674 * @handle: sas device handle (assigned by firmware)
675 * Context: Calling function should acquire ioc->raid_device_lock
676 *
677 * This searches for raid_device based on wwid, then return raid_device
678 * object.
679 */
680static struct _raid_device *
681_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
682{
683 struct _raid_device *raid_device, *r;
684
685 r = NULL;
686 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
687 if (raid_device->wwid != wwid)
688 continue;
689 r = raid_device;
690 goto out;
691 }
692
693 out:
694 return r;
695}
696
697/**
698 * _scsih_raid_device_add - add raid_device object
699 * @ioc: per adapter object
700 * @raid_device: raid_device object
701 *
702 * This is added to the raid_device_list link list.
703 */
704static void
705_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
706 struct _raid_device *raid_device)
707{
708 unsigned long flags;
709
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530710 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600711 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
712 raid_device->handle, (unsigned long long)raid_device->wwid));
713
714 spin_lock_irqsave(&ioc->raid_device_lock, flags);
715 list_add_tail(&raid_device->list, &ioc->raid_device_list);
716 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
717}
718
719/**
720 * _scsih_raid_device_remove - delete raid_device object
721 * @ioc: per adapter object
722 * @raid_device: raid_device object
723 *
724 * This is removed from the raid_device_list link list.
725 */
726static void
727_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
728 struct _raid_device *raid_device)
729{
730 unsigned long flags;
731
732 spin_lock_irqsave(&ioc->raid_device_lock, flags);
733 list_del(&raid_device->list);
734 memset(raid_device, 0, sizeof(struct _raid_device));
735 kfree(raid_device);
736 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
737}
738
739/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530740 * mpt2sas_scsih_expander_find_by_handle - expander device search
741 * @ioc: per adapter object
742 * @handle: expander handle (assigned by firmware)
743 * Context: Calling function should acquire ioc->sas_device_lock
744 *
745 * This searches for expander device based on handle, then returns the
746 * sas_node object.
747 */
748struct _sas_node *
749mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
750{
751 struct _sas_node *sas_expander, *r;
752
753 r = NULL;
754 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
755 if (sas_expander->handle != handle)
756 continue;
757 r = sas_expander;
758 goto out;
759 }
760 out:
761 return r;
762}
763
764/**
Eric Moore635374e2009-03-09 01:21:12 -0600765 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
766 * @ioc: per adapter object
767 * @sas_address: sas address
768 * Context: Calling function should acquire ioc->sas_node_lock.
769 *
770 * This searches for expander device based on sas_address, then returns the
771 * sas_node object.
772 */
773struct _sas_node *
774mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
775 u64 sas_address)
776{
777 struct _sas_node *sas_expander, *r;
778
779 r = NULL;
780 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
781 if (sas_expander->sas_address != sas_address)
782 continue;
783 r = sas_expander;
784 goto out;
785 }
786 out:
787 return r;
788}
789
790/**
791 * _scsih_expander_node_add - insert expander device to the list.
792 * @ioc: per adapter object
793 * @sas_expander: the sas_device object
794 * Context: This function will acquire ioc->sas_node_lock.
795 *
796 * Adding new object to the ioc->sas_expander_list.
797 *
798 * Return nothing.
799 */
800static void
801_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
802 struct _sas_node *sas_expander)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(&ioc->sas_node_lock, flags);
807 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
808 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
809}
810
811/**
812 * _scsih_is_end_device - determines if device is an end device
813 * @device_info: bitfield providing information about the device.
814 * Context: none
815 *
816 * Returns 1 if end device.
817 */
818static int
819_scsih_is_end_device(u32 device_info)
820{
821 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
822 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
823 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
824 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
825 return 1;
826 else
827 return 0;
828}
829
830/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530831 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600832 * @ioc: per adapter object
833 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600834 *
835 * Returns the smid stored scmd pointer.
836 */
837static struct scsi_cmnd *
838_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
839{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530840 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600841}
842
843/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530844 * _scsih_scsi_lookup_get_clear - returns scmd entry
845 * @ioc: per adapter object
846 * @smid: system request message index
847 *
848 * Returns the smid stored scmd pointer.
849 * Then will derefrence the stored scmd pointer.
850 */
851static inline struct scsi_cmnd *
852_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
853{
854 unsigned long flags;
855 struct scsi_cmnd *scmd;
856
857 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
858 scmd = ioc->scsi_lookup[smid - 1].scmd;
859 ioc->scsi_lookup[smid - 1].scmd = NULL;
860 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
861
862 return scmd;
863}
864
865/**
Eric Moore635374e2009-03-09 01:21:12 -0600866 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
867 * @ioc: per adapter object
868 * @smid: system request message index
869 * @scmd: pointer to scsi command object
870 * Context: This function will acquire ioc->scsi_lookup_lock.
871 *
872 * This will search for a scmd pointer in the scsi_lookup array,
873 * returning the revelent smid. A returned value of zero means invalid.
874 */
875static u16
876_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
877 *scmd)
878{
879 u16 smid;
880 unsigned long flags;
881 int i;
882
883 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
884 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530885 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600886 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530887 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600888 goto out;
889 }
890 }
891 out:
892 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
893 return smid;
894}
895
896/**
897 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
898 * @ioc: per adapter object
899 * @id: target id
900 * @channel: channel
901 * Context: This function will acquire ioc->scsi_lookup_lock.
902 *
903 * This will search for a matching channel:id in the scsi_lookup array,
904 * returning 1 if found.
905 */
906static u8
907_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
908 int channel)
909{
910 u8 found;
911 unsigned long flags;
912 int i;
913
914 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
915 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530916 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600917 if (ioc->scsi_lookup[i].scmd &&
918 (ioc->scsi_lookup[i].scmd->device->id == id &&
919 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
920 found = 1;
921 goto out;
922 }
923 }
924 out:
925 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
926 return found;
927}
928
929/**
Eric Moore993e0da2009-05-18 13:00:45 -0600930 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
931 * @ioc: per adapter object
932 * @id: target id
933 * @lun: lun number
934 * @channel: channel
935 * Context: This function will acquire ioc->scsi_lookup_lock.
936 *
937 * This will search for a matching channel:id:lun in the scsi_lookup array,
938 * returning 1 if found.
939 */
940static u8
941_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
942 unsigned int lun, int channel)
943{
944 u8 found;
945 unsigned long flags;
946 int i;
947
948 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
949 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530950 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600951 if (ioc->scsi_lookup[i].scmd &&
952 (ioc->scsi_lookup[i].scmd->device->id == id &&
953 ioc->scsi_lookup[i].scmd->device->channel == channel &&
954 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
955 found = 1;
956 goto out;
957 }
958 }
959 out:
960 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
961 return found;
962}
963
964/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530965 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600966 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530967 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600968 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530969 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -0600970 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530971static struct chain_tracker *
972_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -0600973{
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530974 struct chain_tracker *chain_req;
975 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600976
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530977 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
978 if (list_empty(&ioc->free_chain_list)) {
979 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
980 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
981 ioc->name);
982 return NULL;
983 }
984 chain_req = list_entry(ioc->free_chain_list.next,
985 struct chain_tracker, tracker_list);
986 list_del_init(&chain_req->tracker_list);
987 list_add_tail(&chain_req->tracker_list,
988 &ioc->scsi_lookup[smid - 1].chain_list);
989 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
990 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -0600991}
992
993/**
994 * _scsih_build_scatter_gather - main sg creation routine
995 * @ioc: per adapter object
996 * @scmd: scsi command
997 * @smid: system request message index
998 * Context: none.
999 *
1000 * The main routine that builds scatter gather table from a given
1001 * scsi request sent via the .queuecommand main handler.
1002 *
1003 * Returns 0 success, anything else error
1004 */
1005static int
1006_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1007 struct scsi_cmnd *scmd, u16 smid)
1008{
1009 Mpi2SCSIIORequest_t *mpi_request;
1010 dma_addr_t chain_dma;
1011 struct scatterlist *sg_scmd;
1012 void *sg_local, *chain;
1013 u32 chain_offset;
1014 u32 chain_length;
1015 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001016 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001017 u32 sges_in_segment;
1018 u32 sgl_flags;
1019 u32 sgl_flags_last_element;
1020 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301021 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001022
1023 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1024
1025 /* init scatter gather flags */
1026 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1027 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1028 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1029 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1030 << MPI2_SGE_FLAGS_SHIFT;
1031 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1032 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1033 << MPI2_SGE_FLAGS_SHIFT;
1034 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1035
1036 sg_scmd = scsi_sglist(scmd);
1037 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001038 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001039 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1040 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1041 return -ENOMEM;
1042 }
1043
1044 sg_local = &mpi_request->SGL;
1045 sges_in_segment = ioc->max_sges_in_main_message;
1046 if (sges_left <= sges_in_segment)
1047 goto fill_in_last_segment;
1048
1049 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1050 (sges_in_segment * ioc->sge_size))/4;
1051
1052 /* fill in main message segment when there is a chain following */
1053 while (sges_in_segment) {
1054 if (sges_in_segment == 1)
1055 ioc->base_add_sg_single(sg_local,
1056 sgl_flags_last_element | sg_dma_len(sg_scmd),
1057 sg_dma_address(sg_scmd));
1058 else
1059 ioc->base_add_sg_single(sg_local, sgl_flags |
1060 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1061 sg_scmd = sg_next(sg_scmd);
1062 sg_local += ioc->sge_size;
1063 sges_left--;
1064 sges_in_segment--;
1065 }
1066
1067 /* initializing the chain flags and pointers */
1068 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301069 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1070 if (!chain_req)
1071 return -1;
1072 chain = chain_req->chain_buffer;
1073 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001074 do {
1075 sges_in_segment = (sges_left <=
1076 ioc->max_sges_in_chain_message) ? sges_left :
1077 ioc->max_sges_in_chain_message;
1078 chain_offset = (sges_left == sges_in_segment) ?
1079 0 : (sges_in_segment * ioc->sge_size)/4;
1080 chain_length = sges_in_segment * ioc->sge_size;
1081 if (chain_offset) {
1082 chain_offset = chain_offset <<
1083 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1084 chain_length += ioc->sge_size;
1085 }
1086 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1087 chain_length, chain_dma);
1088 sg_local = chain;
1089 if (!chain_offset)
1090 goto fill_in_last_segment;
1091
1092 /* fill in chain segments */
1093 while (sges_in_segment) {
1094 if (sges_in_segment == 1)
1095 ioc->base_add_sg_single(sg_local,
1096 sgl_flags_last_element |
1097 sg_dma_len(sg_scmd),
1098 sg_dma_address(sg_scmd));
1099 else
1100 ioc->base_add_sg_single(sg_local, sgl_flags |
1101 sg_dma_len(sg_scmd),
1102 sg_dma_address(sg_scmd));
1103 sg_scmd = sg_next(sg_scmd);
1104 sg_local += ioc->sge_size;
1105 sges_left--;
1106 sges_in_segment--;
1107 }
1108
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301109 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1110 if (!chain_req)
1111 return -1;
1112 chain = chain_req->chain_buffer;
1113 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001114 } while (1);
1115
1116
1117 fill_in_last_segment:
1118
1119 /* fill the last segment */
1120 while (sges_left) {
1121 if (sges_left == 1)
1122 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1123 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1124 else
1125 ioc->base_add_sg_single(sg_local, sgl_flags |
1126 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1127 sg_scmd = sg_next(sg_scmd);
1128 sg_local += ioc->sge_size;
1129 sges_left--;
1130 }
1131
1132 return 0;
1133}
1134
1135/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301136 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001137 * @sdev: scsi device struct
1138 * @qdepth: requested queue depth
1139 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301140 *
1141 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001142 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301143static void
1144_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001145{
1146 struct Scsi_Host *shost = sdev->host;
1147 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301148 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1149 struct MPT2SAS_DEVICE *sas_device_priv_data;
1150 struct MPT2SAS_TARGET *sas_target_priv_data;
1151 struct _sas_device *sas_device;
1152 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001153
1154 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301155
1156 /* limit max device queue for SATA to 32 */
1157 sas_device_priv_data = sdev->hostdata;
1158 if (!sas_device_priv_data)
1159 goto not_sata;
1160 sas_target_priv_data = sas_device_priv_data->sas_target;
1161 if (!sas_target_priv_data)
1162 goto not_sata;
1163 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1164 goto not_sata;
1165 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1166 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1167 sas_device_priv_data->sas_target->sas_address);
1168 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1169 if (sas_device && sas_device->device_info &
1170 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1171 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1172
1173 not_sata:
1174
Eric Moore635374e2009-03-09 01:21:12 -06001175 if (!sdev->tagged_supported)
1176 max_depth = 1;
1177 if (qdepth > max_depth)
1178 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301179 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1180}
1181
1182/**
1183 * _scsih_change_queue_depth - setting device queue depth
1184 * @sdev: scsi device struct
1185 * @qdepth: requested queue depth
1186 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1187 * (see include/scsi/scsi_host.h for definition)
1188 *
1189 * Returns queue depth.
1190 */
1191static int
1192_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1193{
1194 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1195 _scsih_adjust_queue_depth(sdev, qdepth);
1196 else if (reason == SCSI_QDEPTH_QFULL)
1197 scsi_track_queue_full(sdev, qdepth);
1198 else
1199 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001200
1201 if (sdev->inquiry_len > 7)
1202 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1203 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1204 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1205 sdev->ordered_tags, sdev->scsi_level,
1206 (sdev->inquiry[7] & 2) >> 1);
1207
1208 return sdev->queue_depth;
1209}
1210
1211/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301212 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001213 * @sdev: scsi device struct
1214 * @tag_type: requested tag type
1215 *
1216 * Returns queue tag type.
1217 */
1218static int
Eric Moored5d135b2009-05-18 13:02:08 -06001219_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001220{
1221 if (sdev->tagged_supported) {
1222 scsi_set_tag_type(sdev, tag_type);
1223 if (tag_type)
1224 scsi_activate_tcq(sdev, sdev->queue_depth);
1225 else
1226 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1227 } else
1228 tag_type = 0;
1229
1230 return tag_type;
1231}
1232
1233/**
Eric Moored5d135b2009-05-18 13:02:08 -06001234 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001235 * @starget: scsi target struct
1236 *
1237 * Returns 0 if ok. Any other return is assumed to be an error and
1238 * the device is ignored.
1239 */
1240static int
Eric Moored5d135b2009-05-18 13:02:08 -06001241_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001242{
1243 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1244 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1245 struct MPT2SAS_TARGET *sas_target_priv_data;
1246 struct _sas_device *sas_device;
1247 struct _raid_device *raid_device;
1248 unsigned long flags;
1249 struct sas_rphy *rphy;
1250
1251 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1252 if (!sas_target_priv_data)
1253 return -ENOMEM;
1254
1255 starget->hostdata = sas_target_priv_data;
1256 sas_target_priv_data->starget = starget;
1257 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1258
1259 /* RAID volumes */
1260 if (starget->channel == RAID_CHANNEL) {
1261 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1262 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1263 starget->channel);
1264 if (raid_device) {
1265 sas_target_priv_data->handle = raid_device->handle;
1266 sas_target_priv_data->sas_address = raid_device->wwid;
1267 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301268 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001269 raid_device->starget = starget;
1270 }
1271 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1272 return 0;
1273 }
1274
1275 /* sas/sata devices */
1276 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1277 rphy = dev_to_rphy(starget->dev.parent);
1278 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1279 rphy->identify.sas_address);
1280
1281 if (sas_device) {
1282 sas_target_priv_data->handle = sas_device->handle;
1283 sas_target_priv_data->sas_address = sas_device->sas_address;
1284 sas_device->starget = starget;
1285 sas_device->id = starget->id;
1286 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301287 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001288 sas_target_priv_data->flags |=
1289 MPT_TARGET_FLAGS_RAID_COMPONENT;
1290 }
1291 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1292
1293 return 0;
1294}
1295
1296/**
Eric Moored5d135b2009-05-18 13:02:08 -06001297 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001298 * @starget: scsi target struct
1299 *
1300 * Returns nothing.
1301 */
1302static void
Eric Moored5d135b2009-05-18 13:02:08 -06001303_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001304{
1305 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1306 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1307 struct MPT2SAS_TARGET *sas_target_priv_data;
1308 struct _sas_device *sas_device;
1309 struct _raid_device *raid_device;
1310 unsigned long flags;
1311 struct sas_rphy *rphy;
1312
1313 sas_target_priv_data = starget->hostdata;
1314 if (!sas_target_priv_data)
1315 return;
1316
1317 if (starget->channel == RAID_CHANNEL) {
1318 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1319 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1320 starget->channel);
1321 if (raid_device) {
1322 raid_device->starget = NULL;
1323 raid_device->sdev = NULL;
1324 }
1325 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1326 goto out;
1327 }
1328
1329 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1330 rphy = dev_to_rphy(starget->dev.parent);
1331 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1332 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001333 if (sas_device && (sas_device->starget == starget) &&
1334 (sas_device->id == starget->id) &&
1335 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001336 sas_device->starget = NULL;
1337
1338 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1339
1340 out:
1341 kfree(sas_target_priv_data);
1342 starget->hostdata = NULL;
1343}
1344
1345/**
Eric Moored5d135b2009-05-18 13:02:08 -06001346 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001347 * @sdev: scsi device struct
1348 *
1349 * Returns 0 if ok. Any other return is assumed to be an error and
1350 * the device is ignored.
1351 */
1352static int
Eric Moored5d135b2009-05-18 13:02:08 -06001353_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001354{
1355 struct Scsi_Host *shost;
1356 struct MPT2SAS_ADAPTER *ioc;
1357 struct MPT2SAS_TARGET *sas_target_priv_data;
1358 struct MPT2SAS_DEVICE *sas_device_priv_data;
1359 struct scsi_target *starget;
1360 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001361 unsigned long flags;
1362
1363 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1364 if (!sas_device_priv_data)
1365 return -ENOMEM;
1366
1367 sas_device_priv_data->lun = sdev->lun;
1368 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1369
1370 starget = scsi_target(sdev);
1371 sas_target_priv_data = starget->hostdata;
1372 sas_target_priv_data->num_luns++;
1373 sas_device_priv_data->sas_target = sas_target_priv_data;
1374 sdev->hostdata = sas_device_priv_data;
1375 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1376 sdev->no_uld_attach = 1;
1377
1378 shost = dev_to_shost(&starget->dev);
1379 ioc = shost_priv(shost);
1380 if (starget->channel == RAID_CHANNEL) {
1381 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1382 raid_device = _scsih_raid_device_find_by_id(ioc,
1383 starget->id, starget->channel);
1384 if (raid_device)
1385 raid_device->sdev = sdev; /* raid is single lun */
1386 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001387 }
1388
Eric Moore635374e2009-03-09 01:21:12 -06001389 return 0;
1390}
1391
1392/**
Eric Moored5d135b2009-05-18 13:02:08 -06001393 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001394 * @sdev: scsi device struct
1395 *
1396 * Returns nothing.
1397 */
1398static void
Eric Moored5d135b2009-05-18 13:02:08 -06001399_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001400{
1401 struct MPT2SAS_TARGET *sas_target_priv_data;
1402 struct scsi_target *starget;
1403
1404 if (!sdev->hostdata)
1405 return;
1406
1407 starget = scsi_target(sdev);
1408 sas_target_priv_data = starget->hostdata;
1409 sas_target_priv_data->num_luns--;
1410 kfree(sdev->hostdata);
1411 sdev->hostdata = NULL;
1412}
1413
1414/**
Eric Moored5d135b2009-05-18 13:02:08 -06001415 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001416 * @ioc: per adapter object
1417 * @sas_device: the sas_device object
1418 * @sdev: scsi device struct
1419 */
1420static void
Eric Moored5d135b2009-05-18 13:02:08 -06001421_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001422 struct _sas_device *sas_device, struct scsi_device *sdev)
1423{
1424 Mpi2ConfigReply_t mpi_reply;
1425 Mpi2SasDevicePage0_t sas_device_pg0;
1426 u32 ioc_status;
1427 u16 flags;
1428 u32 device_info;
1429
1430 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1431 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1432 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1433 ioc->name, __FILE__, __LINE__, __func__);
1434 return;
1435 }
1436
1437 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1438 MPI2_IOCSTATUS_MASK;
1439 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1440 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1441 ioc->name, __FILE__, __LINE__, __func__);
1442 return;
1443 }
1444
1445 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301446 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001447
1448 sdev_printk(KERN_INFO, sdev,
1449 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1450 "sw_preserve(%s)\n",
1451 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1452 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1453 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1454 "n",
1455 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1456 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1457 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1458}
1459
1460/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301461 * _scsih_is_raid - return boolean indicating device is raid volume
1462 * @dev the device struct object
1463 */
1464static int
1465_scsih_is_raid(struct device *dev)
1466{
1467 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301468 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301469
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301470 if (ioc->is_warpdrive)
1471 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301472 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1473}
1474
1475/**
1476 * _scsih_get_resync - get raid volume resync percent complete
1477 * @dev the device struct object
1478 */
1479static void
1480_scsih_get_resync(struct device *dev)
1481{
1482 struct scsi_device *sdev = to_scsi_device(dev);
1483 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1484 static struct _raid_device *raid_device;
1485 unsigned long flags;
1486 Mpi2RaidVolPage0_t vol_pg0;
1487 Mpi2ConfigReply_t mpi_reply;
1488 u32 volume_status_flags;
1489 u8 percent_complete = 0;
1490
1491 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1492 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1493 sdev->channel);
1494 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1495
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301496 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301497 goto out;
1498
1499 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1500 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1501 sizeof(Mpi2RaidVolPage0_t))) {
1502 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1503 ioc->name, __FILE__, __LINE__, __func__);
1504 goto out;
1505 }
1506
1507 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1508 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1509 percent_complete = raid_device->percent_complete;
1510 out:
1511 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1512}
1513
1514/**
1515 * _scsih_get_state - get raid volume level
1516 * @dev the device struct object
1517 */
1518static void
1519_scsih_get_state(struct device *dev)
1520{
1521 struct scsi_device *sdev = to_scsi_device(dev);
1522 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1523 static struct _raid_device *raid_device;
1524 unsigned long flags;
1525 Mpi2RaidVolPage0_t vol_pg0;
1526 Mpi2ConfigReply_t mpi_reply;
1527 u32 volstate;
1528 enum raid_state state = RAID_STATE_UNKNOWN;
1529
1530 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1531 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1532 sdev->channel);
1533 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1534
1535 if (!raid_device)
1536 goto out;
1537
1538 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1539 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1540 sizeof(Mpi2RaidVolPage0_t))) {
1541 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1542 ioc->name, __FILE__, __LINE__, __func__);
1543 goto out;
1544 }
1545
1546 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1547 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1548 state = RAID_STATE_RESYNCING;
1549 goto out;
1550 }
1551
1552 switch (vol_pg0.VolumeState) {
1553 case MPI2_RAID_VOL_STATE_OPTIMAL:
1554 case MPI2_RAID_VOL_STATE_ONLINE:
1555 state = RAID_STATE_ACTIVE;
1556 break;
1557 case MPI2_RAID_VOL_STATE_DEGRADED:
1558 state = RAID_STATE_DEGRADED;
1559 break;
1560 case MPI2_RAID_VOL_STATE_FAILED:
1561 case MPI2_RAID_VOL_STATE_MISSING:
1562 state = RAID_STATE_OFFLINE;
1563 break;
1564 }
1565 out:
1566 raid_set_state(mpt2sas_raid_template, dev, state);
1567}
1568
1569/**
1570 * _scsih_set_level - set raid level
1571 * @sdev: scsi device struct
1572 * @raid_device: raid_device object
1573 */
1574static void
1575_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1576{
1577 enum raid_level level = RAID_LEVEL_UNKNOWN;
1578
1579 switch (raid_device->volume_type) {
1580 case MPI2_RAID_VOL_TYPE_RAID0:
1581 level = RAID_LEVEL_0;
1582 break;
1583 case MPI2_RAID_VOL_TYPE_RAID10:
1584 level = RAID_LEVEL_10;
1585 break;
1586 case MPI2_RAID_VOL_TYPE_RAID1E:
1587 level = RAID_LEVEL_1E;
1588 break;
1589 case MPI2_RAID_VOL_TYPE_RAID1:
1590 level = RAID_LEVEL_1;
1591 break;
1592 }
1593
1594 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1595}
1596
1597/**
Eric Moore635374e2009-03-09 01:21:12 -06001598 * _scsih_get_volume_capabilities - volume capabilities
1599 * @ioc: per adapter object
1600 * @sas_device: the raid_device object
1601 */
1602static void
1603_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1604 struct _raid_device *raid_device)
1605{
1606 Mpi2RaidVolPage0_t *vol_pg0;
1607 Mpi2RaidPhysDiskPage0_t pd_pg0;
1608 Mpi2SasDevicePage0_t sas_device_pg0;
1609 Mpi2ConfigReply_t mpi_reply;
1610 u16 sz;
1611 u8 num_pds;
1612
1613 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1614 &num_pds)) || !num_pds) {
1615 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1616 ioc->name, __FILE__, __LINE__, __func__);
1617 return;
1618 }
1619
1620 raid_device->num_pds = num_pds;
1621 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1622 sizeof(Mpi2RaidVol0PhysDisk_t));
1623 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1624 if (!vol_pg0) {
1625 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1626 ioc->name, __FILE__, __LINE__, __func__);
1627 return;
1628 }
1629
1630 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1631 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1632 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1633 ioc->name, __FILE__, __LINE__, __func__);
1634 kfree(vol_pg0);
1635 return;
1636 }
1637
1638 raid_device->volume_type = vol_pg0->VolumeType;
1639
1640 /* figure out what the underlying devices are by
1641 * obtaining the device_info bits for the 1st device
1642 */
1643 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1644 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1645 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1646 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1647 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1648 le16_to_cpu(pd_pg0.DevHandle)))) {
1649 raid_device->device_info =
1650 le32_to_cpu(sas_device_pg0.DeviceInfo);
1651 }
1652 }
1653
1654 kfree(vol_pg0);
1655}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301656/**
1657 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1658 * @ioc: per adapter object
1659 */
1660static void
1661_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1662{
1663 Mpi2RaidVolPage1_t vol_pg1;
1664 Mpi2ConfigReply_t mpi_reply;
1665 struct _raid_device *raid_device;
1666 u16 handle;
1667 u16 ioc_status;
1668
1669 handle = 0xFFFF;
1670 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1671 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1672 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1673 MPI2_IOCSTATUS_MASK;
1674 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1675 break;
1676 handle = le16_to_cpu(vol_pg1.DevHandle);
1677 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1678 if (raid_device)
1679 raid_device->direct_io_enabled = 0;
1680 }
1681 return;
1682}
1683
1684
1685/**
1686 * _scsih_get_num_volumes - Get number of volumes in the ioc
1687 * @ioc: per adapter object
1688 */
1689static u8
1690_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1691{
1692 Mpi2RaidVolPage1_t vol_pg1;
1693 Mpi2ConfigReply_t mpi_reply;
1694 u16 handle;
1695 u8 vol_cnt = 0;
1696 u16 ioc_status;
1697
1698 handle = 0xFFFF;
1699 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1700 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1701 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1702 MPI2_IOCSTATUS_MASK;
1703 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1704 break;
1705 vol_cnt++;
1706 handle = le16_to_cpu(vol_pg1.DevHandle);
1707 }
1708 return vol_cnt;
1709}
1710
1711
1712/**
1713 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1714 * @ioc: per adapter object
1715 * @raid_device: the raid_device object
1716 */
1717static void
1718_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1719 struct _raid_device *raid_device)
1720{
1721 Mpi2RaidVolPage0_t *vol_pg0;
1722 Mpi2RaidPhysDiskPage0_t pd_pg0;
1723 Mpi2ConfigReply_t mpi_reply;
1724 u16 sz;
1725 u8 num_pds, count;
1726 u64 mb = 1024 * 1024;
1727 u64 tb_2 = 2 * mb * mb;
1728 u64 capacity;
1729 u32 stripe_sz;
1730 u8 i, stripe_exp;
1731
1732 if (!ioc->is_warpdrive)
1733 return;
1734
1735 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1736 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1737 "globally as drives are exposed\n", ioc->name);
1738 return;
1739 }
1740 if (_scsih_get_num_volumes(ioc) > 1) {
1741 _scsih_disable_ddio(ioc);
1742 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1743 "globally as number of drives > 1\n", ioc->name);
1744 return;
1745 }
1746 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1747 &num_pds)) || !num_pds) {
1748 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1749 "Failure in computing number of drives\n", ioc->name);
1750 return;
1751 }
1752
1753 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1754 sizeof(Mpi2RaidVol0PhysDisk_t));
1755 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1756 if (!vol_pg0) {
1757 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1758 "Memory allocation failure for RVPG0\n", ioc->name);
1759 return;
1760 }
1761
1762 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1763 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1764 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1765 "Failure in retrieving RVPG0\n", ioc->name);
1766 kfree(vol_pg0);
1767 return;
1768 }
1769
1770 /*
1771 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1772 * assumed for WARPDRIVE, disable direct I/O
1773 */
1774 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1775 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1776 "for the drive with handle(0x%04x): num_mem=%d, "
1777 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1778 num_pds, MPT_MAX_WARPDRIVE_PDS);
1779 kfree(vol_pg0);
1780 return;
1781 }
1782 for (count = 0; count < num_pds; count++) {
1783 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1784 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1785 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1786 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1787 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1788 "disabled for the drive with handle(0x%04x) member"
1789 "handle retrieval failed for member number=%d\n",
1790 ioc->name, raid_device->handle,
1791 vol_pg0->PhysDisk[count].PhysDiskNum);
1792 goto out_error;
1793 }
1794 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1795 }
1796
1797 /*
1798 * Assumption for WD: Direct I/O is not supported if the volume is
1799 * not RAID0, if the stripe size is not 64KB, if the block size is
1800 * not 512 and if the volume size is >2TB
1801 */
1802 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1803 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1804 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1805 "for the drive with handle(0x%04x): type=%d, "
1806 "s_sz=%uK, blk_size=%u\n", ioc->name,
1807 raid_device->handle, raid_device->volume_type,
1808 le32_to_cpu(vol_pg0->StripeSize)/2,
1809 le16_to_cpu(vol_pg0->BlockSize));
1810 goto out_error;
1811 }
1812
1813 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1814 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1815
1816 if (capacity > tb_2) {
1817 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1819 ioc->name, raid_device->handle);
1820 goto out_error;
1821 }
1822
1823 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1824 stripe_exp = 0;
1825 for (i = 0; i < 32; i++) {
1826 if (stripe_sz & 1)
1827 break;
1828 stripe_exp++;
1829 stripe_sz >>= 1;
1830 }
1831 if (i == 32) {
1832 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1833 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1834 ioc->name, raid_device->handle,
1835 le32_to_cpu(vol_pg0->StripeSize)/2);
1836 goto out_error;
1837 }
1838 raid_device->stripe_exponent = stripe_exp;
1839 raid_device->direct_io_enabled = 1;
1840
1841 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1842 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1843 /*
1844 * WARPDRIVE: Though the following fields are not used for direct IO,
1845 * stored for future purpose:
1846 */
1847 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1848 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1849 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1850
1851
1852 kfree(vol_pg0);
1853 return;
1854
1855out_error:
1856 raid_device->direct_io_enabled = 0;
1857 for (count = 0; count < num_pds; count++)
1858 raid_device->pd_handle[count] = 0;
1859 kfree(vol_pg0);
1860 return;
1861}
Eric Moore635374e2009-03-09 01:21:12 -06001862
1863/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301864 * _scsih_enable_tlr - setting TLR flags
1865 * @ioc: per adapter object
1866 * @sdev: scsi device struct
1867 *
1868 * Enabling Transaction Layer Retries for tape devices when
1869 * vpd page 0x90 is present
1870 *
1871 */
1872static void
1873_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1874{
1875 /* only for TAPE */
1876 if (sdev->type != TYPE_TAPE)
1877 return;
1878
1879 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1880 return;
1881
1882 sas_enable_tlr(sdev);
1883 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1884 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1885 return;
1886
1887}
1888
1889/**
Eric Moored5d135b2009-05-18 13:02:08 -06001890 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001891 * @sdev: scsi device struct
1892 *
1893 * Returns 0 if ok. Any other return is assumed to be an error and
1894 * the device is ignored.
1895 */
1896static int
Eric Moored5d135b2009-05-18 13:02:08 -06001897_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001898{
1899 struct Scsi_Host *shost = sdev->host;
1900 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1901 struct MPT2SAS_DEVICE *sas_device_priv_data;
1902 struct MPT2SAS_TARGET *sas_target_priv_data;
1903 struct _sas_device *sas_device;
1904 struct _raid_device *raid_device;
1905 unsigned long flags;
1906 int qdepth;
1907 u8 ssp_target = 0;
1908 char *ds = "";
1909 char *r_level = "";
1910
1911 qdepth = 1;
1912 sas_device_priv_data = sdev->hostdata;
1913 sas_device_priv_data->configured_lun = 1;
1914 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1915 sas_target_priv_data = sas_device_priv_data->sas_target;
1916
1917 /* raid volume handling */
1918 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1919
1920 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1921 raid_device = _scsih_raid_device_find_by_handle(ioc,
1922 sas_target_priv_data->handle);
1923 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1924 if (!raid_device) {
1925 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1926 ioc->name, __FILE__, __LINE__, __func__);
1927 return 0;
1928 }
1929
1930 _scsih_get_volume_capabilities(ioc, raid_device);
1931
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301932 /*
1933 * WARPDRIVE: Initialize the required data for Direct IO
1934 */
1935 _scsih_init_warpdrive_properties(ioc, raid_device);
1936
Eric Moore635374e2009-03-09 01:21:12 -06001937 /* RAID Queue Depth Support
1938 * IS volume = underlying qdepth of drive type, either
1939 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1940 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1941 */
1942 if (raid_device->device_info &
1943 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1944 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1945 ds = "SSP";
1946 } else {
1947 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1948 if (raid_device->device_info &
1949 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1950 ds = "SATA";
1951 else
1952 ds = "STP";
1953 }
1954
1955 switch (raid_device->volume_type) {
1956 case MPI2_RAID_VOL_TYPE_RAID0:
1957 r_level = "RAID0";
1958 break;
1959 case MPI2_RAID_VOL_TYPE_RAID1E:
1960 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301961 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301962 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05301963 MFG10_GF0_R10_DISPLAY) &&
1964 !(raid_device->num_pds % 2))
1965 r_level = "RAID10";
1966 else
1967 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001968 break;
1969 case MPI2_RAID_VOL_TYPE_RAID1:
1970 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1971 r_level = "RAID1";
1972 break;
1973 case MPI2_RAID_VOL_TYPE_RAID10:
1974 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1975 r_level = "RAID10";
1976 break;
1977 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1978 default:
1979 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1980 r_level = "RAIDX";
1981 break;
1982 }
1983
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301984 if (!ioc->hide_ir_msg)
1985 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1986 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
1987 r_level, raid_device->handle,
1988 (unsigned long long)raid_device->wwid,
1989 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001990 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301991 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301992 if (!ioc->is_warpdrive)
1993 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001994 return 0;
1995 }
1996
1997 /* non-raid handling */
1998 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1999 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2000 sas_device_priv_data->sas_target->sas_address);
2001 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2002 if (sas_device) {
2003 if (sas_target_priv_data->flags &
2004 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2005 mpt2sas_config_get_volume_handle(ioc,
2006 sas_device->handle, &sas_device->volume_handle);
2007 mpt2sas_config_get_volume_wwid(ioc,
2008 sas_device->volume_handle,
2009 &sas_device->volume_wwid);
2010 }
2011 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2012 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2013 ssp_target = 1;
2014 ds = "SSP";
2015 } else {
2016 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2017 if (sas_device->device_info &
2018 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2019 ds = "STP";
2020 else if (sas_device->device_info &
2021 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2022 ds = "SATA";
2023 }
2024
2025 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302026 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002027 ds, sas_device->handle,
2028 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302029 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002030 (unsigned long long)sas_device->device_name);
2031 sdev_printk(KERN_INFO, sdev, "%s: "
2032 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2033 (unsigned long long) sas_device->enclosure_logical_id,
2034 sas_device->slot);
2035
2036 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002037 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002038 }
2039
Mike Christiee881a172009-10-15 17:46:39 -07002040 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002041
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302042 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002043 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302044 _scsih_enable_tlr(ioc, sdev);
2045 }
Eric Moore635374e2009-03-09 01:21:12 -06002046 return 0;
2047}
2048
2049/**
Eric Moored5d135b2009-05-18 13:02:08 -06002050 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002051 * @sdev: scsi device struct
2052 * @bdev: pointer to block device context
2053 * @capacity: device size (in 512 byte sectors)
2054 * @params: three element array to place output:
2055 * params[0] number of heads (max 255)
2056 * params[1] number of sectors (max 63)
2057 * params[2] number of cylinders
2058 *
2059 * Return nothing.
2060 */
2061static int
Eric Moored5d135b2009-05-18 13:02:08 -06002062_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002063 sector_t capacity, int params[])
2064{
2065 int heads;
2066 int sectors;
2067 sector_t cylinders;
2068 ulong dummy;
2069
2070 heads = 64;
2071 sectors = 32;
2072
2073 dummy = heads * sectors;
2074 cylinders = capacity;
2075 sector_div(cylinders, dummy);
2076
2077 /*
2078 * Handle extended translation size for logical drives
2079 * > 1Gb
2080 */
2081 if ((ulong)capacity >= 0x200000) {
2082 heads = 255;
2083 sectors = 63;
2084 dummy = heads * sectors;
2085 cylinders = capacity;
2086 sector_div(cylinders, dummy);
2087 }
2088
2089 /* return result */
2090 params[0] = heads;
2091 params[1] = sectors;
2092 params[2] = cylinders;
2093
2094 return 0;
2095}
2096
2097/**
2098 * _scsih_response_code - translation of device response code
2099 * @ioc: per adapter object
2100 * @response_code: response code returned by the device
2101 *
2102 * Return nothing.
2103 */
2104static void
2105_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2106{
2107 char *desc;
2108
2109 switch (response_code) {
2110 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2111 desc = "task management request completed";
2112 break;
2113 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2114 desc = "invalid frame";
2115 break;
2116 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2117 desc = "task management request not supported";
2118 break;
2119 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2120 desc = "task management request failed";
2121 break;
2122 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2123 desc = "task management request succeeded";
2124 break;
2125 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2126 desc = "invalid lun";
2127 break;
2128 case 0xA:
2129 desc = "overlapped tag attempted";
2130 break;
2131 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2132 desc = "task queued, however not sent to target";
2133 break;
2134 default:
2135 desc = "unknown";
2136 break;
2137 }
2138 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2139 ioc->name, response_code, desc);
2140}
2141
2142/**
Eric Moored5d135b2009-05-18 13:02:08 -06002143 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002144 * @ioc: per adapter object
2145 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302146 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002147 * @reply: reply message frame(lower 32bit addr)
2148 * Context: none.
2149 *
2150 * The callback handler when using scsih_issue_tm.
2151 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302152 * Return 1 meaning mf should be freed from _base_interrupt
2153 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002154 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302155static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302156_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002157{
2158 MPI2DefaultReply_t *mpi_reply;
2159
2160 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302161 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002162 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302163 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002164 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2165 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2166 if (mpi_reply) {
2167 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2168 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2169 }
2170 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2171 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302172 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002173}
2174
2175/**
2176 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2177 * @ioc: per adapter object
2178 * @handle: device handle
2179 *
2180 * During taskmangement request, we need to freeze the device queue.
2181 */
2182void
2183mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2184{
2185 struct MPT2SAS_DEVICE *sas_device_priv_data;
2186 struct scsi_device *sdev;
2187 u8 skip = 0;
2188
2189 shost_for_each_device(sdev, ioc->shost) {
2190 if (skip)
2191 continue;
2192 sas_device_priv_data = sdev->hostdata;
2193 if (!sas_device_priv_data)
2194 continue;
2195 if (sas_device_priv_data->sas_target->handle == handle) {
2196 sas_device_priv_data->sas_target->tm_busy = 1;
2197 skip = 1;
2198 ioc->ignore_loginfos = 1;
2199 }
2200 }
2201}
2202
2203/**
2204 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2205 * @ioc: per adapter object
2206 * @handle: device handle
2207 *
2208 * During taskmangement request, we need to freeze the device queue.
2209 */
2210void
2211mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2212{
2213 struct MPT2SAS_DEVICE *sas_device_priv_data;
2214 struct scsi_device *sdev;
2215 u8 skip = 0;
2216
2217 shost_for_each_device(sdev, ioc->shost) {
2218 if (skip)
2219 continue;
2220 sas_device_priv_data = sdev->hostdata;
2221 if (!sas_device_priv_data)
2222 continue;
2223 if (sas_device_priv_data->sas_target->handle == handle) {
2224 sas_device_priv_data->sas_target->tm_busy = 0;
2225 skip = 1;
2226 ioc->ignore_loginfos = 0;
2227 }
2228 }
2229}
2230
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302231
Eric Moore635374e2009-03-09 01:21:12 -06002232/**
2233 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2234 * @ioc: per adapter struct
2235 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302236 * @channel: the channel assigned by the OS
2237 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002238 * @lun: lun number
2239 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2240 * @smid_task: smid assigned to the task
2241 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302242 * @serial_number: the serial_number from scmd
2243 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302244 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002245 *
2246 * A generic API for sending task management requests to firmware.
2247 *
Eric Moore635374e2009-03-09 01:21:12 -06002248 * The callback index is set inside `ioc->tm_cb_idx`.
2249 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302250 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002251 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302252int
2253mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2254 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302255 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002256{
2257 Mpi2SCSITaskManagementRequest_t *mpi_request;
2258 Mpi2SCSITaskManagementReply_t *mpi_reply;
2259 u16 smid = 0;
2260 u32 ioc_state;
2261 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302262 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302263 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002264
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302265 if (m_type == TM_MUTEX_ON)
2266 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302267 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2268 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2269 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302270 rc = FAILED;
2271 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302272 }
2273
Eric Moore3cb54692010-07-08 14:44:34 -06002274 if (ioc->shost_recovery || ioc->remove_host ||
2275 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002276 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2277 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302278 rc = FAILED;
2279 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002280 }
Eric Moore635374e2009-03-09 01:21:12 -06002281
2282 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2283 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302284 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002285 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302286 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302287 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302288 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302289 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002290 }
2291
2292 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2293 mpt2sas_base_fault_info(ioc, ioc_state &
2294 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302295 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302296 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302297 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302298 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002299 }
2300
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302301 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002302 if (!smid) {
2303 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2304 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302305 rc = FAILED;
2306 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002307 }
2308
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302309 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2310 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2311
Eric Moore635374e2009-03-09 01:21:12 -06002312 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302313 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2314 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002315 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2316 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2317 ioc->tm_cmds.smid = smid;
2318 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302319 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002320 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2321 mpi_request->DevHandle = cpu_to_le16(handle);
2322 mpi_request->TaskType = type;
2323 mpi_request->TaskMID = cpu_to_le16(smid_task);
2324 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2325 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302326 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302327 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002328 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002329 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2330 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2331 ioc->name, __func__);
2332 _debug_dump_mf(mpi_request,
2333 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302334 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302335 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302336 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302337 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302338 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2339 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2340 goto err_out;
2341 }
Eric Moore635374e2009-03-09 01:21:12 -06002342 }
2343
2344 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2345 mpi_reply = ioc->tm_cmds.reply;
2346 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2347 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2348 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2349 le32_to_cpu(mpi_reply->IOCLogInfo),
2350 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302351 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002352 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302353 if (mpi_reply->IOCStatus)
2354 _debug_dump_mf(mpi_request,
2355 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2356 }
Eric Moore635374e2009-03-09 01:21:12 -06002357 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302358
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302359 switch (type) {
2360 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302361 rc = SUCCESS;
2362 if (scsi_lookup->scmd == NULL)
2363 break;
2364 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302365 break;
2366
2367 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2368 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2369 rc = FAILED;
2370 else
2371 rc = SUCCESS;
2372 break;
2373
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302374 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2376 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2377 rc = FAILED;
2378 else
2379 rc = SUCCESS;
2380 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302381 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2382 rc = SUCCESS;
2383 break;
2384 default:
2385 rc = FAILED;
2386 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302387 }
2388
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302389 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2390 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302391 if (m_type == TM_MUTEX_ON)
2392 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302393
2394 return rc;
2395
2396 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302397 if (m_type == TM_MUTEX_ON)
2398 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302399 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002400}
2401
2402/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302403 * _scsih_tm_display_info - displays info about the device
2404 * @ioc: per adapter struct
2405 * @scmd: pointer to scsi command object
2406 *
2407 * Called by task management callback handlers.
2408 */
2409static void
2410_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2411{
2412 struct scsi_target *starget = scmd->device->sdev_target;
2413 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2414 struct _sas_device *sas_device = NULL;
2415 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302416 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302417
2418 if (!priv_target)
2419 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302420 if (ioc->hide_ir_msg)
2421 device_str = "WarpDrive";
2422 else
2423 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302424
2425 scsi_print_command(scmd);
2426 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302427 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2428 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2429 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302430 } else {
2431 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2432 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2433 priv_target->sas_address);
2434 if (sas_device) {
2435 if (priv_target->flags &
2436 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2437 starget_printk(KERN_INFO, starget,
2438 "volume handle(0x%04x), "
2439 "volume wwid(0x%016llx)\n",
2440 sas_device->volume_handle,
2441 (unsigned long long)sas_device->volume_wwid);
2442 }
2443 starget_printk(KERN_INFO, starget,
2444 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2445 sas_device->handle,
2446 (unsigned long long)sas_device->sas_address,
2447 sas_device->phy);
2448 starget_printk(KERN_INFO, starget,
2449 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2450 (unsigned long long)sas_device->enclosure_logical_id,
2451 sas_device->slot);
2452 }
2453 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2454 }
2455}
2456
2457/**
Eric Moored5d135b2009-05-18 13:02:08 -06002458 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302459 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002460 *
2461 * Returns SUCCESS if command aborted else FAILED
2462 */
2463static int
Eric Moored5d135b2009-05-18 13:02:08 -06002464_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002465{
2466 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2467 struct MPT2SAS_DEVICE *sas_device_priv_data;
2468 u16 smid;
2469 u16 handle;
2470 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002471
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302472 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2473 "scmd(%p)\n", scmd);
2474 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002475
2476 sas_device_priv_data = scmd->device->hostdata;
2477 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302478 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2479 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002480 scmd->result = DID_NO_CONNECT << 16;
2481 scmd->scsi_done(scmd);
2482 r = SUCCESS;
2483 goto out;
2484 }
2485
2486 /* search for the command */
2487 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2488 if (!smid) {
2489 scmd->result = DID_RESET << 16;
2490 r = SUCCESS;
2491 goto out;
2492 }
2493
2494 /* for hidden raid components and volumes this is not supported */
2495 if (sas_device_priv_data->sas_target->flags &
2496 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2497 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2498 scmd->result = DID_RESET << 16;
2499 r = FAILED;
2500 goto out;
2501 }
2502
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302503 mpt2sas_halt_firmware(ioc);
2504
Eric Moore635374e2009-03-09 01:21:12 -06002505 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302506 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2507 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302508 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2509 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002510
2511 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302512 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2513 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002514 return r;
2515}
2516
Eric Moore635374e2009-03-09 01:21:12 -06002517/**
Eric Moored5d135b2009-05-18 13:02:08 -06002518 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302519 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002520 *
2521 * Returns SUCCESS if command aborted else FAILED
2522 */
2523static int
Eric Moored5d135b2009-05-18 13:02:08 -06002524_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002525{
2526 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2527 struct MPT2SAS_DEVICE *sas_device_priv_data;
2528 struct _sas_device *sas_device;
2529 unsigned long flags;
2530 u16 handle;
2531 int r;
2532
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302533 struct scsi_target *starget = scmd->device->sdev_target;
2534
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302535 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302536 "scmd(%p)\n", scmd);
2537 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002538
2539 sas_device_priv_data = scmd->device->hostdata;
2540 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302541 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302542 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002543 scmd->result = DID_NO_CONNECT << 16;
2544 scmd->scsi_done(scmd);
2545 r = SUCCESS;
2546 goto out;
2547 }
2548
2549 /* for hidden raid components obtain the volume_handle */
2550 handle = 0;
2551 if (sas_device_priv_data->sas_target->flags &
2552 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2553 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2554 sas_device = _scsih_sas_device_find_by_handle(ioc,
2555 sas_device_priv_data->sas_target->handle);
2556 if (sas_device)
2557 handle = sas_device->volume_handle;
2558 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2559 } else
2560 handle = sas_device_priv_data->sas_target->handle;
2561
2562 if (!handle) {
2563 scmd->result = DID_RESET << 16;
2564 r = FAILED;
2565 goto out;
2566 }
2567
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302568 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2569 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302570 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2571 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002572
2573 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302574 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2575 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002576 return r;
2577}
2578
2579/**
Eric Moored5d135b2009-05-18 13:02:08 -06002580 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302581 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002582 *
2583 * Returns SUCCESS if command aborted else FAILED
2584 */
2585static int
Eric Moored5d135b2009-05-18 13:02:08 -06002586_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002587{
2588 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2589 struct MPT2SAS_DEVICE *sas_device_priv_data;
2590 struct _sas_device *sas_device;
2591 unsigned long flags;
2592 u16 handle;
2593 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302594 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002595
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302596 starget_printk(KERN_INFO, starget, "attempting target reset! "
2597 "scmd(%p)\n", scmd);
2598 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002599
2600 sas_device_priv_data = scmd->device->hostdata;
2601 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302602 starget_printk(KERN_INFO, starget, "target been deleted! "
2603 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002604 scmd->result = DID_NO_CONNECT << 16;
2605 scmd->scsi_done(scmd);
2606 r = SUCCESS;
2607 goto out;
2608 }
2609
2610 /* for hidden raid components obtain the volume_handle */
2611 handle = 0;
2612 if (sas_device_priv_data->sas_target->flags &
2613 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2614 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2615 sas_device = _scsih_sas_device_find_by_handle(ioc,
2616 sas_device_priv_data->sas_target->handle);
2617 if (sas_device)
2618 handle = sas_device->volume_handle;
2619 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2620 } else
2621 handle = sas_device_priv_data->sas_target->handle;
2622
2623 if (!handle) {
2624 scmd->result = DID_RESET << 16;
2625 r = FAILED;
2626 goto out;
2627 }
2628
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302629 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2630 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302631 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002632
2633 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302634 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2635 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002636 return r;
2637}
2638
2639/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302640 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302641 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002642 *
2643 * Returns SUCCESS if command aborted else FAILED
2644 */
2645static int
Eric Moored5d135b2009-05-18 13:02:08 -06002646_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002647{
2648 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2649 int r, retval;
2650
2651 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2652 ioc->name, scmd);
2653 scsi_print_command(scmd);
2654
2655 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2656 FORCE_BIG_HAMMER);
2657 r = (retval < 0) ? FAILED : SUCCESS;
2658 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2659 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2660
2661 return r;
2662}
2663
2664/**
2665 * _scsih_fw_event_add - insert and queue up fw_event
2666 * @ioc: per adapter object
2667 * @fw_event: object describing the event
2668 * Context: This function will acquire ioc->fw_event_lock.
2669 *
2670 * This adds the firmware event object into link list, then queues it up to
2671 * be processed from user context.
2672 *
2673 * Return nothing.
2674 */
2675static void
2676_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2677{
2678 unsigned long flags;
2679
2680 if (ioc->firmware_event_thread == NULL)
2681 return;
2682
2683 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2684 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302685 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2686 queue_delayed_work(ioc->firmware_event_thread,
2687 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002688 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2689}
2690
2691/**
2692 * _scsih_fw_event_free - delete fw_event
2693 * @ioc: per adapter object
2694 * @fw_event: object describing the event
2695 * Context: This function will acquire ioc->fw_event_lock.
2696 *
2697 * This removes firmware event object from link list, frees associated memory.
2698 *
2699 * Return nothing.
2700 */
2701static void
2702_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2703 *fw_event)
2704{
2705 unsigned long flags;
2706
2707 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2708 list_del(&fw_event->list);
2709 kfree(fw_event->event_data);
2710 kfree(fw_event);
2711 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2712}
2713
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302714
Eric Moore635374e2009-03-09 01:21:12 -06002715/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302716 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002717 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002718 *
2719 * Return nothing.
2720 */
2721static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302722_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002723{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302724 struct fw_event_work *fw_event;
2725
2726 if (ioc->wait_for_port_enable_to_complete)
2727 return;
2728 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2729 if (!fw_event)
2730 return;
2731 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2732 fw_event->ioc = ioc;
2733 _scsih_fw_event_add(ioc, fw_event);
2734}
2735
2736/**
2737 * _scsih_fw_event_cleanup_queue - cleanup event queue
2738 * @ioc: per adapter object
2739 *
2740 * Walk the firmware event queue, either killing timers, or waiting
2741 * for outstanding events to complete
2742 *
2743 * Return nothing.
2744 */
2745static void
2746_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2747{
2748 struct fw_event_work *fw_event, *next;
2749
2750 if (list_empty(&ioc->fw_event_list) ||
2751 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002752 return;
2753
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302754 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2755 if (cancel_delayed_work(&fw_event->delayed_work)) {
2756 _scsih_fw_event_free(ioc, fw_event);
2757 continue;
2758 }
2759 fw_event->cancel_pending_work = 1;
2760 }
Eric Moore635374e2009-03-09 01:21:12 -06002761}
2762
Eric Moore635374e2009-03-09 01:21:12 -06002763/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302764 * _scsih_ublock_io_all_device - unblock every device
2765 * @ioc: per adapter object
2766 *
2767 * change the device state from block to running
2768 */
2769static void
2770_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2771{
2772 struct MPT2SAS_DEVICE *sas_device_priv_data;
2773 struct scsi_device *sdev;
2774
2775 shost_for_each_device(sdev, ioc->shost) {
2776 sas_device_priv_data = sdev->hostdata;
2777 if (!sas_device_priv_data)
2778 continue;
2779 if (!sas_device_priv_data->block)
2780 continue;
2781 sas_device_priv_data->block = 0;
2782 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2783 "handle(0x%04x)\n",
2784 sas_device_priv_data->sas_target->handle));
2785 scsi_internal_device_unblock(sdev);
2786 }
2787}
2788/**
Eric Moore635374e2009-03-09 01:21:12 -06002789 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2790 * @ioc: per adapter object
2791 * @handle: device handle
2792 *
2793 * During device pull we need to appropiately set the sdev state.
2794 */
2795static void
2796_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2797{
2798 struct MPT2SAS_DEVICE *sas_device_priv_data;
2799 struct scsi_device *sdev;
2800
2801 shost_for_each_device(sdev, ioc->shost) {
2802 sas_device_priv_data = sdev->hostdata;
2803 if (!sas_device_priv_data)
2804 continue;
2805 if (!sas_device_priv_data->block)
2806 continue;
2807 if (sas_device_priv_data->sas_target->handle == handle) {
2808 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2809 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2810 "handle(0x%04x)\n", ioc->name, handle));
2811 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302812 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002813 }
2814 }
2815}
2816
2817/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302818 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2819 * @ioc: per adapter object
2820 * @handle: device handle
2821 *
2822 * During device pull we need to appropiately set the sdev state.
2823 */
2824static void
2825_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2826{
2827 struct MPT2SAS_DEVICE *sas_device_priv_data;
2828 struct scsi_device *sdev;
2829
2830 shost_for_each_device(sdev, ioc->shost) {
2831 sas_device_priv_data = sdev->hostdata;
2832 if (!sas_device_priv_data)
2833 continue;
2834 if (sas_device_priv_data->block)
2835 continue;
2836 sas_device_priv_data->block = 1;
2837 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2838 "handle(0x%04x)\n",
2839 sas_device_priv_data->sas_target->handle));
2840 scsi_internal_device_block(sdev);
2841 }
2842}
2843
2844
2845/**
Eric Moore635374e2009-03-09 01:21:12 -06002846 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2847 * @ioc: per adapter object
2848 * @handle: device handle
2849 *
2850 * During device pull we need to appropiately set the sdev state.
2851 */
2852static void
2853_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2854{
2855 struct MPT2SAS_DEVICE *sas_device_priv_data;
2856 struct scsi_device *sdev;
2857
2858 shost_for_each_device(sdev, ioc->shost) {
2859 sas_device_priv_data = sdev->hostdata;
2860 if (!sas_device_priv_data)
2861 continue;
2862 if (sas_device_priv_data->block)
2863 continue;
2864 if (sas_device_priv_data->sas_target->handle == handle) {
2865 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2866 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2867 "handle(0x%04x)\n", ioc->name, handle));
2868 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302869 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002870 }
2871 }
2872}
2873
2874/**
2875 * _scsih_block_io_to_children_attached_to_ex
2876 * @ioc: per adapter object
2877 * @sas_expander: the sas_device object
2878 *
2879 * This routine set sdev state to SDEV_BLOCK for all devices
2880 * attached to this expander. This function called when expander is
2881 * pulled.
2882 */
2883static void
2884_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2885 struct _sas_node *sas_expander)
2886{
2887 struct _sas_port *mpt2sas_port;
2888 struct _sas_device *sas_device;
2889 struct _sas_node *expander_sibling;
2890 unsigned long flags;
2891
2892 if (!sas_expander)
2893 return;
2894
2895 list_for_each_entry(mpt2sas_port,
2896 &sas_expander->sas_port_list, port_list) {
2897 if (mpt2sas_port->remote_identify.device_type ==
2898 SAS_END_DEVICE) {
2899 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2900 sas_device =
2901 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2902 mpt2sas_port->remote_identify.sas_address);
2903 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2904 if (!sas_device)
2905 continue;
2906 _scsih_block_io_device(ioc, sas_device->handle);
2907 }
2908 }
2909
2910 list_for_each_entry(mpt2sas_port,
2911 &sas_expander->sas_port_list, port_list) {
2912
2913 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302914 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06002915 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302916 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06002917
2918 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2919 expander_sibling =
2920 mpt2sas_scsih_expander_find_by_sas_address(
2921 ioc, mpt2sas_port->remote_identify.sas_address);
2922 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2923 _scsih_block_io_to_children_attached_to_ex(ioc,
2924 expander_sibling);
2925 }
2926 }
2927}
2928
2929/**
2930 * _scsih_block_io_to_children_attached_directly
2931 * @ioc: per adapter object
2932 * @event_data: topology change event data
2933 *
2934 * This routine set sdev state to SDEV_BLOCK for all devices
2935 * direct attached during device pull.
2936 */
2937static void
2938_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2939 Mpi2EventDataSasTopologyChangeList_t *event_data)
2940{
2941 int i;
2942 u16 handle;
2943 u16 reason_code;
2944 u8 phy_number;
2945
2946 for (i = 0; i < event_data->NumEntries; i++) {
2947 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2948 if (!handle)
2949 continue;
2950 phy_number = event_data->StartPhyNum + i;
2951 reason_code = event_data->PHY[i].PhyStatus &
2952 MPI2_EVENT_SAS_TOPO_RC_MASK;
2953 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2954 _scsih_block_io_device(ioc, handle);
2955 }
2956}
2957
2958/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302959 * _scsih_tm_tr_send - send task management request
2960 * @ioc: per adapter object
2961 * @handle: device handle
2962 * Context: interrupt time.
2963 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002964 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302965 * with controller firmware. This function will issue target reset
2966 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002967 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302968 *
2969 * This is designed to send muliple task management request at the same
2970 * time to the fifo. If the fifo is full, we will append the request,
2971 * and process it in a future completion.
2972 */
2973static void
2974_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2975{
2976 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302977 u16 smid;
2978 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302979 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302980 unsigned long flags;
2981 struct _tr_list *delayed_tr;
2982
Eric Moore3cb54692010-07-08 14:44:34 -06002983 if (ioc->shost_recovery || ioc->remove_host ||
2984 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302985 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2986 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302987 return;
2988 }
2989
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302990 /* if PD, then return */
2991 if (test_bit(handle, ioc->pd_handles))
2992 return;
2993
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302994 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2995 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302996 if (sas_device && sas_device->starget &&
2997 sas_device->starget->hostdata) {
2998 sas_target_priv_data = sas_device->starget->hostdata;
2999 sas_target_priv_data->deleted = 1;
3000 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3001 "setting delete flag: handle(0x%04x), "
3002 "sas_addr(0x%016llx)\n", ioc->name, handle,
3003 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303004 }
3005 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303006
3007 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3008 if (!smid) {
3009 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3010 if (!delayed_tr)
3011 return;
3012 INIT_LIST_HEAD(&delayed_tr->list);
3013 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303014 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3015 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3016 "DELAYED:tr:handle(0x%04x), (open)\n",
3017 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303018 return;
3019 }
3020
Kashyap, Desai1278b112010-03-09 17:34:13 +05303021 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3022 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3023 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303024 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3025 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3026 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3027 mpi_request->DevHandle = cpu_to_le16(handle);
3028 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303029 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3030}
3031
3032
3033
3034/**
3035 * _scsih_sas_control_complete - completion routine
3036 * @ioc: per adapter object
3037 * @smid: system request message index
3038 * @msix_index: MSIX table index supplied by the OS
3039 * @reply: reply message frame(lower 32bit addr)
3040 * Context: interrupt time.
3041 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003042 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303043 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003044 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303045 *
3046 * Return 1 meaning mf should be freed from _base_interrupt
3047 * 0 means the mf is freed from this function.
3048 */
3049static u8
3050_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3051 u8 msix_index, u32 reply)
3052{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303053#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303054 Mpi2SasIoUnitControlReply_t *mpi_reply =
3055 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303056#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303057 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3058 "sc_complete:handle(0x%04x), (open) "
3059 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3060 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3061 le16_to_cpu(mpi_reply->IOCStatus),
3062 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303063 return 1;
3064}
3065
3066/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303067 * _scsih_tm_tr_volume_send - send target reset request for volumes
3068 * @ioc: per adapter object
3069 * @handle: device handle
3070 * Context: interrupt time.
3071 *
3072 * This is designed to send muliple task management request at the same
3073 * time to the fifo. If the fifo is full, we will append the request,
3074 * and process it in a future completion.
3075 */
3076static void
3077_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3078{
3079 Mpi2SCSITaskManagementRequest_t *mpi_request;
3080 u16 smid;
3081 struct _tr_list *delayed_tr;
3082
Eric Moore3cb54692010-07-08 14:44:34 -06003083 if (ioc->shost_recovery || ioc->remove_host ||
3084 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303085 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3086 "progress!\n", __func__, ioc->name));
3087 return;
3088 }
3089
3090 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3091 if (!smid) {
3092 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3093 if (!delayed_tr)
3094 return;
3095 INIT_LIST_HEAD(&delayed_tr->list);
3096 delayed_tr->handle = handle;
3097 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3098 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3099 "DELAYED:tr:handle(0x%04x), (open)\n",
3100 ioc->name, handle));
3101 return;
3102 }
3103
3104 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3105 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3106 ioc->tm_tr_volume_cb_idx));
3107 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3108 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3109 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3110 mpi_request->DevHandle = cpu_to_le16(handle);
3111 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3112 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3113}
3114
3115/**
3116 * _scsih_tm_volume_tr_complete - target reset completion
3117 * @ioc: per adapter object
3118 * @smid: system request message index
3119 * @msix_index: MSIX table index supplied by the OS
3120 * @reply: reply message frame(lower 32bit addr)
3121 * Context: interrupt time.
3122 *
3123 * Return 1 meaning mf should be freed from _base_interrupt
3124 * 0 means the mf is freed from this function.
3125 */
3126static u8
3127_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3128 u8 msix_index, u32 reply)
3129{
3130 u16 handle;
3131 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3132 Mpi2SCSITaskManagementReply_t *mpi_reply =
3133 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3134
Eric Moore3cb54692010-07-08 14:44:34 -06003135 if (ioc->shost_recovery || ioc->remove_host ||
3136 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303137 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3138 "progress!\n", __func__, ioc->name));
3139 return 1;
3140 }
3141
3142 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3143 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3144 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3145 dewtprintk(ioc, printk("spurious interrupt: "
3146 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3147 le16_to_cpu(mpi_reply->DevHandle), smid));
3148 return 0;
3149 }
3150
3151 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3152 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3153 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3154 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3155 le32_to_cpu(mpi_reply->IOCLogInfo),
3156 le32_to_cpu(mpi_reply->TerminationCount)));
3157
3158 return _scsih_check_for_pending_tm(ioc, smid);
3159}
3160
3161/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303162 * _scsih_tm_tr_complete -
3163 * @ioc: per adapter object
3164 * @smid: system request message index
3165 * @msix_index: MSIX table index supplied by the OS
3166 * @reply: reply message frame(lower 32bit addr)
3167 * Context: interrupt time.
3168 *
3169 * This is the target reset completion routine.
3170 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003171 * handshake protocol with controller firmware.
3172 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303173 *
3174 * Return 1 meaning mf should be freed from _base_interrupt
3175 * 0 means the mf is freed from this function.
3176 */
3177static u8
3178_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3179 u32 reply)
3180{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303181 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303182 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303183 Mpi2SCSITaskManagementReply_t *mpi_reply =
3184 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3185 Mpi2SasIoUnitControlRequest_t *mpi_request;
3186 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303187
Eric Moore3cb54692010-07-08 14:44:34 -06003188 if (ioc->shost_recovery || ioc->remove_host ||
3189 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05303190 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3191 "progress!\n", __func__, ioc->name));
3192 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303193 }
3194
Kashyap, Desai1278b112010-03-09 17:34:13 +05303195 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3196 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3197 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3198 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3199 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3200 le16_to_cpu(mpi_reply->DevHandle), smid));
3201 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303202 }
3203
Kashyap, Desai1278b112010-03-09 17:34:13 +05303204 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3205 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3206 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3207 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3208 le32_to_cpu(mpi_reply->IOCLogInfo),
3209 le32_to_cpu(mpi_reply->TerminationCount)));
3210
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303211 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3212 if (!smid_sas_ctrl) {
3213 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3214 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303215 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303216 }
3217
Kashyap, Desai1278b112010-03-09 17:34:13 +05303218 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3219 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3220 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303221 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3222 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3223 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3224 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303225 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303226 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303227
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303228 return _scsih_check_for_pending_tm(ioc, smid);
3229}
3230
3231/**
3232 * _scsih_check_for_pending_tm - check for pending task management
3233 * @ioc: per adapter object
3234 * @smid: system request message index
3235 *
3236 * This will check delayed target reset list, and feed the
3237 * next reqeust.
3238 *
3239 * Return 1 meaning mf should be freed from _base_interrupt
3240 * 0 means the mf is freed from this function.
3241 */
3242static u8
3243_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3244{
3245 struct _tr_list *delayed_tr;
3246
3247 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3248 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3249 struct _tr_list, list);
3250 mpt2sas_base_free_smid(ioc, smid);
3251 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3252 list_del(&delayed_tr->list);
3253 kfree(delayed_tr);
3254 return 0;
3255 }
3256
Kashyap, Desai1278b112010-03-09 17:34:13 +05303257 if (!list_empty(&ioc->delayed_tr_list)) {
3258 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3259 struct _tr_list, list);
3260 mpt2sas_base_free_smid(ioc, smid);
3261 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3262 list_del(&delayed_tr->list);
3263 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303264 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303265 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303266
Kashyap, Desai1278b112010-03-09 17:34:13 +05303267 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303268}
3269
3270/**
Eric Moore635374e2009-03-09 01:21:12 -06003271 * _scsih_check_topo_delete_events - sanity check on topo events
3272 * @ioc: per adapter object
3273 * @event_data: the event data payload
3274 *
3275 * This routine added to better handle cable breaker.
3276 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003277 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003278 * add and delete events in a single shot. When there is a delete event
3279 * the routine will void any pending add events waiting in the event queue.
3280 *
3281 * Return nothing.
3282 */
3283static void
3284_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3285 Mpi2EventDataSasTopologyChangeList_t *event_data)
3286{
3287 struct fw_event_work *fw_event;
3288 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3289 u16 expander_handle;
3290 struct _sas_node *sas_expander;
3291 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303292 int i, reason_code;
3293 u16 handle;
3294
3295 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303296 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3297 if (!handle)
3298 continue;
3299 reason_code = event_data->PHY[i].PhyStatus &
3300 MPI2_EVENT_SAS_TOPO_RC_MASK;
3301 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3302 _scsih_tm_tr_send(ioc, handle);
3303 }
Eric Moore635374e2009-03-09 01:21:12 -06003304
3305 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3306 if (expander_handle < ioc->sas_hba.num_phys) {
3307 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3308 return;
3309 }
3310
3311 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3312 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3313 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3314 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3315 expander_handle);
3316 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3317 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3318 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3319 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3320
3321 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3322 return;
3323
3324 /* mark ignore flag for pending events */
3325 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3326 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3327 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3328 fw_event->ignore)
3329 continue;
3330 local_event_data = fw_event->event_data;
3331 if (local_event_data->ExpStatus ==
3332 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3333 local_event_data->ExpStatus ==
3334 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3335 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3336 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303337 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003338 "setting ignoring flag\n", ioc->name));
3339 fw_event->ignore = 1;
3340 }
3341 }
3342 }
3343 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3344}
3345
3346/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303347 * _scsih_set_volume_delete_flag - setting volume delete flag
3348 * @ioc: per adapter object
3349 * @handle: device handle
3350 *
3351 * This
3352 * Return nothing.
3353 */
3354static void
3355_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3356{
3357 struct _raid_device *raid_device;
3358 struct MPT2SAS_TARGET *sas_target_priv_data;
3359 unsigned long flags;
3360
3361 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3362 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3363 if (raid_device && raid_device->starget &&
3364 raid_device->starget->hostdata) {
3365 sas_target_priv_data =
3366 raid_device->starget->hostdata;
3367 sas_target_priv_data->deleted = 1;
3368 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3369 "setting delete flag: handle(0x%04x), "
3370 "wwid(0x%016llx)\n", ioc->name, handle,
3371 (unsigned long long) raid_device->wwid));
3372 }
3373 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3374}
3375
3376/**
3377 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3378 * @handle: input handle
3379 * @a: handle for volume a
3380 * @b: handle for volume b
3381 *
3382 * IR firmware only supports two raid volumes. The purpose of this
3383 * routine is to set the volume handle in either a or b. When the given
3384 * input handle is non-zero, or when a and b have not been set before.
3385 */
3386static void
3387_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3388{
3389 if (!handle || handle == *a || handle == *b)
3390 return;
3391 if (!*a)
3392 *a = handle;
3393 else if (!*b)
3394 *b = handle;
3395}
3396
3397/**
3398 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3399 * @ioc: per adapter object
3400 * @event_data: the event data payload
3401 * Context: interrupt time.
3402 *
3403 * This routine will send target reset to volume, followed by target
3404 * resets to the PDs. This is called when a PD has been removed, or
3405 * volume has been deleted or removed. When the target reset is sent
3406 * to volume, the PD target resets need to be queued to start upon
3407 * completion of the volume target reset.
3408 *
3409 * Return nothing.
3410 */
3411static void
3412_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3413 Mpi2EventDataIrConfigChangeList_t *event_data)
3414{
3415 Mpi2EventIrConfigElement_t *element;
3416 int i;
3417 u16 handle, volume_handle, a, b;
3418 struct _tr_list *delayed_tr;
3419
3420 a = 0;
3421 b = 0;
3422
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303423 if (ioc->is_warpdrive)
3424 return;
3425
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303426 /* Volume Resets for Deleted or Removed */
3427 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3428 for (i = 0; i < event_data->NumElements; i++, element++) {
3429 if (element->ReasonCode ==
3430 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3431 element->ReasonCode ==
3432 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3433 volume_handle = le16_to_cpu(element->VolDevHandle);
3434 _scsih_set_volume_delete_flag(ioc, volume_handle);
3435 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3436 }
3437 }
3438
3439 /* Volume Resets for UNHIDE events */
3440 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3441 for (i = 0; i < event_data->NumElements; i++, element++) {
3442 if (le32_to_cpu(event_data->Flags) &
3443 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3444 continue;
3445 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3446 volume_handle = le16_to_cpu(element->VolDevHandle);
3447 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3448 }
3449 }
3450
3451 if (a)
3452 _scsih_tm_tr_volume_send(ioc, a);
3453 if (b)
3454 _scsih_tm_tr_volume_send(ioc, b);
3455
3456 /* PD target resets */
3457 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3458 for (i = 0; i < event_data->NumElements; i++, element++) {
3459 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3460 continue;
3461 handle = le16_to_cpu(element->PhysDiskDevHandle);
3462 volume_handle = le16_to_cpu(element->VolDevHandle);
3463 clear_bit(handle, ioc->pd_handles);
3464 if (!volume_handle)
3465 _scsih_tm_tr_send(ioc, handle);
3466 else if (volume_handle == a || volume_handle == b) {
3467 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3468 BUG_ON(!delayed_tr);
3469 INIT_LIST_HEAD(&delayed_tr->list);
3470 delayed_tr->handle = handle;
3471 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3472 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3473 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3474 handle));
3475 } else
3476 _scsih_tm_tr_send(ioc, handle);
3477 }
3478}
3479
3480
3481/**
3482 * _scsih_check_volume_delete_events - set delete flag for volumes
3483 * @ioc: per adapter object
3484 * @event_data: the event data payload
3485 * Context: interrupt time.
3486 *
3487 * This will handle the case when the cable connected to entire volume is
3488 * pulled. We will take care of setting the deleted flag so normal IO will
3489 * not be sent.
3490 *
3491 * Return nothing.
3492 */
3493static void
3494_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3495 Mpi2EventDataIrVolume_t *event_data)
3496{
3497 u32 state;
3498
3499 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3500 return;
3501 state = le32_to_cpu(event_data->NewValue);
3502 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3503 MPI2_RAID_VOL_STATE_FAILED)
3504 _scsih_set_volume_delete_flag(ioc,
3505 le16_to_cpu(event_data->VolDevHandle));
3506}
3507
3508/**
Eric Moore635374e2009-03-09 01:21:12 -06003509 * _scsih_flush_running_cmds - completing outstanding commands.
3510 * @ioc: per adapter object
3511 *
3512 * The flushing out of all pending scmd commands following host reset,
3513 * where all IO is dropped to the floor.
3514 *
3515 * Return nothing.
3516 */
3517static void
3518_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3519{
3520 struct scsi_cmnd *scmd;
3521 u16 smid;
3522 u16 count = 0;
3523
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303524 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303525 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003526 if (!scmd)
3527 continue;
3528 count++;
3529 mpt2sas_base_free_smid(ioc, smid);
3530 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003531 if (ioc->pci_error_recovery)
3532 scmd->result = DID_NO_CONNECT << 16;
3533 else
3534 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003535 scmd->scsi_done(scmd);
3536 }
3537 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3538 ioc->name, count));
3539}
3540
3541/**
Eric Moore3c621b32009-05-18 12:59:41 -06003542 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3543 * @scmd: pointer to scsi command object
3544 * @mpi_request: pointer to the SCSI_IO reqest message frame
3545 *
3546 * Supporting protection 1 and 3.
3547 *
3548 * Returns nothing
3549 */
3550static void
3551_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3552{
3553 u16 eedp_flags;
3554 unsigned char prot_op = scsi_get_prot_op(scmd);
3555 unsigned char prot_type = scsi_get_prot_type(scmd);
3556
Eric Moored334aa72010-04-22 10:47:40 -06003557 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003558 return;
3559
3560 if (prot_op == SCSI_PROT_READ_STRIP)
3561 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3562 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3563 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3564 else
3565 return;
3566
Eric Moore3c621b32009-05-18 12:59:41 -06003567 switch (prot_type) {
3568 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003569 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003570
3571 /*
3572 * enable ref/guard checking
3573 * auto increment ref tag
3574 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303575 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003576 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3577 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3578 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3579 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003580 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003581
Eric Moore3c621b32009-05-18 12:59:41 -06003582 case SCSI_PROT_DIF_TYPE3:
3583
3584 /*
3585 * enable guard checking
3586 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303587 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003588 break;
3589 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303590 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3591 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003592}
3593
3594/**
3595 * _scsih_eedp_error_handling - return sense code for EEDP errors
3596 * @scmd: pointer to scsi command object
3597 * @ioc_status: ioc status
3598 *
3599 * Returns nothing
3600 */
3601static void
3602_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3603{
3604 u8 ascq;
3605 u8 sk;
3606 u8 host_byte;
3607
3608 switch (ioc_status) {
3609 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3610 ascq = 0x01;
3611 break;
3612 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3613 ascq = 0x02;
3614 break;
3615 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3616 ascq = 0x03;
3617 break;
3618 default:
3619 ascq = 0x00;
3620 break;
3621 }
3622
3623 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3624 sk = ILLEGAL_REQUEST;
3625 host_byte = DID_ABORT;
3626 } else {
3627 sk = ABORTED_COMMAND;
3628 host_byte = DID_OK;
3629 }
3630
3631 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3632 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3633 SAM_STAT_CHECK_CONDITION;
3634}
3635
3636/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303637 * _scsih_scsi_direct_io_get - returns direct io flag
3638 * @ioc: per adapter object
3639 * @smid: system request message index
3640 *
3641 * Returns the smid stored scmd pointer.
3642 */
3643static inline u8
3644_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3645{
3646 return ioc->scsi_lookup[smid - 1].direct_io;
3647}
3648
3649/**
3650 * _scsih_scsi_direct_io_set - sets direct io flag
3651 * @ioc: per adapter object
3652 * @smid: system request message index
3653 * @direct_io: Zero or non-zero value to set in the direct_io flag
3654 *
3655 * Returns Nothing.
3656 */
3657static inline void
3658_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3659{
3660 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3661}
3662
3663
3664/**
3665 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3666 * @ioc: per adapter object
3667 * @scmd: pointer to scsi command object
3668 * @raid_device: pointer to raid device data structure
3669 * @mpi_request: pointer to the SCSI_IO reqest message frame
3670 * @smid: system request message index
3671 *
3672 * Returns nothing
3673 */
3674static void
3675_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3676 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3677 u16 smid)
3678{
3679 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3680 u32 stripe_sz, stripe_exp;
3681 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3682 u8 cdb0 = scmd->cmnd[0];
3683
3684 /*
3685 * Try Direct I/O to RAID memeber disks
3686 */
3687 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3688 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3689 cdb_ptr = mpi_request->CDB.CDB32;
3690
3691 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3692 | cdb_ptr[5])) {
3693 io_size = scsi_bufflen(scmd) >> 9;
3694 /* get virtual lba */
3695 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3696 &cdb_ptr[6];
3697 tmp_ptr = (u8 *)&v_lba + 3;
3698 *tmp_ptr-- = *lba_ptr1++;
3699 *tmp_ptr-- = *lba_ptr1++;
3700 *tmp_ptr-- = *lba_ptr1++;
3701 *tmp_ptr = *lba_ptr1;
3702
3703 if (((u64)v_lba + (u64)io_size - 1) <=
3704 (u32)raid_device->max_lba) {
3705 stripe_sz = raid_device->stripe_sz;
3706 stripe_exp = raid_device->stripe_exponent;
3707 stripe_off = v_lba & (stripe_sz - 1);
3708
3709 /* Check whether IO falls within a stripe */
3710 if ((stripe_off + io_size) <= stripe_sz) {
3711 num_pds = raid_device->num_pds;
3712 p_lba = v_lba >> stripe_exp;
3713 stripe_unit = p_lba / num_pds;
3714 column = p_lba % num_pds;
3715 p_lba = (stripe_unit << stripe_exp) +
3716 stripe_off;
3717 mpi_request->DevHandle =
3718 cpu_to_le16(raid_device->
3719 pd_handle[column]);
3720 tmp_ptr = (u8 *)&p_lba + 3;
3721 *lba_ptr2++ = *tmp_ptr--;
3722 *lba_ptr2++ = *tmp_ptr--;
3723 *lba_ptr2++ = *tmp_ptr--;
3724 *lba_ptr2 = *tmp_ptr;
3725 /*
3726 * WD: To indicate this I/O is directI/O
3727 */
3728 _scsih_scsi_direct_io_set(ioc, smid, 1);
3729 }
3730 }
3731 }
3732 }
3733}
3734
3735/**
Eric Moored5d135b2009-05-18 13:02:08 -06003736 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003737 * @scmd: pointer to scsi command object
3738 * @done: function pointer to be invoked on completion
3739 *
3740 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3741 *
3742 * Returns 0 on success. If there's a failure, return either:
3743 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3744 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3745 */
3746static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003747_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003748{
3749 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3750 struct MPT2SAS_DEVICE *sas_device_priv_data;
3751 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303752 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003753 Mpi2SCSIIORequest_t *mpi_request;
3754 u32 mpi_control;
3755 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003756
3757 scmd->scsi_done = done;
3758 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303759 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003760 scmd->result = DID_NO_CONNECT << 16;
3761 scmd->scsi_done(scmd);
3762 return 0;
3763 }
3764
Kashyap, Desai78215782011-06-14 10:57:08 +05303765 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003766 scmd->result = DID_NO_CONNECT << 16;
3767 scmd->scsi_done(scmd);
3768 return 0;
3769 }
3770
Eric Moore635374e2009-03-09 01:21:12 -06003771 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303772 /* invalid device handle */
3773 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003774 scmd->result = DID_NO_CONNECT << 16;
3775 scmd->scsi_done(scmd);
3776 return 0;
3777 }
3778
Kashyap, Desai130b9582010-04-08 17:54:32 +05303779 /* host recovery or link resets sent via IOCTLs */
3780 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003781 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003782 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303783 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3784 return SCSI_MLQUEUE_DEVICE_BUSY;
3785 /* device has been deleted */
3786 else if (sas_target_priv_data->deleted) {
3787 scmd->result = DID_NO_CONNECT << 16;
3788 scmd->scsi_done(scmd);
3789 return 0;
3790 }
Eric Moore635374e2009-03-09 01:21:12 -06003791
3792 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3793 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3794 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3795 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3796 else
3797 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3798
3799 /* set tags */
3800 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3801 if (scmd->device->tagged_supported) {
3802 if (scmd->device->ordered_tags)
3803 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3804 else
3805 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3806 } else
3807/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3808/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3809 */
3810 mpi_control |= (0x500);
3811
3812 } else
3813 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303814 /* Make sure Device is not raid volume.
3815 * We do not expose raid functionality to upper layer for warpdrive.
3816 */
3817 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003818 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003819 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3820
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303821 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003822 if (!smid) {
3823 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3824 ioc->name, __func__);
3825 goto out;
3826 }
3827 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3828 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003829 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003830 if (scmd->cmd_len == 32)
3831 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003832 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3833 if (sas_device_priv_data->sas_target->flags &
3834 MPT_TARGET_FLAGS_RAID_COMPONENT)
3835 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3836 else
3837 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3838 mpi_request->DevHandle =
3839 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3840 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3841 mpi_request->Control = cpu_to_le32(mpi_control);
3842 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3843 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3844 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3845 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303846 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003847 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3848 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3849 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303850 mpi_request->VF_ID = 0; /* TODO */
3851 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003852 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3853 mpi_request->LUN);
3854 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3855
3856 if (!mpi_request->DataLength) {
3857 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3858 } else {
3859 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3860 mpt2sas_base_free_smid(ioc, smid);
3861 goto out;
3862 }
3863 }
3864
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303865 raid_device = sas_target_priv_data->raid_device;
3866 if (raid_device && raid_device->direct_io_enabled)
3867 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3868 smid);
3869
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303870 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3871 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303872 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303873 else
3874 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003875 return 0;
3876
3877 out:
3878 return SCSI_MLQUEUE_HOST_BUSY;
3879}
3880
Jeff Garzikf2812332010-11-16 02:10:29 -05003881static DEF_SCSI_QCMD(_scsih_qcmd)
3882
Eric Moore635374e2009-03-09 01:21:12 -06003883/**
3884 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3885 * @sense_buffer: sense data returned by target
3886 * @data: normalized skey/asc/ascq
3887 *
3888 * Return nothing.
3889 */
3890static void
3891_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3892{
3893 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3894 /* descriptor format */
3895 data->skey = sense_buffer[1] & 0x0F;
3896 data->asc = sense_buffer[2];
3897 data->ascq = sense_buffer[3];
3898 } else {
3899 /* fixed format */
3900 data->skey = sense_buffer[2] & 0x0F;
3901 data->asc = sense_buffer[12];
3902 data->ascq = sense_buffer[13];
3903 }
3904}
3905
3906#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3907/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003908 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003909 * @ioc: per adapter object
3910 * @scmd: pointer to scsi command object
3911 * @mpi_reply: reply mf payload returned from firmware
3912 *
3913 * scsi_status - SCSI Status code returned from target device
3914 * scsi_state - state info associated with SCSI_IO determined by ioc
3915 * ioc_status - ioc supplied status info
3916 *
3917 * Return nothing.
3918 */
3919static void
3920_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3921 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3922{
3923 u32 response_info;
3924 u8 *response_bytes;
3925 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3926 MPI2_IOCSTATUS_MASK;
3927 u8 scsi_state = mpi_reply->SCSIState;
3928 u8 scsi_status = mpi_reply->SCSIStatus;
3929 char *desc_ioc_state = NULL;
3930 char *desc_scsi_status = NULL;
3931 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303932 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303933 struct _sas_device *sas_device = NULL;
3934 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303935 struct scsi_target *starget = scmd->device->sdev_target;
3936 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303937 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303938
3939 if (!priv_target)
3940 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303941
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303942 if (ioc->hide_ir_msg)
3943 device_str = "WarpDrive";
3944 else
3945 device_str = "volume";
3946
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303947 if (log_info == 0x31170000)
3948 return;
Eric Moore635374e2009-03-09 01:21:12 -06003949
3950 switch (ioc_status) {
3951 case MPI2_IOCSTATUS_SUCCESS:
3952 desc_ioc_state = "success";
3953 break;
3954 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3955 desc_ioc_state = "invalid function";
3956 break;
3957 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3958 desc_ioc_state = "scsi recovered error";
3959 break;
3960 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3961 desc_ioc_state = "scsi invalid dev handle";
3962 break;
3963 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3964 desc_ioc_state = "scsi device not there";
3965 break;
3966 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3967 desc_ioc_state = "scsi data overrun";
3968 break;
3969 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3970 desc_ioc_state = "scsi data underrun";
3971 break;
3972 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3973 desc_ioc_state = "scsi io data error";
3974 break;
3975 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3976 desc_ioc_state = "scsi protocol error";
3977 break;
3978 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3979 desc_ioc_state = "scsi task terminated";
3980 break;
3981 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3982 desc_ioc_state = "scsi residual mismatch";
3983 break;
3984 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3985 desc_ioc_state = "scsi task mgmt failed";
3986 break;
3987 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3988 desc_ioc_state = "scsi ioc terminated";
3989 break;
3990 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3991 desc_ioc_state = "scsi ext terminated";
3992 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003993 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3994 desc_ioc_state = "eedp guard error";
3995 break;
3996 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3997 desc_ioc_state = "eedp ref tag error";
3998 break;
3999 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4000 desc_ioc_state = "eedp app tag error";
4001 break;
Eric Moore635374e2009-03-09 01:21:12 -06004002 default:
4003 desc_ioc_state = "unknown";
4004 break;
4005 }
4006
4007 switch (scsi_status) {
4008 case MPI2_SCSI_STATUS_GOOD:
4009 desc_scsi_status = "good";
4010 break;
4011 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4012 desc_scsi_status = "check condition";
4013 break;
4014 case MPI2_SCSI_STATUS_CONDITION_MET:
4015 desc_scsi_status = "condition met";
4016 break;
4017 case MPI2_SCSI_STATUS_BUSY:
4018 desc_scsi_status = "busy";
4019 break;
4020 case MPI2_SCSI_STATUS_INTERMEDIATE:
4021 desc_scsi_status = "intermediate";
4022 break;
4023 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4024 desc_scsi_status = "intermediate condmet";
4025 break;
4026 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4027 desc_scsi_status = "reservation conflict";
4028 break;
4029 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4030 desc_scsi_status = "command terminated";
4031 break;
4032 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4033 desc_scsi_status = "task set full";
4034 break;
4035 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4036 desc_scsi_status = "aca active";
4037 break;
4038 case MPI2_SCSI_STATUS_TASK_ABORTED:
4039 desc_scsi_status = "task aborted";
4040 break;
4041 default:
4042 desc_scsi_status = "unknown";
4043 break;
4044 }
4045
4046 desc_scsi_state[0] = '\0';
4047 if (!scsi_state)
4048 desc_scsi_state = " ";
4049 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4050 strcat(desc_scsi_state, "response info ");
4051 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4052 strcat(desc_scsi_state, "state terminated ");
4053 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4054 strcat(desc_scsi_state, "no status ");
4055 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4056 strcat(desc_scsi_state, "autosense failed ");
4057 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4058 strcat(desc_scsi_state, "autosense valid ");
4059
4060 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304061
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304062 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304063 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4064 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304065 } else {
4066 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4067 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4068 priv_target->sas_address);
4069 if (sas_device) {
4070 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4071 "phy(%d)\n", ioc->name, sas_device->sas_address,
4072 sas_device->phy);
4073 printk(MPT2SAS_WARN_FMT
4074 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4075 ioc->name, sas_device->enclosure_logical_id,
4076 sas_device->slot);
4077 }
4078 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304079 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304080
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304081 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4082 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4083 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004084 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4085 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4086 scsi_get_resid(scmd));
4087 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4088 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4089 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4090 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4091 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4092 scsi_status, desc_scsi_state, scsi_state);
4093
4094 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4095 struct sense_info data;
4096 _scsih_normalize_sense(scmd->sense_buffer, &data);
4097 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304098 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4099 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004100 }
4101
4102 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4103 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4104 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304105 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004106 }
4107}
4108#endif
4109
4110/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304111 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004112 * @ioc: per adapter object
4113 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304114 * Context: process
4115 *
4116 * Return nothing.
4117 */
4118static void
4119_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4120{
4121 Mpi2SepReply_t mpi_reply;
4122 Mpi2SepRequest_t mpi_request;
4123
4124 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4125 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4126 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4127 mpi_request.SlotStatus =
4128 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4129 mpi_request.DevHandle = cpu_to_le16(handle);
4130 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4131 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4132 &mpi_request)) != 0) {
4133 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4134 __FILE__, __LINE__, __func__);
4135 return;
4136 }
4137
4138 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4139 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4140 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4141 le16_to_cpu(mpi_reply.IOCStatus),
4142 le32_to_cpu(mpi_reply.IOCLogInfo)));
4143 return;
4144 }
4145}
4146
4147/**
4148 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4149 * @ioc: per adapter object
4150 * @handle: device handle
4151 * Context: interrupt.
4152 *
4153 * Return nothing.
4154 */
4155static void
4156_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4157{
4158 struct fw_event_work *fw_event;
4159
4160 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4161 if (!fw_event)
4162 return;
4163 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4164 fw_event->device_handle = handle;
4165 fw_event->ioc = ioc;
4166 _scsih_fw_event_add(ioc, fw_event);
4167}
4168
4169/**
4170 * _scsih_smart_predicted_fault - process smart errors
4171 * @ioc: per adapter object
4172 * @handle: device handle
4173 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004174 *
4175 * Return nothing.
4176 */
4177static void
4178_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4179{
Eric Moore635374e2009-03-09 01:21:12 -06004180 struct scsi_target *starget;
4181 struct MPT2SAS_TARGET *sas_target_priv_data;
4182 Mpi2EventNotificationReply_t *event_reply;
4183 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4184 struct _sas_device *sas_device;
4185 ssize_t sz;
4186 unsigned long flags;
4187
4188 /* only handle non-raid devices */
4189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4190 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4191 if (!sas_device) {
4192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4193 return;
4194 }
4195 starget = sas_device->starget;
4196 sas_target_priv_data = starget->hostdata;
4197
4198 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4199 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4200 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4201 return;
4202 }
4203 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4204 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4205
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304206 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4207 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004208
4209 /* insert into event log */
4210 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4211 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4212 event_reply = kzalloc(sz, GFP_KERNEL);
4213 if (!event_reply) {
4214 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4215 ioc->name, __FILE__, __LINE__, __func__);
4216 return;
4217 }
4218
4219 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4220 event_reply->Event =
4221 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4222 event_reply->MsgLength = sz/4;
4223 event_reply->EventDataLength =
4224 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4225 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4226 event_reply->EventData;
4227 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4228 event_data->ASC = 0x5D;
4229 event_data->DevHandle = cpu_to_le16(handle);
4230 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4231 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4232 kfree(event_reply);
4233}
4234
4235/**
Eric Moored5d135b2009-05-18 13:02:08 -06004236 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004237 * @ioc: per adapter object
4238 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304239 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004240 * @reply: reply message frame(lower 32bit addr)
4241 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304242 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004243 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304244 * Return 1 meaning mf should be freed from _base_interrupt
4245 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004246 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304247static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304248_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004249{
4250 Mpi2SCSIIORequest_t *mpi_request;
4251 Mpi2SCSIIOReply_t *mpi_reply;
4252 struct scsi_cmnd *scmd;
4253 u16 ioc_status;
4254 u32 xfer_cnt;
4255 u8 scsi_state;
4256 u8 scsi_status;
4257 u32 log_info;
4258 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304259 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304260 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004261
4262 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304263 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004264 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304265 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004266
4267 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4268
4269 if (mpi_reply == NULL) {
4270 scmd->result = DID_OK << 16;
4271 goto out;
4272 }
4273
4274 sas_device_priv_data = scmd->device->hostdata;
4275 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4276 sas_device_priv_data->sas_target->deleted) {
4277 scmd->result = DID_NO_CONNECT << 16;
4278 goto out;
4279 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304280 /*
4281 * WARPDRIVE: If direct_io is set then it is directIO,
4282 * the failed direct I/O should be redirected to volume
4283 */
4284 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304285 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4286 ioc->scsi_lookup[smid - 1].scmd = scmd;
4287 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304288 _scsih_scsi_direct_io_set(ioc, smid, 0);
4289 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4290 mpi_request->DevHandle =
4291 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4292 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4293 sas_device_priv_data->sas_target->handle);
4294 return 0;
4295 }
4296
Eric Moore635374e2009-03-09 01:21:12 -06004297
4298 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304299 scsi_state = mpi_reply->SCSIState;
4300 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4301 response_code =
4302 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004303 if (!sas_device_priv_data->tlr_snoop_check) {
4304 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304305 /* Make sure Device is not raid volume.
4306 * We do not expose raid functionality to upper layer for warpdrive.
4307 */
4308 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304309 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304310 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4311 sas_disable_tlr(scmd->device);
4312 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4313 }
Eric Moore635374e2009-03-09 01:21:12 -06004314 }
4315
4316 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4317 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4318 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4319 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4320 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4321 else
4322 log_info = 0;
4323 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004324 scsi_status = mpi_reply->SCSIStatus;
4325
4326 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4327 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4328 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4329 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4330 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4331 }
4332
4333 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4334 struct sense_info data;
4335 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4336 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004337 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004338 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004339 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004340 _scsih_normalize_sense(scmd->sense_buffer, &data);
4341 /* failure prediction threshold exceeded */
4342 if (data.asc == 0x5D)
4343 _scsih_smart_predicted_fault(ioc,
4344 le16_to_cpu(mpi_reply->DevHandle));
4345 }
4346
4347 switch (ioc_status) {
4348 case MPI2_IOCSTATUS_BUSY:
4349 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4350 scmd->result = SAM_STAT_BUSY;
4351 break;
4352
4353 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4354 scmd->result = DID_NO_CONNECT << 16;
4355 break;
4356
4357 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4358 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304359 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4360 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004361 }
Eric Moore635374e2009-03-09 01:21:12 -06004362 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4363 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4364 scmd->result = DID_RESET << 16;
4365 break;
4366
4367 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4368 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4369 scmd->result = DID_SOFT_ERROR << 16;
4370 else
4371 scmd->result = (DID_OK << 16) | scsi_status;
4372 break;
4373
4374 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4375 scmd->result = (DID_OK << 16) | scsi_status;
4376
4377 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4378 break;
4379
4380 if (xfer_cnt < scmd->underflow) {
4381 if (scsi_status == SAM_STAT_BUSY)
4382 scmd->result = SAM_STAT_BUSY;
4383 else
4384 scmd->result = DID_SOFT_ERROR << 16;
4385 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4386 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4387 scmd->result = DID_SOFT_ERROR << 16;
4388 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4389 scmd->result = DID_RESET << 16;
4390 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4391 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4392 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4393 scmd->result = (DRIVER_SENSE << 24) |
4394 SAM_STAT_CHECK_CONDITION;
4395 scmd->sense_buffer[0] = 0x70;
4396 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4397 scmd->sense_buffer[12] = 0x20;
4398 scmd->sense_buffer[13] = 0;
4399 }
4400 break;
4401
4402 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4403 scsi_set_resid(scmd, 0);
4404 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4405 case MPI2_IOCSTATUS_SUCCESS:
4406 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304407 if (response_code ==
4408 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4409 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4410 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004411 scmd->result = DID_SOFT_ERROR << 16;
4412 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4413 scmd->result = DID_RESET << 16;
4414 break;
4415
Eric Moore3c621b32009-05-18 12:59:41 -06004416 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4417 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4418 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4419 _scsih_eedp_error_handling(scmd, ioc_status);
4420 break;
Eric Moore635374e2009-03-09 01:21:12 -06004421 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4422 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4423 case MPI2_IOCSTATUS_INVALID_SGL:
4424 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4425 case MPI2_IOCSTATUS_INVALID_FIELD:
4426 case MPI2_IOCSTATUS_INVALID_STATE:
4427 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4428 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4429 default:
4430 scmd->result = DID_SOFT_ERROR << 16;
4431 break;
4432
4433 }
4434
4435#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4436 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4437 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4438#endif
4439
4440 out:
4441 scsi_dma_unmap(scmd);
4442 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304443 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004444}
4445
4446/**
Eric Moore635374e2009-03-09 01:21:12 -06004447 * _scsih_sas_host_refresh - refreshing sas host object contents
4448 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004449 * Context: user
4450 *
4451 * During port enable, fw will send topology events for every device. Its
4452 * possible that the handles may change from the previous setting, so this
4453 * code keeping handles updating if changed.
4454 *
4455 * Return nothing.
4456 */
4457static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304458_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004459{
4460 u16 sz;
4461 u16 ioc_status;
4462 int i;
4463 Mpi2ConfigReply_t mpi_reply;
4464 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304465 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304466 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004467
4468 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4469 "updating handles for sas_host(0x%016llx)\n",
4470 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4471
4472 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4473 * sizeof(Mpi2SasIOUnit0PhyData_t));
4474 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4475 if (!sas_iounit_pg0) {
4476 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4477 ioc->name, __FILE__, __LINE__, __func__);
4478 return;
4479 }
Eric Moore635374e2009-03-09 01:21:12 -06004480
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304481 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4482 sas_iounit_pg0, sz)) != 0)
4483 goto out;
4484 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4485 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4486 goto out;
4487 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304488 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304489 if (i == 0)
4490 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4491 PhyData[0].ControllerDevHandle);
4492 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4493 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4494 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304495 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4496 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304497 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304498 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304499 }
Eric Moore635374e2009-03-09 01:21:12 -06004500 out:
4501 kfree(sas_iounit_pg0);
4502}
4503
4504/**
4505 * _scsih_sas_host_add - create sas host object
4506 * @ioc: per adapter object
4507 *
4508 * Creating host side data object, stored in ioc->sas_hba
4509 *
4510 * Return nothing.
4511 */
4512static void
4513_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4514{
4515 int i;
4516 Mpi2ConfigReply_t mpi_reply;
4517 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4518 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4519 Mpi2SasPhyPage0_t phy_pg0;
4520 Mpi2SasDevicePage0_t sas_device_pg0;
4521 Mpi2SasEnclosurePage0_t enclosure_pg0;
4522 u16 ioc_status;
4523 u16 sz;
4524 u16 device_missing_delay;
4525
4526 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4527 if (!ioc->sas_hba.num_phys) {
4528 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4529 ioc->name, __FILE__, __LINE__, __func__);
4530 return;
4531 }
4532
4533 /* sas_iounit page 0 */
4534 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4535 sizeof(Mpi2SasIOUnit0PhyData_t));
4536 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4537 if (!sas_iounit_pg0) {
4538 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4539 ioc->name, __FILE__, __LINE__, __func__);
4540 return;
4541 }
4542 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4543 sas_iounit_pg0, sz))) {
4544 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4545 ioc->name, __FILE__, __LINE__, __func__);
4546 goto out;
4547 }
4548 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4549 MPI2_IOCSTATUS_MASK;
4550 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4551 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4552 ioc->name, __FILE__, __LINE__, __func__);
4553 goto out;
4554 }
4555
4556 /* sas_iounit page 1 */
4557 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4558 sizeof(Mpi2SasIOUnit1PhyData_t));
4559 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4560 if (!sas_iounit_pg1) {
4561 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4562 ioc->name, __FILE__, __LINE__, __func__);
4563 goto out;
4564 }
4565 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4566 sas_iounit_pg1, sz))) {
4567 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4568 ioc->name, __FILE__, __LINE__, __func__);
4569 goto out;
4570 }
4571 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4572 MPI2_IOCSTATUS_MASK;
4573 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4574 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4575 ioc->name, __FILE__, __LINE__, __func__);
4576 goto out;
4577 }
4578
4579 ioc->io_missing_delay =
4580 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4581 device_missing_delay =
4582 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4583 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4584 ioc->device_missing_delay = (device_missing_delay &
4585 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4586 else
4587 ioc->device_missing_delay = device_missing_delay &
4588 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4589
4590 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4591 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4592 sizeof(struct _sas_phy), GFP_KERNEL);
4593 if (!ioc->sas_hba.phy) {
4594 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4595 ioc->name, __FILE__, __LINE__, __func__);
4596 goto out;
4597 }
4598 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4599 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4600 i))) {
4601 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4602 ioc->name, __FILE__, __LINE__, __func__);
4603 goto out;
4604 }
4605 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4606 MPI2_IOCSTATUS_MASK;
4607 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4608 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4609 ioc->name, __FILE__, __LINE__, __func__);
4610 goto out;
4611 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304612
4613 if (i == 0)
4614 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4615 PhyData[0].ControllerDevHandle);
4616 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004617 ioc->sas_hba.phy[i].phy_id = i;
4618 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4619 phy_pg0, ioc->sas_hba.parent_dev);
4620 }
4621 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304622 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004623 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4624 ioc->name, __FILE__, __LINE__, __func__);
4625 goto out;
4626 }
Eric Moore635374e2009-03-09 01:21:12 -06004627 ioc->sas_hba.enclosure_handle =
4628 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4629 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4630 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4631 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4632 (unsigned long long) ioc->sas_hba.sas_address,
4633 ioc->sas_hba.num_phys) ;
4634
4635 if (ioc->sas_hba.enclosure_handle) {
4636 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4637 &enclosure_pg0,
4638 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4639 ioc->sas_hba.enclosure_handle))) {
4640 ioc->sas_hba.enclosure_logical_id =
4641 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4642 }
4643 }
4644
4645 out:
4646 kfree(sas_iounit_pg1);
4647 kfree(sas_iounit_pg0);
4648}
4649
4650/**
4651 * _scsih_expander_add - creating expander object
4652 * @ioc: per adapter object
4653 * @handle: expander handle
4654 *
4655 * Creating expander object, stored in ioc->sas_expander_list.
4656 *
4657 * Return 0 for success, else error.
4658 */
4659static int
4660_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4661{
4662 struct _sas_node *sas_expander;
4663 Mpi2ConfigReply_t mpi_reply;
4664 Mpi2ExpanderPage0_t expander_pg0;
4665 Mpi2ExpanderPage1_t expander_pg1;
4666 Mpi2SasEnclosurePage0_t enclosure_pg0;
4667 u32 ioc_status;
4668 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304669 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004670 int i;
4671 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304672 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004673 int rc = 0;
4674
4675 if (!handle)
4676 return -1;
4677
Eric Moore3cb54692010-07-08 14:44:34 -06004678 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304679 return -1;
4680
Eric Moore635374e2009-03-09 01:21:12 -06004681 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4682 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4684 ioc->name, __FILE__, __LINE__, __func__);
4685 return -1;
4686 }
4687
4688 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4689 MPI2_IOCSTATUS_MASK;
4690 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4691 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4692 ioc->name, __FILE__, __LINE__, __func__);
4693 return -1;
4694 }
4695
4696 /* handle out of order topology events */
4697 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304698 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4699 != 0) {
4700 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4701 ioc->name, __FILE__, __LINE__, __func__);
4702 return -1;
4703 }
4704 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004705 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304706 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4707 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004708 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4709 if (!sas_expander) {
4710 rc = _scsih_expander_add(ioc, parent_handle);
4711 if (rc != 0)
4712 return rc;
4713 }
4714 }
4715
Eric Moore635374e2009-03-09 01:21:12 -06004716 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304717 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004718 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4719 sas_address);
4720 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4721
4722 if (sas_expander)
4723 return 0;
4724
4725 sas_expander = kzalloc(sizeof(struct _sas_node),
4726 GFP_KERNEL);
4727 if (!sas_expander) {
4728 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4729 ioc->name, __FILE__, __LINE__, __func__);
4730 return -1;
4731 }
4732
4733 sas_expander->handle = handle;
4734 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304735 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004736 sas_expander->sas_address = sas_address;
4737
4738 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4739 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304740 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004741 sas_expander->sas_address, sas_expander->num_phys);
4742
4743 if (!sas_expander->num_phys)
4744 goto out_fail;
4745 sas_expander->phy = kcalloc(sas_expander->num_phys,
4746 sizeof(struct _sas_phy), GFP_KERNEL);
4747 if (!sas_expander->phy) {
4748 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4749 ioc->name, __FILE__, __LINE__, __func__);
4750 rc = -1;
4751 goto out_fail;
4752 }
4753
4754 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4755 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304756 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004757 if (!mpt2sas_port) {
4758 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4759 ioc->name, __FILE__, __LINE__, __func__);
4760 rc = -1;
4761 goto out_fail;
4762 }
4763 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4764
4765 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4766 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4767 &expander_pg1, i, handle))) {
4768 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4769 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304770 rc = -1;
4771 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004772 }
4773 sas_expander->phy[i].handle = handle;
4774 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304775
4776 if ((mpt2sas_transport_add_expander_phy(ioc,
4777 &sas_expander->phy[i], expander_pg1,
4778 sas_expander->parent_dev))) {
4779 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4780 ioc->name, __FILE__, __LINE__, __func__);
4781 rc = -1;
4782 goto out_fail;
4783 }
Eric Moore635374e2009-03-09 01:21:12 -06004784 }
4785
4786 if (sas_expander->enclosure_handle) {
4787 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4788 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4789 sas_expander->enclosure_handle))) {
4790 sas_expander->enclosure_logical_id =
4791 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4792 }
4793 }
4794
4795 _scsih_expander_node_add(ioc, sas_expander);
4796 return 0;
4797
4798 out_fail:
4799
Kashyap, Desai20f58952009-08-07 19:34:26 +05304800 if (mpt2sas_port)
4801 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304802 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004803 kfree(sas_expander);
4804 return rc;
4805}
4806
4807/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304808 * _scsih_done - scsih callback handler.
4809 * @ioc: per adapter object
4810 * @smid: system request message index
4811 * @msix_index: MSIX table index supplied by the OS
4812 * @reply: reply message frame(lower 32bit addr)
4813 *
4814 * Callback handler when sending internal generated message frames.
4815 * The callback index passed is `ioc->scsih_cb_idx`
4816 *
4817 * Return 1 meaning mf should be freed from _base_interrupt
4818 * 0 means the mf is freed from this function.
4819 */
4820static u8
4821_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4822{
4823 MPI2DefaultReply_t *mpi_reply;
4824
4825 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4826 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4827 return 1;
4828 if (ioc->scsih_cmds.smid != smid)
4829 return 1;
4830 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4831 if (mpi_reply) {
4832 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4833 mpi_reply->MsgLength*4);
4834 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4835 }
4836 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4837 complete(&ioc->scsih_cmds.done);
4838 return 1;
4839}
4840
4841/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304842 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004843 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304844 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004845 *
4846 * Return nothing.
4847 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304848void
4849mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004850{
4851 struct _sas_node *sas_expander;
4852 unsigned long flags;
4853
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304854 if (ioc->shost_recovery)
4855 return;
4856
Eric Moore635374e2009-03-09 01:21:12 -06004857 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304858 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4859 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304860 if (!sas_expander) {
4861 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4862 return;
4863 }
4864 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004865 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4866 _scsih_expander_node_remove(ioc, sas_expander);
4867}
4868
4869/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304870 * _scsih_check_access_status - check access flags
4871 * @ioc: per adapter object
4872 * @sas_address: sas address
4873 * @handle: sas device handle
4874 * @access_flags: errors returned during discovery of the device
4875 *
4876 * Return 0 for success, else failure
4877 */
4878static u8
4879_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4880 u16 handle, u8 access_status)
4881{
4882 u8 rc = 1;
4883 char *desc = NULL;
4884
4885 switch (access_status) {
4886 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4887 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4888 rc = 0;
4889 break;
4890 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4891 desc = "sata capability failed";
4892 break;
4893 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4894 desc = "sata affiliation conflict";
4895 break;
4896 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4897 desc = "route not addressable";
4898 break;
4899 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4900 desc = "smp error not addressable";
4901 break;
4902 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4903 desc = "device blocked";
4904 break;
4905 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4906 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4907 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4908 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4909 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4910 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4911 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4912 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4913 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4914 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4915 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4916 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4917 desc = "sata initialization failed";
4918 break;
4919 default:
4920 desc = "unknown";
4921 break;
4922 }
4923
4924 if (!rc)
4925 return 0;
4926
4927 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4928 "handle(0x%04x)\n", ioc->name, desc,
4929 (unsigned long long)sas_address, handle);
4930 return rc;
4931}
4932
4933static void
4934_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4935{
4936 Mpi2ConfigReply_t mpi_reply;
4937 Mpi2SasDevicePage0_t sas_device_pg0;
4938 struct _sas_device *sas_device;
4939 u32 ioc_status;
4940 unsigned long flags;
4941 u64 sas_address;
4942 struct scsi_target *starget;
4943 struct MPT2SAS_TARGET *sas_target_priv_data;
4944 u32 device_info;
4945
4946 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4947 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4948 return;
4949
4950 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4951 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4952 return;
4953
4954 /* check if this is end device */
4955 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4956 if (!(_scsih_is_end_device(device_info)))
4957 return;
4958
4959 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4960 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4961 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4962 sas_address);
4963
4964 if (!sas_device) {
4965 printk(MPT2SAS_ERR_FMT "device is not present "
4966 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4967 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4968 return;
4969 }
4970
4971 if (unlikely(sas_device->handle != handle)) {
4972 starget = sas_device->starget;
4973 sas_target_priv_data = starget->hostdata;
4974 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4975 " to (0x%04x)!!!\n", sas_device->handle, handle);
4976 sas_target_priv_data->handle = handle;
4977 sas_device->handle = handle;
4978 }
4979 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4980
4981 /* check if device is present */
4982 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4983 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4984 printk(MPT2SAS_ERR_FMT "device is not present "
4985 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4986 return;
4987 }
4988
4989 /* check if there were any issues with discovery */
4990 if (_scsih_check_access_status(ioc, sas_address, handle,
4991 sas_device_pg0.AccessStatus))
4992 return;
4993 _scsih_ublock_io_device(ioc, handle);
4994
4995}
4996
4997/**
Eric Moore635374e2009-03-09 01:21:12 -06004998 * _scsih_add_device - creating sas device object
4999 * @ioc: per adapter object
5000 * @handle: sas device handle
5001 * @phy_num: phy number end device attached to
5002 * @is_pd: is this hidden raid component
5003 *
5004 * Creating end device object, stored in ioc->sas_device_list.
5005 *
5006 * Returns 0 for success, non-zero for failure.
5007 */
5008static int
5009_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5010{
5011 Mpi2ConfigReply_t mpi_reply;
5012 Mpi2SasDevicePage0_t sas_device_pg0;
5013 Mpi2SasEnclosurePage0_t enclosure_pg0;
5014 struct _sas_device *sas_device;
5015 u32 ioc_status;
5016 __le64 sas_address;
5017 u32 device_info;
5018 unsigned long flags;
5019
5020 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5021 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5022 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5023 ioc->name, __FILE__, __LINE__, __func__);
5024 return -1;
5025 }
5026
5027 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5028 MPI2_IOCSTATUS_MASK;
5029 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5030 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5031 ioc->name, __FILE__, __LINE__, __func__);
5032 return -1;
5033 }
5034
Kashyap, Desaib4344272010-03-17 16:24:14 +05305035 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5036
Eric Moore635374e2009-03-09 01:21:12 -06005037 /* check if device is present */
5038 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5039 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5040 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5041 ioc->name, __FILE__, __LINE__, __func__);
5042 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5043 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5044 return -1;
5045 }
5046
Kashyap, Desaib4344272010-03-17 16:24:14 +05305047 /* check if there were any issues with discovery */
5048 if (_scsih_check_access_status(ioc, sas_address, handle,
5049 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005050 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005051
5052 /* check if this is end device */
5053 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5054 if (!(_scsih_is_end_device(device_info))) {
5055 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5056 ioc->name, __FILE__, __LINE__, __func__);
5057 return -1;
5058 }
5059
Eric Moore635374e2009-03-09 01:21:12 -06005060
5061 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5062 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5063 sas_address);
5064 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5065
Kashyap, Desaib4344272010-03-17 16:24:14 +05305066 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005067 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005068
5069 sas_device = kzalloc(sizeof(struct _sas_device),
5070 GFP_KERNEL);
5071 if (!sas_device) {
5072 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5073 ioc->name, __FILE__, __LINE__, __func__);
5074 return -1;
5075 }
5076
5077 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305078 if (_scsih_get_sas_address(ioc, le16_to_cpu
5079 (sas_device_pg0.ParentDevHandle),
5080 &sas_device->sas_address_parent) != 0)
5081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5082 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005083 sas_device->enclosure_handle =
5084 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5085 sas_device->slot =
5086 le16_to_cpu(sas_device_pg0.Slot);
5087 sas_device->device_info = device_info;
5088 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305089 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005090
5091 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305092 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5093 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5094 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005095 sas_device->enclosure_logical_id =
5096 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005097
5098 /* get device name */
5099 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5100
5101 if (ioc->wait_for_port_enable_to_complete)
5102 _scsih_sas_device_init_add(ioc, sas_device);
5103 else
5104 _scsih_sas_device_add(ioc, sas_device);
5105
5106 return 0;
5107}
5108
5109/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305110 * _scsih_remove_device - removing sas device object
5111 * @ioc: per adapter object
5112 * @sas_device_delete: the sas_device object
5113 *
5114 * Return nothing.
5115 */
5116static void
5117_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5118 struct _sas_device *sas_device)
5119{
5120 struct _sas_device sas_device_backup;
5121 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305122
Kashyap, Desai1278b112010-03-09 17:34:13 +05305123 if (!sas_device)
5124 return;
Eric Moore635374e2009-03-09 01:21:12 -06005125
Kashyap, Desai1278b112010-03-09 17:34:13 +05305126 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005127 _scsih_sas_device_remove(ioc, sas_device);
5128
Kashyap, Desai1278b112010-03-09 17:34:13 +05305129 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5130 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5131 sas_device_backup.handle, (unsigned long long)
5132 sas_device_backup.sas_address));
5133
5134 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5135 sas_target_priv_data = sas_device_backup.starget->hostdata;
5136 sas_target_priv_data->deleted = 1;
5137 }
5138
Kashyap, Desai1278b112010-03-09 17:34:13 +05305139 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5140
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305141 if (!ioc->hide_drives)
5142 mpt2sas_transport_port_remove(ioc,
5143 sas_device_backup.sas_address,
5144 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305145
5146 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5147 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5148 (unsigned long long) sas_device_backup.sas_address);
5149
5150 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5151 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5152 sas_device_backup.handle, (unsigned long long)
5153 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005154}
5155
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305156/**
5157 * mpt2sas_device_remove - removing device object
5158 * @ioc: per adapter object
5159 * @sas_address: expander sas_address
5160 *
5161 * Return nothing.
5162 */
5163void
5164mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5165{
5166 struct _sas_device *sas_device;
5167 unsigned long flags;
5168
5169 if (ioc->shost_recovery)
5170 return;
5171
5172 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5173 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5174 sas_address);
5175 if (!sas_device) {
5176 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5177 return;
5178 }
5179 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5180 _scsih_remove_device(ioc, sas_device);
5181}
5182
Eric Moore635374e2009-03-09 01:21:12 -06005183#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5184/**
5185 * _scsih_sas_topology_change_event_debug - debug for topology event
5186 * @ioc: per adapter object
5187 * @event_data: event data payload
5188 * Context: user.
5189 */
5190static void
5191_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5192 Mpi2EventDataSasTopologyChangeList_t *event_data)
5193{
5194 int i;
5195 u16 handle;
5196 u16 reason_code;
5197 u8 phy_number;
5198 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305199 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005200
5201 switch (event_data->ExpStatus) {
5202 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5203 status_str = "add";
5204 break;
5205 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5206 status_str = "remove";
5207 break;
5208 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305209 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005210 status_str = "responding";
5211 break;
5212 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5213 status_str = "remove delay";
5214 break;
5215 default:
5216 status_str = "unknown status";
5217 break;
5218 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305219 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005220 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305221 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005222 "start_phy(%02d), count(%d)\n",
5223 le16_to_cpu(event_data->ExpanderDevHandle),
5224 le16_to_cpu(event_data->EnclosureHandle),
5225 event_data->StartPhyNum, event_data->NumEntries);
5226 for (i = 0; i < event_data->NumEntries; i++) {
5227 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5228 if (!handle)
5229 continue;
5230 phy_number = event_data->StartPhyNum + i;
5231 reason_code = event_data->PHY[i].PhyStatus &
5232 MPI2_EVENT_SAS_TOPO_RC_MASK;
5233 switch (reason_code) {
5234 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305235 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005236 break;
5237 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305238 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005239 break;
5240 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305241 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005242 break;
5243 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305244 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005245 break;
5246 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305247 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005248 break;
5249 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305250 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005251 break;
5252 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305253 link_rate = event_data->PHY[i].LinkRate >> 4;
5254 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305255 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305256 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5257 handle, status_str, link_rate, prev_link_rate);
5258
Eric Moore635374e2009-03-09 01:21:12 -06005259 }
5260}
5261#endif
5262
5263/**
5264 * _scsih_sas_topology_change_event - handle topology changes
5265 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305266 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005267 * Context: user.
5268 *
5269 */
5270static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305271_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005272 struct fw_event_work *fw_event)
5273{
5274 int i;
5275 u16 parent_handle, handle;
5276 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305277 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005278 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305279 struct _sas_device *sas_device;
5280 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005281 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305282 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305283 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005284
5285#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5286 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5287 _scsih_sas_topology_change_event_debug(ioc, event_data);
5288#endif
5289
Eric Moore3cb54692010-07-08 14:44:34 -06005290 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305291 return;
5292
Eric Moore635374e2009-03-09 01:21:12 -06005293 if (!ioc->sas_hba.num_phys)
5294 _scsih_sas_host_add(ioc);
5295 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305296 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005297
5298 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305299 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005300 "event\n", ioc->name));
5301 return;
5302 }
5303
5304 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5305
5306 /* handle expander add */
5307 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5308 if (_scsih_expander_add(ioc, parent_handle) != 0)
5309 return;
5310
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305311 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5312 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5313 parent_handle);
5314 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305315 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305316 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305317 max_phys = sas_expander->num_phys;
5318 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305319 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305320 max_phys = ioc->sas_hba.num_phys;
5321 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305322 return;
5323
Eric Moore635374e2009-03-09 01:21:12 -06005324 /* handle siblings events */
5325 for (i = 0; i < event_data->NumEntries; i++) {
5326 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305327 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005328 "expander event\n", ioc->name));
5329 return;
5330 }
Eric Moore3cb54692010-07-08 14:44:34 -06005331 if (ioc->shost_recovery || ioc->remove_host ||
5332 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305333 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305334 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305335 if (phy_number >= max_phys)
5336 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305337 reason_code = event_data->PHY[i].PhyStatus &
5338 MPI2_EVENT_SAS_TOPO_RC_MASK;
5339 if ((event_data->PHY[i].PhyStatus &
5340 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5341 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005342 continue;
5343 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5344 if (!handle)
5345 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305346 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305347 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005348 switch (reason_code) {
5349 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305350
5351 if (link_rate == prev_link_rate)
5352 break;
5353
5354 mpt2sas_transport_update_links(ioc, sas_address,
5355 handle, phy_number, link_rate);
5356
Kashyap, Desaib4344272010-03-17 16:24:14 +05305357 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5358 break;
5359
5360 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305361 break;
Eric Moore635374e2009-03-09 01:21:12 -06005362 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305363
5364 mpt2sas_transport_update_links(ioc, sas_address,
5365 handle, phy_number, link_rate);
5366
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305367 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005368 break;
5369 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305370
5371 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5372 sas_device = _scsih_sas_device_find_by_handle(ioc,
5373 handle);
5374 if (!sas_device) {
5375 spin_unlock_irqrestore(&ioc->sas_device_lock,
5376 flags);
5377 break;
5378 }
5379 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5380 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005381 break;
5382 }
5383 }
5384
5385 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305386 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5387 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305388 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005389
5390}
5391
5392#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5393/**
5394 * _scsih_sas_device_status_change_event_debug - debug for device event
5395 * @event_data: event data payload
5396 * Context: user.
5397 *
5398 * Return nothing.
5399 */
5400static void
5401_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5402 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5403{
5404 char *reason_str = NULL;
5405
5406 switch (event_data->ReasonCode) {
5407 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5408 reason_str = "smart data";
5409 break;
5410 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5411 reason_str = "unsupported device discovered";
5412 break;
5413 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5414 reason_str = "internal device reset";
5415 break;
5416 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5417 reason_str = "internal task abort";
5418 break;
5419 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5420 reason_str = "internal task abort set";
5421 break;
5422 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5423 reason_str = "internal clear task set";
5424 break;
5425 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5426 reason_str = "internal query task";
5427 break;
5428 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5429 reason_str = "sata init failure";
5430 break;
5431 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5432 reason_str = "internal device reset complete";
5433 break;
5434 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5435 reason_str = "internal task abort complete";
5436 break;
5437 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5438 reason_str = "internal async notification";
5439 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305440 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5441 reason_str = "expander reduced functionality";
5442 break;
5443 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5444 reason_str = "expander reduced functionality complete";
5445 break;
Eric Moore635374e2009-03-09 01:21:12 -06005446 default:
5447 reason_str = "unknown reason";
5448 break;
5449 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305450 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305451 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5452 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5453 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5454 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005455 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305456 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005457 event_data->ASC, event_data->ASCQ);
5458 printk(KERN_INFO "\n");
5459}
5460#endif
5461
5462/**
5463 * _scsih_sas_device_status_change_event - handle device status change
5464 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305465 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005466 * Context: user.
5467 *
5468 * Return nothing.
5469 */
5470static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305471_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5472 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005473{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305474 struct MPT2SAS_TARGET *target_priv_data;
5475 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305476 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305477 unsigned long flags;
5478 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5479 fw_event->event_data;
5480
Eric Moore635374e2009-03-09 01:21:12 -06005481#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5482 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305483 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305484 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005485#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305486
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305487 /* In MPI Revision K (0xC), the internal device reset complete was
5488 * implemented, so avoid setting tm_busy flag for older firmware.
5489 */
5490 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5491 return;
5492
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305493 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305494 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305495 event_data->ReasonCode !=
5496 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305497 return;
5498
5499 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5500 sas_address = le64_to_cpu(event_data->SASAddress);
5501 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5502 sas_address);
5503 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5504
5505 if (!sas_device || !sas_device->starget)
5506 return;
5507
5508 target_priv_data = sas_device->starget->hostdata;
5509 if (!target_priv_data)
5510 return;
5511
5512 if (event_data->ReasonCode ==
5513 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5514 target_priv_data->tm_busy = 1;
5515 else
5516 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005517}
5518
5519#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5520/**
5521 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5522 * @ioc: per adapter object
5523 * @event_data: event data payload
5524 * Context: user.
5525 *
5526 * Return nothing.
5527 */
5528static void
5529_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5530 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5531{
5532 char *reason_str = NULL;
5533
5534 switch (event_data->ReasonCode) {
5535 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5536 reason_str = "enclosure add";
5537 break;
5538 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5539 reason_str = "enclosure remove";
5540 break;
5541 default:
5542 reason_str = "unknown reason";
5543 break;
5544 }
5545
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305546 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005547 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5548 " number slots(%d)\n", ioc->name, reason_str,
5549 le16_to_cpu(event_data->EnclosureHandle),
5550 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5551 le16_to_cpu(event_data->StartSlot));
5552}
5553#endif
5554
5555/**
5556 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5557 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305558 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005559 * Context: user.
5560 *
5561 * Return nothing.
5562 */
5563static void
5564_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305565 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005566{
5567#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5568 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5569 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305570 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005571#endif
5572}
5573
5574/**
5575 * _scsih_sas_broadcast_primative_event - handle broadcast events
5576 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305577 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005578 * Context: user.
5579 *
5580 * Return nothing.
5581 */
5582static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305583_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5584 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005585{
5586 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305587 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005588 u16 smid, handle;
5589 u32 lun;
5590 struct MPT2SAS_DEVICE *sas_device_priv_data;
5591 u32 termination_count;
5592 u32 query_count;
5593 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305594 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305595 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305596 unsigned long flags;
5597 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305598 u8 max_retries = 0;
5599 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305600
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305601 mutex_lock(&ioc->tm_cmds.mutex);
5602 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5603 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5604 event_data->PortWidth));
5605
5606 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005607
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305608 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305609 mpi_reply = ioc->tm_cmds.reply;
5610broadcast_aen_retry:
5611
5612 /* sanity checks for retrying this loop */
5613 if (max_retries++ == 5) {
5614 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5615 ioc->name, __func__));
5616 goto out;
5617 } else if (max_retries > 1)
5618 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5619 ioc->name, __func__, max_retries - 1));
5620
Eric Moore635374e2009-03-09 01:21:12 -06005621 termination_count = 0;
5622 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305623 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305624 if (ioc->ioc_reset_in_progress_status)
5625 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005626 scmd = _scsih_scsi_lookup_get(ioc, smid);
5627 if (!scmd)
5628 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305629 sdev = scmd->device;
5630 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005631 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5632 continue;
5633 /* skip hidden raid components */
5634 if (sas_device_priv_data->sas_target->flags &
5635 MPT_TARGET_FLAGS_RAID_COMPONENT)
5636 continue;
5637 /* skip volumes */
5638 if (sas_device_priv_data->sas_target->flags &
5639 MPT_TARGET_FLAGS_VOLUME)
5640 continue;
5641
5642 handle = sas_device_priv_data->sas_target->handle;
5643 lun = sas_device_priv_data->lun;
5644 query_count++;
5645
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305646 if (ioc->ioc_reset_in_progress_status)
5647 goto out;
5648
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305649 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305650 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5651 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5652 TM_MUTEX_OFF);
5653 if (r == FAILED) {
5654 sdev_printk(KERN_WARNING, sdev,
5655 "mpt2sas_scsih_issue_tm: FAILED when sending "
5656 "QUERY_TASK: scmd(%p)\n", scmd);
5657 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5658 goto broadcast_aen_retry;
5659 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305660 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5661 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305662 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5663 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5664 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5665 scmd);
5666 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5667 goto broadcast_aen_retry;
5668 }
5669
5670 /* see if IO is still owned by IOC and target */
5671 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005672 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5673 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305674 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305675 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005676 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305677 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305678 task_abort_retries = 0;
5679 tm_retry:
5680 if (task_abort_retries++ == 60) {
5681 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5682 "%s: ABORT_TASK: giving up\n", ioc->name,
5683 __func__));
5684 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5685 goto broadcast_aen_retry;
5686 }
5687
5688 if (ioc->ioc_reset_in_progress_status)
5689 goto out_no_lock;
5690
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305691 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5692 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305693 scmd->serial_number, TM_MUTEX_OFF);
5694 if (r == FAILED) {
5695 sdev_printk(KERN_WARNING, sdev,
5696 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305697 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305698 goto tm_retry;
5699 }
5700
5701 if (task_abort_retries > 1)
5702 sdev_printk(KERN_WARNING, sdev,
5703 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5704 " scmd(%p)\n",
5705 task_abort_retries - 1, scmd);
5706
Eric Moore635374e2009-03-09 01:21:12 -06005707 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305708 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005709 }
Eric Moore635374e2009-03-09 01:21:12 -06005710
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305711 if (ioc->broadcast_aen_pending) {
5712 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5713 " pending AEN\n", ioc->name, __func__));
5714 ioc->broadcast_aen_pending = 0;
5715 goto broadcast_aen_retry;
5716 }
5717
5718 out:
5719 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5720 out_no_lock:
5721
5722 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005723 "%s - exit, query_count = %d termination_count = %d\n",
5724 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305725
5726 ioc->broadcast_aen_busy = 0;
5727 if (!ioc->ioc_reset_in_progress_status)
5728 _scsih_ublock_io_all_device(ioc);
5729 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005730}
5731
5732/**
5733 * _scsih_sas_discovery_event - handle discovery events
5734 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305735 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005736 * Context: user.
5737 *
5738 * Return nothing.
5739 */
5740static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305741_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5742 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005743{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305744 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5745
Eric Moore635374e2009-03-09 01:21:12 -06005746#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5747 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305748 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005749 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5750 "start" : "stop");
5751 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305752 printk("discovery_status(0x%08x)",
5753 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005754 printk("\n");
5755 }
5756#endif
5757
5758 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5759 !ioc->sas_hba.num_phys)
5760 _scsih_sas_host_add(ioc);
5761}
5762
5763/**
5764 * _scsih_reprobe_lun - reprobing lun
5765 * @sdev: scsi device struct
5766 * @no_uld_attach: sdev->no_uld_attach flag setting
5767 *
5768 **/
5769static void
5770_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5771{
5772 int rc;
5773
5774 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5775 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5776 sdev->no_uld_attach ? "hidding" : "exposing");
5777 rc = scsi_device_reprobe(sdev);
5778}
5779
5780/**
5781 * _scsih_reprobe_target - reprobing target
5782 * @starget: scsi target struct
5783 * @no_uld_attach: sdev->no_uld_attach flag setting
5784 *
5785 * Note: no_uld_attach flag determines whether the disk device is attached
5786 * to block layer. A value of `1` means to not attach.
5787 **/
5788static void
5789_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5790{
5791 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5792
5793 if (no_uld_attach)
5794 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5795 else
5796 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5797
5798 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5799 _scsih_reprobe_lun);
5800}
5801/**
5802 * _scsih_sas_volume_add - add new volume
5803 * @ioc: per adapter object
5804 * @element: IR config element data
5805 * Context: user.
5806 *
5807 * Return nothing.
5808 */
5809static void
5810_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5811 Mpi2EventIrConfigElement_t *element)
5812{
5813 struct _raid_device *raid_device;
5814 unsigned long flags;
5815 u64 wwid;
5816 u16 handle = le16_to_cpu(element->VolDevHandle);
5817 int rc;
5818
Eric Moore635374e2009-03-09 01:21:12 -06005819 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5820 if (!wwid) {
5821 printk(MPT2SAS_ERR_FMT
5822 "failure at %s:%d/%s()!\n", ioc->name,
5823 __FILE__, __LINE__, __func__);
5824 return;
5825 }
5826
5827 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5828 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5829 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5830
5831 if (raid_device)
5832 return;
5833
5834 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5835 if (!raid_device) {
5836 printk(MPT2SAS_ERR_FMT
5837 "failure at %s:%d/%s()!\n", ioc->name,
5838 __FILE__, __LINE__, __func__);
5839 return;
5840 }
5841
5842 raid_device->id = ioc->sas_id++;
5843 raid_device->channel = RAID_CHANNEL;
5844 raid_device->handle = handle;
5845 raid_device->wwid = wwid;
5846 _scsih_raid_device_add(ioc, raid_device);
5847 if (!ioc->wait_for_port_enable_to_complete) {
5848 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5849 raid_device->id, 0);
5850 if (rc)
5851 _scsih_raid_device_remove(ioc, raid_device);
5852 } else
5853 _scsih_determine_boot_device(ioc, raid_device, 1);
5854}
5855
5856/**
5857 * _scsih_sas_volume_delete - delete volume
5858 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305859 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005860 * Context: user.
5861 *
5862 * Return nothing.
5863 */
5864static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305865_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005866{
5867 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005868 unsigned long flags;
5869 struct MPT2SAS_TARGET *sas_target_priv_data;
5870
Eric Moore635374e2009-03-09 01:21:12 -06005871 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5872 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5873 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5874 if (!raid_device)
5875 return;
5876 if (raid_device->starget) {
5877 sas_target_priv_data = raid_device->starget->hostdata;
5878 sas_target_priv_data->deleted = 1;
5879 scsi_remove_target(&raid_device->starget->dev);
5880 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305881 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5882 "(0x%016llx)\n", ioc->name, raid_device->handle,
5883 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005884 _scsih_raid_device_remove(ioc, raid_device);
5885}
5886
5887/**
5888 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5889 * @ioc: per adapter object
5890 * @element: IR config element data
5891 * Context: user.
5892 *
5893 * Return nothing.
5894 */
5895static void
5896_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5897 Mpi2EventIrConfigElement_t *element)
5898{
5899 struct _sas_device *sas_device;
5900 unsigned long flags;
5901 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5902
5903 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5904 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5905 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5906 if (!sas_device)
5907 return;
5908
5909 /* exposing raid component */
5910 sas_device->volume_handle = 0;
5911 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305912 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005913 _scsih_reprobe_target(sas_device->starget, 0);
5914}
5915
5916/**
5917 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5918 * @ioc: per adapter object
5919 * @element: IR config element data
5920 * Context: user.
5921 *
5922 * Return nothing.
5923 */
5924static void
5925_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5926 Mpi2EventIrConfigElement_t *element)
5927{
5928 struct _sas_device *sas_device;
5929 unsigned long flags;
5930 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5931
5932 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5933 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5934 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5935 if (!sas_device)
5936 return;
5937
5938 /* hiding raid component */
5939 mpt2sas_config_get_volume_handle(ioc, handle,
5940 &sas_device->volume_handle);
5941 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5942 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305943 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005944 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305945
Eric Moore635374e2009-03-09 01:21:12 -06005946}
5947
5948/**
5949 * _scsih_sas_pd_delete - delete pd component
5950 * @ioc: per adapter object
5951 * @element: IR config element data
5952 * Context: user.
5953 *
5954 * Return nothing.
5955 */
5956static void
5957_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5958 Mpi2EventIrConfigElement_t *element)
5959{
5960 struct _sas_device *sas_device;
5961 unsigned long flags;
5962 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5963
5964 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5965 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5966 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5967 if (!sas_device)
5968 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305969 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005970}
5971
5972/**
5973 * _scsih_sas_pd_add - remove pd component
5974 * @ioc: per adapter object
5975 * @element: IR config element data
5976 * Context: user.
5977 *
5978 * Return nothing.
5979 */
5980static void
5981_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5982 Mpi2EventIrConfigElement_t *element)
5983{
5984 struct _sas_device *sas_device;
5985 unsigned long flags;
5986 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305987 Mpi2ConfigReply_t mpi_reply;
5988 Mpi2SasDevicePage0_t sas_device_pg0;
5989 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305990 u64 sas_address;
5991 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005992
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305993 set_bit(handle, ioc->pd_handles);
5994
Eric Moore635374e2009-03-09 01:21:12 -06005995 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5996 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5997 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305998 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305999 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306000
6001 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6002 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6003 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6004 ioc->name, __FILE__, __LINE__, __func__);
6005 return;
6006 }
6007
6008 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6009 MPI2_IOCSTATUS_MASK;
6010 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6011 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6012 ioc->name, __FILE__, __LINE__, __func__);
6013 return;
6014 }
6015
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306016 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6017 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6018 mpt2sas_transport_update_links(ioc, sas_address, handle,
6019 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306020
6021 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006022}
6023
6024#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6025/**
6026 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6027 * @ioc: per adapter object
6028 * @event_data: event data payload
6029 * Context: user.
6030 *
6031 * Return nothing.
6032 */
6033static void
6034_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6035 Mpi2EventDataIrConfigChangeList_t *event_data)
6036{
6037 Mpi2EventIrConfigElement_t *element;
6038 u8 element_type;
6039 int i;
6040 char *reason_str = NULL, *element_str = NULL;
6041
6042 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6043
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306044 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006045 ioc->name, (le32_to_cpu(event_data->Flags) &
6046 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6047 "foreign" : "native", event_data->NumElements);
6048 for (i = 0; i < event_data->NumElements; i++, element++) {
6049 switch (element->ReasonCode) {
6050 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6051 reason_str = "add";
6052 break;
6053 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6054 reason_str = "remove";
6055 break;
6056 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6057 reason_str = "no change";
6058 break;
6059 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6060 reason_str = "hide";
6061 break;
6062 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6063 reason_str = "unhide";
6064 break;
6065 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6066 reason_str = "volume_created";
6067 break;
6068 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6069 reason_str = "volume_deleted";
6070 break;
6071 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6072 reason_str = "pd_created";
6073 break;
6074 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6075 reason_str = "pd_deleted";
6076 break;
6077 default:
6078 reason_str = "unknown reason";
6079 break;
6080 }
6081 element_type = le16_to_cpu(element->ElementFlags) &
6082 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6083 switch (element_type) {
6084 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6085 element_str = "volume";
6086 break;
6087 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6088 element_str = "phys disk";
6089 break;
6090 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6091 element_str = "hot spare";
6092 break;
6093 default:
6094 element_str = "unknown element";
6095 break;
6096 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306097 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006098 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6099 reason_str, le16_to_cpu(element->VolDevHandle),
6100 le16_to_cpu(element->PhysDiskDevHandle),
6101 element->PhysDiskNum);
6102 }
6103}
6104#endif
6105
6106/**
6107 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6108 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306109 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006110 * Context: user.
6111 *
6112 * Return nothing.
6113 */
6114static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306115_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6116 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006117{
6118 Mpi2EventIrConfigElement_t *element;
6119 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306120 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306121 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006122
6123#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306124 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6125 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006126 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6127
6128#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05306129 foreign_config = (le32_to_cpu(event_data->Flags) &
6130 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006131
6132 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6133 for (i = 0; i < event_data->NumElements; i++, element++) {
6134
6135 switch (element->ReasonCode) {
6136 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6137 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306138 if (!foreign_config)
6139 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006140 break;
6141 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6142 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306143 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306144 _scsih_sas_volume_delete(ioc,
6145 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006146 break;
6147 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306148 if (!ioc->is_warpdrive)
6149 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006150 break;
6151 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306152 if (!ioc->is_warpdrive)
6153 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006154 break;
6155 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306156 if (!ioc->is_warpdrive)
6157 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006158 break;
6159 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306160 if (!ioc->is_warpdrive)
6161 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006162 break;
6163 }
6164 }
6165}
6166
6167/**
6168 * _scsih_sas_ir_volume_event - IR volume event
6169 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306170 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006171 * Context: user.
6172 *
6173 * Return nothing.
6174 */
6175static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306176_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6177 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006178{
6179 u64 wwid;
6180 unsigned long flags;
6181 struct _raid_device *raid_device;
6182 u16 handle;
6183 u32 state;
6184 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306185 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006186
6187 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6188 return;
6189
6190 handle = le16_to_cpu(event_data->VolDevHandle);
6191 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306192 if (!ioc->hide_ir_msg)
6193 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6194 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6195 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006196
Eric Moore635374e2009-03-09 01:21:12 -06006197 switch (state) {
6198 case MPI2_RAID_VOL_STATE_MISSING:
6199 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306200 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006201 break;
6202
6203 case MPI2_RAID_VOL_STATE_ONLINE:
6204 case MPI2_RAID_VOL_STATE_DEGRADED:
6205 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306206
6207 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6208 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6209 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6210
Eric Moore635374e2009-03-09 01:21:12 -06006211 if (raid_device)
6212 break;
6213
6214 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6215 if (!wwid) {
6216 printk(MPT2SAS_ERR_FMT
6217 "failure at %s:%d/%s()!\n", ioc->name,
6218 __FILE__, __LINE__, __func__);
6219 break;
6220 }
6221
6222 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6223 if (!raid_device) {
6224 printk(MPT2SAS_ERR_FMT
6225 "failure at %s:%d/%s()!\n", ioc->name,
6226 __FILE__, __LINE__, __func__);
6227 break;
6228 }
6229
6230 raid_device->id = ioc->sas_id++;
6231 raid_device->channel = RAID_CHANNEL;
6232 raid_device->handle = handle;
6233 raid_device->wwid = wwid;
6234 _scsih_raid_device_add(ioc, raid_device);
6235 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6236 raid_device->id, 0);
6237 if (rc)
6238 _scsih_raid_device_remove(ioc, raid_device);
6239 break;
6240
6241 case MPI2_RAID_VOL_STATE_INITIALIZING:
6242 default:
6243 break;
6244 }
6245}
6246
6247/**
6248 * _scsih_sas_ir_physical_disk_event - PD event
6249 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306250 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006251 * Context: user.
6252 *
6253 * Return nothing.
6254 */
6255static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306256_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6257 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006258{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306259 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006260 u32 state;
6261 struct _sas_device *sas_device;
6262 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306263 Mpi2ConfigReply_t mpi_reply;
6264 Mpi2SasDevicePage0_t sas_device_pg0;
6265 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306266 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306267 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006268
6269 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6270 return;
6271
6272 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6273 state = le32_to_cpu(event_data->NewValue);
6274
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306275 if (!ioc->hide_ir_msg)
6276 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6277 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6278 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006279
Eric Moore635374e2009-03-09 01:21:12 -06006280 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006281 case MPI2_RAID_PD_STATE_ONLINE:
6282 case MPI2_RAID_PD_STATE_DEGRADED:
6283 case MPI2_RAID_PD_STATE_REBUILDING:
6284 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306285 case MPI2_RAID_PD_STATE_HOT_SPARE:
6286
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306287 if (!ioc->is_warpdrive)
6288 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306289
6290 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6291 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6292 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6293
6294 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306295 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306296
6297 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6298 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6299 handle))) {
6300 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6301 ioc->name, __FILE__, __LINE__, __func__);
6302 return;
6303 }
6304
6305 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6306 MPI2_IOCSTATUS_MASK;
6307 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6308 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6309 ioc->name, __FILE__, __LINE__, __func__);
6310 return;
6311 }
6312
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306313 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6314 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6315 mpt2sas_transport_update_links(ioc, sas_address, handle,
6316 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306317
6318 _scsih_add_device(ioc, handle, 0, 1);
6319
Eric Moore635374e2009-03-09 01:21:12 -06006320 break;
6321
Kashyap, Desai62727a72009-08-07 19:35:18 +05306322 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006323 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6324 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006325 default:
6326 break;
6327 }
6328}
6329
6330#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6331/**
6332 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6333 * @ioc: per adapter object
6334 * @event_data: event data payload
6335 * Context: user.
6336 *
6337 * Return nothing.
6338 */
6339static void
6340_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6341 Mpi2EventDataIrOperationStatus_t *event_data)
6342{
6343 char *reason_str = NULL;
6344
6345 switch (event_data->RAIDOperation) {
6346 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6347 reason_str = "resync";
6348 break;
6349 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6350 reason_str = "online capacity expansion";
6351 break;
6352 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6353 reason_str = "consistency check";
6354 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306355 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6356 reason_str = "background init";
6357 break;
6358 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6359 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006360 break;
6361 }
6362
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306363 if (!reason_str)
6364 return;
6365
Eric Moore635374e2009-03-09 01:21:12 -06006366 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6367 "\thandle(0x%04x), percent complete(%d)\n",
6368 ioc->name, reason_str,
6369 le16_to_cpu(event_data->VolDevHandle),
6370 event_data->PercentComplete);
6371}
6372#endif
6373
6374/**
6375 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6376 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306377 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006378 * Context: user.
6379 *
6380 * Return nothing.
6381 */
6382static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306383_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6384 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006385{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306386 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6387 static struct _raid_device *raid_device;
6388 unsigned long flags;
6389 u16 handle;
6390
Eric Moore635374e2009-03-09 01:21:12 -06006391#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306392 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6393 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306394 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306395 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006396#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306397
6398 /* code added for raid transport support */
6399 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6400
6401 handle = le16_to_cpu(event_data->VolDevHandle);
6402
6403 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6404 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6405 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6406
6407 if (!raid_device)
6408 return;
6409
6410 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6411 raid_device->percent_complete =
6412 event_data->PercentComplete;
6413 }
Eric Moore635374e2009-03-09 01:21:12 -06006414}
6415
6416/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306417 * _scsih_prep_device_scan - initialize parameters prior to device scan
6418 * @ioc: per adapter object
6419 *
6420 * Set the deleted flag prior to device scan. If the device is found during
6421 * the scan, then we clear the deleted flag.
6422 */
6423static void
6424_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6425{
6426 struct MPT2SAS_DEVICE *sas_device_priv_data;
6427 struct scsi_device *sdev;
6428
6429 shost_for_each_device(sdev, ioc->shost) {
6430 sas_device_priv_data = sdev->hostdata;
6431 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6432 sas_device_priv_data->sas_target->deleted = 1;
6433 }
6434}
6435
6436/**
Eric Moore635374e2009-03-09 01:21:12 -06006437 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6438 * @ioc: per adapter object
6439 * @sas_address: sas address
6440 * @slot: enclosure slot id
6441 * @handle: device handle
6442 *
6443 * After host reset, find out whether devices are still responding.
6444 * Used in _scsi_remove_unresponsive_sas_devices.
6445 *
6446 * Return nothing.
6447 */
6448static void
6449_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6450 u16 slot, u16 handle)
6451{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306452 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006453 struct scsi_target *starget;
6454 struct _sas_device *sas_device;
6455 unsigned long flags;
6456
6457 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6458 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6459 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306460 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006461 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306462 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306463 if (starget && starget->hostdata) {
6464 sas_target_priv_data = starget->hostdata;
6465 sas_target_priv_data->tm_busy = 0;
6466 sas_target_priv_data->deleted = 0;
6467 } else
6468 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306469 if (starget)
6470 starget_printk(KERN_INFO, starget,
6471 "handle(0x%04x), sas_addr(0x%016llx), "
6472 "enclosure logical id(0x%016llx), "
6473 "slot(%d)\n", handle,
6474 (unsigned long long)sas_device->sas_address,
6475 (unsigned long long)
6476 sas_device->enclosure_logical_id,
6477 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006478 if (sas_device->handle == handle)
6479 goto out;
6480 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6481 sas_device->handle);
6482 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306483 if (sas_target_priv_data)
6484 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006485 goto out;
6486 }
6487 }
6488 out:
6489 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6490}
6491
6492/**
6493 * _scsih_search_responding_sas_devices -
6494 * @ioc: per adapter object
6495 *
6496 * After host reset, find out whether devices are still responding.
6497 * If not remove.
6498 *
6499 * Return nothing.
6500 */
6501static void
6502_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6503{
6504 Mpi2SasDevicePage0_t sas_device_pg0;
6505 Mpi2ConfigReply_t mpi_reply;
6506 u16 ioc_status;
6507 __le64 sas_address;
6508 u16 handle;
6509 u32 device_info;
6510 u16 slot;
6511
6512 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6513
6514 if (list_empty(&ioc->sas_device_list))
6515 return;
6516
6517 handle = 0xFFFF;
6518 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6519 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6520 handle))) {
6521 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6522 MPI2_IOCSTATUS_MASK;
6523 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6524 break;
6525 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6526 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6527 if (!(_scsih_is_end_device(device_info)))
6528 continue;
6529 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6530 slot = le16_to_cpu(sas_device_pg0.Slot);
6531 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6532 handle);
6533 }
6534}
6535
6536/**
6537 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6538 * @ioc: per adapter object
6539 * @wwid: world wide identifier for raid volume
6540 * @handle: device handle
6541 *
6542 * After host reset, find out whether devices are still responding.
6543 * Used in _scsi_remove_unresponsive_raid_devices.
6544 *
6545 * Return nothing.
6546 */
6547static void
6548_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6549 u16 handle)
6550{
6551 struct MPT2SAS_TARGET *sas_target_priv_data;
6552 struct scsi_target *starget;
6553 struct _raid_device *raid_device;
6554 unsigned long flags;
6555
6556 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6557 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6558 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306559 starget = raid_device->starget;
6560 if (starget && starget->hostdata) {
6561 sas_target_priv_data = starget->hostdata;
6562 sas_target_priv_data->deleted = 0;
6563 } else
6564 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006565 raid_device->responding = 1;
6566 starget_printk(KERN_INFO, raid_device->starget,
6567 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6568 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306569 /*
6570 * WARPDRIVE: The handles of the PDs might have changed
6571 * across the host reset so re-initialize the
6572 * required data for Direct IO
6573 */
6574 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006575 if (raid_device->handle == handle)
6576 goto out;
6577 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6578 raid_device->handle);
6579 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306580 if (sas_target_priv_data)
6581 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006582 goto out;
6583 }
6584 }
6585 out:
6586 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6587}
6588
6589/**
6590 * _scsih_search_responding_raid_devices -
6591 * @ioc: per adapter object
6592 *
6593 * After host reset, find out whether devices are still responding.
6594 * If not remove.
6595 *
6596 * Return nothing.
6597 */
6598static void
6599_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6600{
6601 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306602 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306603 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006604 Mpi2ConfigReply_t mpi_reply;
6605 u16 ioc_status;
6606 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306607 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006608
6609 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6610
6611 if (list_empty(&ioc->raid_device_list))
6612 return;
6613
6614 handle = 0xFFFF;
6615 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6616 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6617 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6618 MPI2_IOCSTATUS_MASK;
6619 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6620 break;
6621 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306622
6623 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6624 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6625 sizeof(Mpi2RaidVolPage0_t)))
6626 continue;
6627
6628 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6629 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6630 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6631 _scsih_mark_responding_raid_device(ioc,
6632 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006633 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306634
6635 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306636 if (!ioc->is_warpdrive) {
6637 phys_disk_num = 0xFF;
6638 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6639 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6640 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6641 phys_disk_num))) {
6642 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6643 MPI2_IOCSTATUS_MASK;
6644 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6645 break;
6646 phys_disk_num = pd_pg0.PhysDiskNum;
6647 handle = le16_to_cpu(pd_pg0.DevHandle);
6648 set_bit(handle, ioc->pd_handles);
6649 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306650 }
Eric Moore635374e2009-03-09 01:21:12 -06006651}
6652
6653/**
6654 * _scsih_mark_responding_expander - mark a expander as responding
6655 * @ioc: per adapter object
6656 * @sas_address: sas address
6657 * @handle:
6658 *
6659 * After host reset, find out whether devices are still responding.
6660 * Used in _scsi_remove_unresponsive_expanders.
6661 *
6662 * Return nothing.
6663 */
6664static void
6665_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6666 u16 handle)
6667{
6668 struct _sas_node *sas_expander;
6669 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306670 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006671
6672 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6673 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306674 if (sas_expander->sas_address != sas_address)
6675 continue;
6676 sas_expander->responding = 1;
6677 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006678 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306679 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6680 " from(0x%04x) to (0x%04x)!!!\n",
6681 (unsigned long long)sas_expander->sas_address,
6682 sas_expander->handle, handle);
6683 sas_expander->handle = handle;
6684 for (i = 0 ; i < sas_expander->num_phys ; i++)
6685 sas_expander->phy[i].handle = handle;
6686 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006687 }
6688 out:
6689 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6690}
6691
6692/**
6693 * _scsih_search_responding_expanders -
6694 * @ioc: per adapter object
6695 *
6696 * After host reset, find out whether devices are still responding.
6697 * If not remove.
6698 *
6699 * Return nothing.
6700 */
6701static void
6702_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6703{
6704 Mpi2ExpanderPage0_t expander_pg0;
6705 Mpi2ConfigReply_t mpi_reply;
6706 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306707 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006708 u16 handle;
6709
6710 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6711
6712 if (list_empty(&ioc->sas_expander_list))
6713 return;
6714
6715 handle = 0xFFFF;
6716 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6717 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6718
6719 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6720 MPI2_IOCSTATUS_MASK;
6721 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6722 break;
6723
6724 handle = le16_to_cpu(expander_pg0.DevHandle);
6725 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6726 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6727 "sas_addr(0x%016llx)\n", handle,
6728 (unsigned long long)sas_address);
6729 _scsih_mark_responding_expander(ioc, sas_address, handle);
6730 }
6731
6732}
6733
6734/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306735 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006736 * @ioc: per adapter object
6737 *
6738 * Return nothing.
6739 */
6740static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306741_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006742{
6743 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306744 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006745 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006746
Eric Moore635374e2009-03-09 01:21:12 -06006747
6748 list_for_each_entry_safe(sas_device, sas_device_next,
6749 &ioc->sas_device_list, list) {
6750 if (sas_device->responding) {
6751 sas_device->responding = 0;
6752 continue;
6753 }
6754 if (sas_device->starget)
6755 starget_printk(KERN_INFO, sas_device->starget,
6756 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6757 "enclosure logical id(0x%016llx), slot(%d)\n",
6758 sas_device->handle,
6759 (unsigned long long)sas_device->sas_address,
6760 (unsigned long long)
6761 sas_device->enclosure_logical_id,
6762 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306763 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006764 }
6765
6766 list_for_each_entry_safe(raid_device, raid_device_next,
6767 &ioc->raid_device_list, list) {
6768 if (raid_device->responding) {
6769 raid_device->responding = 0;
6770 continue;
6771 }
6772 if (raid_device->starget) {
6773 starget_printk(KERN_INFO, raid_device->starget,
6774 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6775 raid_device->handle,
6776 (unsigned long long)raid_device->wwid);
6777 scsi_remove_target(&raid_device->starget->dev);
6778 }
6779 _scsih_raid_device_remove(ioc, raid_device);
6780 }
6781
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306782 retry_expander_search:
6783 sas_expander = NULL;
6784 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006785 if (sas_expander->responding) {
6786 sas_expander->responding = 0;
6787 continue;
6788 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306789 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306790 goto retry_expander_search;
6791 }
6792}
6793
6794/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306795 * _scsih_hide_unhide_sas_devices - add/remove device to/from OS
6796 * @ioc: per adapter object
6797 *
6798 * Return nothing.
6799 */
6800static void
6801_scsih_hide_unhide_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6802{
6803 struct _sas_device *sas_device, *sas_device_next;
6804
6805 if (!ioc->is_warpdrive || ioc->mfg_pg10_hide_flag !=
6806 MFG_PAGE10_HIDE_IF_VOL_PRESENT)
6807 return;
6808
6809 if (ioc->hide_drives) {
6810 if (_scsih_get_num_volumes(ioc))
6811 return;
6812 ioc->hide_drives = 0;
6813 list_for_each_entry_safe(sas_device, sas_device_next,
6814 &ioc->sas_device_list, list) {
6815 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6816 sas_device->sas_address_parent)) {
6817 _scsih_sas_device_remove(ioc, sas_device);
6818 } else if (!sas_device->starget) {
6819 mpt2sas_transport_port_remove(ioc,
6820 sas_device->sas_address,
6821 sas_device->sas_address_parent);
6822 _scsih_sas_device_remove(ioc, sas_device);
6823 }
6824 }
6825 } else {
6826 if (!_scsih_get_num_volumes(ioc))
6827 return;
6828 ioc->hide_drives = 1;
6829 list_for_each_entry_safe(sas_device, sas_device_next,
6830 &ioc->sas_device_list, list) {
6831 mpt2sas_transport_port_remove(ioc,
6832 sas_device->sas_address,
6833 sas_device->sas_address_parent);
6834 }
6835 }
6836}
6837
6838/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306839 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6840 * @ioc: per adapter object
6841 * @reset_phase: phase
6842 *
6843 * The handler for doing any required cleanup or initialization.
6844 *
6845 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6846 * MPT2_IOC_DONE_RESET
6847 *
6848 * Return nothing.
6849 */
6850void
6851mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6852{
6853 switch (reset_phase) {
6854 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306855 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306856 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306857 break;
6858 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306859 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306860 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306861 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6862 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6863 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6864 complete(&ioc->scsih_cmds.done);
6865 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306866 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6867 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6868 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6869 complete(&ioc->tm_cmds.done);
6870 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306871 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306872 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306873 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306874 break;
6875 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306876 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306877 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306878 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306879 _scsih_prep_device_scan(ioc);
6880 _scsih_search_responding_sas_devices(ioc);
6881 _scsih_search_responding_raid_devices(ioc);
6882 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306883 break;
Eric Moore635374e2009-03-09 01:21:12 -06006884 }
6885}
6886
6887/**
6888 * _firmware_event_work - delayed task for processing firmware events
6889 * @ioc: per adapter object
6890 * @work: equal to the fw_event_work object
6891 * Context: user.
6892 *
6893 * Return nothing.
6894 */
6895static void
6896_firmware_event_work(struct work_struct *work)
6897{
6898 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306899 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006900 unsigned long flags;
6901 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6902
Eric Moore635374e2009-03-09 01:21:12 -06006903 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06006904 if (ioc->remove_host || fw_event->cancel_pending_work ||
6905 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06006906 _scsih_fw_event_free(ioc, fw_event);
6907 return;
6908 }
Eric Moore635374e2009-03-09 01:21:12 -06006909
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306910 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6911 _scsih_fw_event_free(ioc, fw_event);
6912 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6913 if (ioc->shost_recovery) {
6914 init_completion(&ioc->shost_recovery_done);
6915 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6916 flags);
6917 wait_for_completion(&ioc->shost_recovery_done);
6918 } else
6919 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6920 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306921 _scsih_remove_unresponding_sas_devices(ioc);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306922 _scsih_hide_unhide_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006923 return;
6924 }
Eric Moore635374e2009-03-09 01:21:12 -06006925
6926 switch (fw_event->event) {
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05306927 case MPT2SAS_TURN_ON_FAULT_LED:
6928 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
6929 break;
Eric Moore635374e2009-03-09 01:21:12 -06006930 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306931 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006932 break;
6933 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306934 _scsih_sas_device_status_change_event(ioc,
6935 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006936 break;
6937 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306938 _scsih_sas_discovery_event(ioc,
6939 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006940 break;
6941 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306942 _scsih_sas_broadcast_primative_event(ioc,
6943 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006944 break;
6945 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6946 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306947 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006948 break;
6949 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306950 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006951 break;
6952 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306953 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006954 break;
6955 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306956 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006957 break;
6958 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306959 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006960 break;
Eric Moore635374e2009-03-09 01:21:12 -06006961 }
6962 _scsih_fw_event_free(ioc, fw_event);
6963}
6964
6965/**
6966 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6967 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306968 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006969 * @reply: reply message frame(lower 32bit addr)
6970 * Context: interrupt.
6971 *
6972 * This function merely adds a new work task into ioc->firmware_event_thread.
6973 * The tasks are worked from _firmware_event_work in user context.
6974 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306975 * Return 1 meaning mf should be freed from _base_interrupt
6976 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006977 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306978u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306979mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6980 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006981{
6982 struct fw_event_work *fw_event;
6983 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006984 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306985 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006986
6987 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06006988 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306989 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006990
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306991 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006992 event = le16_to_cpu(mpi_reply->Event);
6993
6994 switch (event) {
6995 /* handle these */
6996 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6997 {
6998 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6999 (Mpi2EventDataSasBroadcastPrimitive_t *)
7000 mpi_reply->EventData;
7001
7002 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307003 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307004 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307005
7006 if (ioc->broadcast_aen_busy) {
7007 ioc->broadcast_aen_pending++;
7008 return 1;
7009 } else
7010 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007011 break;
7012 }
7013
7014 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7015 _scsih_check_topo_delete_events(ioc,
7016 (Mpi2EventDataSasTopologyChangeList_t *)
7017 mpi_reply->EventData);
7018 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307019 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7020 _scsih_check_ir_config_unhide_events(ioc,
7021 (Mpi2EventDataIrConfigChangeList_t *)
7022 mpi_reply->EventData);
7023 break;
7024 case MPI2_EVENT_IR_VOLUME:
7025 _scsih_check_volume_delete_events(ioc,
7026 (Mpi2EventDataIrVolume_t *)
7027 mpi_reply->EventData);
7028 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307029 case MPI2_EVENT_LOG_ENTRY_ADDED:
7030 {
7031 Mpi2EventDataLogEntryAdded_t *log_entry;
7032 u32 *log_code;
7033
7034 if (!ioc->is_warpdrive)
7035 break;
7036
7037 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7038 mpi_reply->EventData;
7039 log_code = (u32 *)log_entry->LogData;
7040
7041 if (le16_to_cpu(log_entry->LogEntryQualifier)
7042 != MPT2_WARPDRIVE_LOGENTRY)
7043 break;
7044
7045 switch (le32_to_cpu(*log_code)) {
7046 case MPT2_WARPDRIVE_LC_SSDT:
7047 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7048 "IO Throttling has occurred in the WarpDrive "
7049 "subsystem. Check WarpDrive documentation for "
7050 "additional details.\n", ioc->name);
7051 break;
7052 case MPT2_WARPDRIVE_LC_SSDLW:
7053 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7054 "Program/Erase Cycles for the WarpDrive subsystem "
7055 "in degraded range. Check WarpDrive documentation "
7056 "for additional details.\n", ioc->name);
7057 break;
7058 case MPT2_WARPDRIVE_LC_SSDLF:
7059 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7060 "There are no Program/Erase Cycles for the "
7061 "WarpDrive subsystem. The storage device will be "
7062 "in read-only mode. Check WarpDrive documentation "
7063 "for additional details.\n", ioc->name);
7064 break;
7065 case MPT2_WARPDRIVE_LC_BRMF:
7066 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7067 "The Backup Rail Monitor has failed on the "
7068 "WarpDrive subsystem. Check WarpDrive "
7069 "documentation for additional details.\n",
7070 ioc->name);
7071 break;
7072 }
7073
7074 break;
7075 }
Eric Moore635374e2009-03-09 01:21:12 -06007076 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7077 case MPI2_EVENT_IR_OPERATION_STATUS:
7078 case MPI2_EVENT_SAS_DISCOVERY:
7079 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007080 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007081 break;
7082
7083 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307084 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007085 }
7086
7087 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7088 if (!fw_event) {
7089 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7090 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307091 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007092 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307093 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7094 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007095 if (!fw_event->event_data) {
7096 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7097 ioc->name, __FILE__, __LINE__, __func__);
7098 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307099 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007100 }
7101
7102 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307103 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007104 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307105 fw_event->VF_ID = mpi_reply->VF_ID;
7106 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007107 fw_event->event = event;
7108 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307109 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007110}
7111
7112/* shost template */
7113static struct scsi_host_template scsih_driver_template = {
7114 .module = THIS_MODULE,
7115 .name = "Fusion MPT SAS Host",
7116 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007117 .queuecommand = _scsih_qcmd,
7118 .target_alloc = _scsih_target_alloc,
7119 .slave_alloc = _scsih_slave_alloc,
7120 .slave_configure = _scsih_slave_configure,
7121 .target_destroy = _scsih_target_destroy,
7122 .slave_destroy = _scsih_slave_destroy,
7123 .change_queue_depth = _scsih_change_queue_depth,
7124 .change_queue_type = _scsih_change_queue_type,
7125 .eh_abort_handler = _scsih_abort,
7126 .eh_device_reset_handler = _scsih_dev_reset,
7127 .eh_target_reset_handler = _scsih_target_reset,
7128 .eh_host_reset_handler = _scsih_host_reset,
7129 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007130 .can_queue = 1,
7131 .this_id = -1,
7132 .sg_tablesize = MPT2SAS_SG_DEPTH,
7133 .max_sectors = 8192,
7134 .cmd_per_lun = 7,
7135 .use_clustering = ENABLE_CLUSTERING,
7136 .shost_attrs = mpt2sas_host_attrs,
7137 .sdev_attrs = mpt2sas_dev_attrs,
7138};
7139
7140/**
7141 * _scsih_expander_node_remove - removing expander device from list.
7142 * @ioc: per adapter object
7143 * @sas_expander: the sas_device object
7144 * Context: Calling function should acquire ioc->sas_node_lock.
7145 *
7146 * Removing object and freeing associated memory from the
7147 * ioc->sas_expander_list.
7148 *
7149 * Return nothing.
7150 */
7151static void
7152_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7153 struct _sas_node *sas_expander)
7154{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307155 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007156
7157 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307158 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007159 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307160 if (ioc->shost_recovery)
7161 return;
Eric Moore635374e2009-03-09 01:21:12 -06007162 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307163 SAS_END_DEVICE)
7164 mpt2sas_device_remove(ioc,
7165 mpt2sas_port->remote_identify.sas_address);
7166 else if (mpt2sas_port->remote_identify.device_type ==
7167 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007168 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307169 SAS_FANOUT_EXPANDER_DEVICE)
7170 mpt2sas_expander_remove(ioc,
7171 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007172 }
7173
7174 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307175 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007176
7177 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7178 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7179 sas_expander->handle, (unsigned long long)
7180 sas_expander->sas_address);
7181
Eric Moore635374e2009-03-09 01:21:12 -06007182 kfree(sas_expander->phy);
7183 kfree(sas_expander);
7184}
7185
7186/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307187 * _scsih_ir_shutdown - IR shutdown notification
7188 * @ioc: per adapter object
7189 *
7190 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7191 * the host system is shutting down.
7192 *
7193 * Return nothing.
7194 */
7195static void
7196_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7197{
7198 Mpi2RaidActionRequest_t *mpi_request;
7199 Mpi2RaidActionReply_t *mpi_reply;
7200 u16 smid;
7201
7202 /* is IR firmware build loaded ? */
7203 if (!ioc->ir_firmware)
7204 return;
7205
7206 /* are there any volumes ? */
7207 if (list_empty(&ioc->raid_device_list))
7208 return;
7209
7210 mutex_lock(&ioc->scsih_cmds.mutex);
7211
7212 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7213 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7214 ioc->name, __func__);
7215 goto out;
7216 }
7217 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7218
7219 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7220 if (!smid) {
7221 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7222 ioc->name, __func__);
7223 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7224 goto out;
7225 }
7226
7227 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7228 ioc->scsih_cmds.smid = smid;
7229 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7230
7231 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7232 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7233
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307234 if (!ioc->hide_ir_msg)
7235 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307236 init_completion(&ioc->scsih_cmds.done);
7237 mpt2sas_base_put_smid_default(ioc, smid);
7238 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7239
7240 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7241 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7242 ioc->name, __func__);
7243 goto out;
7244 }
7245
7246 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7247 mpi_reply = ioc->scsih_cmds.reply;
7248
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307249 if (!ioc->hide_ir_msg)
7250 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7251 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7252 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7253 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307254 }
7255
7256 out:
7257 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7258 mutex_unlock(&ioc->scsih_cmds.mutex);
7259}
7260
7261/**
7262 * _scsih_shutdown - routine call during system shutdown
7263 * @pdev: PCI device struct
7264 *
7265 * Return nothing.
7266 */
7267static void
7268_scsih_shutdown(struct pci_dev *pdev)
7269{
7270 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7271 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307272 struct workqueue_struct *wq;
7273 unsigned long flags;
7274
7275 ioc->remove_host = 1;
7276 _scsih_fw_event_cleanup_queue(ioc);
7277
7278 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7279 wq = ioc->firmware_event_thread;
7280 ioc->firmware_event_thread = NULL;
7281 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7282 if (wq)
7283 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307284
7285 _scsih_ir_shutdown(ioc);
7286 mpt2sas_base_detach(ioc);
7287}
7288
7289/**
Eric Moored5d135b2009-05-18 13:02:08 -06007290 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007291 * @pdev: PCI device struct
7292 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307293 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007294 * Return nothing.
7295 */
7296static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007297_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007298{
7299 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7300 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307301 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307302 struct _raid_device *raid_device, *next;
7303 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007304 struct workqueue_struct *wq;
7305 unsigned long flags;
7306
7307 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307308 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007309
7310 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7311 wq = ioc->firmware_event_thread;
7312 ioc->firmware_event_thread = NULL;
7313 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7314 if (wq)
7315 destroy_workqueue(wq);
7316
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307317 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307318 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307319 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7320 list) {
7321 if (raid_device->starget) {
7322 sas_target_priv_data =
7323 raid_device->starget->hostdata;
7324 sas_target_priv_data->deleted = 1;
7325 scsi_remove_target(&raid_device->starget->dev);
7326 }
7327 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7328 "(0x%016llx)\n", ioc->name, raid_device->handle,
7329 (unsigned long long) raid_device->wwid);
7330 _scsih_raid_device_remove(ioc, raid_device);
7331 }
7332
Eric Moore635374e2009-03-09 01:21:12 -06007333 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307334 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007335 &ioc->sas_hba.sas_port_list, port_list) {
7336 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307337 SAS_END_DEVICE)
7338 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007339 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307340 else if (mpt2sas_port->remote_identify.device_type ==
7341 SAS_EDGE_EXPANDER_DEVICE ||
7342 mpt2sas_port->remote_identify.device_type ==
7343 SAS_FANOUT_EXPANDER_DEVICE)
7344 mpt2sas_expander_remove(ioc,
7345 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007346 }
7347
7348 /* free phys attached to the sas_host */
7349 if (ioc->sas_hba.num_phys) {
7350 kfree(ioc->sas_hba.phy);
7351 ioc->sas_hba.phy = NULL;
7352 ioc->sas_hba.num_phys = 0;
7353 }
7354
7355 sas_remove_host(shost);
Eric Moore635374e2009-03-09 01:21:12 -06007356 list_del(&ioc->list);
7357 scsi_remove_host(shost);
7358 scsi_host_put(shost);
7359}
7360
7361/**
7362 * _scsih_probe_boot_devices - reports 1st device
7363 * @ioc: per adapter object
7364 *
7365 * If specified in bios page 2, this routine reports the 1st
7366 * device scsi-ml or sas transport for persistent boot device
7367 * purposes. Please refer to function _scsih_determine_boot_device()
7368 */
7369static void
7370_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7371{
7372 u8 is_raid;
7373 void *device;
7374 struct _sas_device *sas_device;
7375 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307376 u16 handle;
7377 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007378 u64 sas_address;
7379 unsigned long flags;
7380 int rc;
7381
7382 device = NULL;
7383 if (ioc->req_boot_device.device) {
7384 device = ioc->req_boot_device.device;
7385 is_raid = ioc->req_boot_device.is_raid;
7386 } else if (ioc->req_alt_boot_device.device) {
7387 device = ioc->req_alt_boot_device.device;
7388 is_raid = ioc->req_alt_boot_device.is_raid;
7389 } else if (ioc->current_boot_device.device) {
7390 device = ioc->current_boot_device.device;
7391 is_raid = ioc->current_boot_device.is_raid;
7392 }
7393
7394 if (!device)
7395 return;
7396
7397 if (is_raid) {
7398 raid_device = device;
7399 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7400 raid_device->id, 0);
7401 if (rc)
7402 _scsih_raid_device_remove(ioc, raid_device);
7403 } else {
7404 sas_device = device;
7405 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307406 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007407 sas_address = sas_device->sas_address;
7408 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7409 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7410 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307411
7412 if (ioc->hide_drives)
7413 return;
Eric Moore635374e2009-03-09 01:21:12 -06007414 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307415 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007416 _scsih_sas_device_remove(ioc, sas_device);
7417 } else if (!sas_device->starget) {
7418 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307419 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007420 _scsih_sas_device_remove(ioc, sas_device);
7421 }
7422 }
7423}
7424
7425/**
7426 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7427 * @ioc: per adapter object
7428 *
7429 * Called during initial loading of the driver.
7430 */
7431static void
7432_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7433{
7434 struct _raid_device *raid_device, *raid_next;
7435 int rc;
7436
7437 list_for_each_entry_safe(raid_device, raid_next,
7438 &ioc->raid_device_list, list) {
7439 if (raid_device->starget)
7440 continue;
7441 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7442 raid_device->id, 0);
7443 if (rc)
7444 _scsih_raid_device_remove(ioc, raid_device);
7445 }
7446}
7447
7448/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307449 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007450 * @ioc: per adapter object
7451 *
7452 * Called during initial loading of the driver.
7453 */
7454static void
7455_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7456{
7457 struct _sas_device *sas_device, *next;
7458 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007459
7460 /* SAS Device List */
7461 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7462 list) {
7463 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7464 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7465 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7466
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307467 if (ioc->hide_drives)
7468 continue;
7469
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307470 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7471 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007472 _scsih_sas_device_remove(ioc, sas_device);
7473 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307474 mpt2sas_transport_port_remove(ioc,
7475 sas_device->sas_address,
7476 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007477 _scsih_sas_device_remove(ioc, sas_device);
7478 }
7479 }
7480}
7481
7482/**
7483 * _scsih_probe_devices - probing for devices
7484 * @ioc: per adapter object
7485 *
7486 * Called during initial loading of the driver.
7487 */
7488static void
7489_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7490{
7491 u16 volume_mapping_flags =
7492 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7493 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7494
7495 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7496 return; /* return when IOC doesn't support initiator mode */
7497
7498 _scsih_probe_boot_devices(ioc);
7499
7500 if (ioc->ir_firmware) {
7501 if ((volume_mapping_flags &
7502 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
7503 _scsih_probe_sas(ioc);
7504 _scsih_probe_raid(ioc);
7505 } else {
7506 _scsih_probe_raid(ioc);
7507 _scsih_probe_sas(ioc);
7508 }
7509 } else
7510 _scsih_probe_sas(ioc);
7511}
7512
7513/**
Eric Moored5d135b2009-05-18 13:02:08 -06007514 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007515 * @pdev: PCI device struct
7516 * @id: pci device id
7517 *
7518 * Returns 0 success, anything else error.
7519 */
7520static int
Eric Moored5d135b2009-05-18 13:02:08 -06007521_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007522{
7523 struct MPT2SAS_ADAPTER *ioc;
7524 struct Scsi_Host *shost;
7525
7526 shost = scsi_host_alloc(&scsih_driver_template,
7527 sizeof(struct MPT2SAS_ADAPTER));
7528 if (!shost)
7529 return -ENODEV;
7530
7531 /* init local params */
7532 ioc = shost_priv(shost);
7533 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7534 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007535 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007536 ioc->shost = shost;
7537 ioc->id = mpt_ids++;
7538 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7539 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307540 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7541 ioc->is_warpdrive = 1;
7542 ioc->hide_ir_msg = 1;
7543 } else
7544 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007545 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7546 ioc->tm_cb_idx = tm_cb_idx;
7547 ioc->ctl_cb_idx = ctl_cb_idx;
7548 ioc->base_cb_idx = base_cb_idx;
7549 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307550 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007551 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307552 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307553 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307554 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007555 ioc->logging_level = logging_level;
7556 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307557 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007558 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7559 spin_lock_init(&ioc->scsi_lookup_lock);
7560 spin_lock_init(&ioc->sas_device_lock);
7561 spin_lock_init(&ioc->sas_node_lock);
7562 spin_lock_init(&ioc->fw_event_lock);
7563 spin_lock_init(&ioc->raid_device_lock);
7564
7565 INIT_LIST_HEAD(&ioc->sas_device_list);
7566 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7567 INIT_LIST_HEAD(&ioc->sas_expander_list);
7568 INIT_LIST_HEAD(&ioc->fw_event_list);
7569 INIT_LIST_HEAD(&ioc->raid_device_list);
7570 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307571 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307572 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007573
7574 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007575 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007576 shost->max_lun = max_lun;
7577 shost->transportt = mpt2sas_transport_template;
7578 shost->unique_id = ioc->id;
7579
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307580 if (max_sectors != 0xFFFF) {
7581 if (max_sectors < 64) {
7582 shost->max_sectors = 64;
7583 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7584 "for max_sectors, range is 64 to 8192. Assigning "
7585 "value of 64.\n", ioc->name, max_sectors);
7586 } else if (max_sectors > 8192) {
7587 shost->max_sectors = 8192;
7588 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7589 "for max_sectors, range is 64 to 8192. Assigning "
7590 "default value of 8192.\n", ioc->name,
7591 max_sectors);
7592 } else {
7593 shost->max_sectors = max_sectors & 0xFFFE;
7594 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
7595 "set to %d\n", ioc->name, shost->max_sectors);
7596 }
7597 }
7598
Eric Moore635374e2009-03-09 01:21:12 -06007599 if ((scsi_add_host(shost, &pdev->dev))) {
7600 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7601 ioc->name, __FILE__, __LINE__, __func__);
7602 list_del(&ioc->list);
7603 goto out_add_shost_fail;
7604 }
7605
Eric Moore3c621b32009-05-18 12:59:41 -06007606 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007607 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307608 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007609
Eric Moore635374e2009-03-09 01:21:12 -06007610 /* event thread */
7611 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7612 "fw_event%d", ioc->id);
7613 ioc->firmware_event_thread = create_singlethread_workqueue(
7614 ioc->firmware_event_name);
7615 if (!ioc->firmware_event_thread) {
7616 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7617 ioc->name, __FILE__, __LINE__, __func__);
7618 goto out_thread_fail;
7619 }
7620
7621 ioc->wait_for_port_enable_to_complete = 1;
7622 if ((mpt2sas_base_attach(ioc))) {
7623 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7624 ioc->name, __FILE__, __LINE__, __func__);
7625 goto out_attach_fail;
7626 }
7627
7628 ioc->wait_for_port_enable_to_complete = 0;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307629 if (ioc->is_warpdrive) {
7630 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
7631 ioc->hide_drives = 0;
7632 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
7633 ioc->hide_drives = 1;
7634 else {
7635 if (_scsih_get_num_volumes(ioc))
7636 ioc->hide_drives = 1;
7637 else
7638 ioc->hide_drives = 0;
7639 }
7640 } else
7641 ioc->hide_drives = 0;
7642
Eric Moore635374e2009-03-09 01:21:12 -06007643 _scsih_probe_devices(ioc);
7644 return 0;
7645
7646 out_attach_fail:
7647 destroy_workqueue(ioc->firmware_event_thread);
7648 out_thread_fail:
7649 list_del(&ioc->list);
7650 scsi_remove_host(shost);
7651 out_add_shost_fail:
7652 return -ENODEV;
7653}
7654
7655#ifdef CONFIG_PM
7656/**
Eric Moored5d135b2009-05-18 13:02:08 -06007657 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06007658 * @pdev: PCI device struct
7659 * @state: PM state change to (usually PCI_D3)
7660 *
7661 * Returns 0 success, anything else error.
7662 */
7663static int
Eric Moored5d135b2009-05-18 13:02:08 -06007664_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06007665{
7666 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7667 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05307668 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06007669
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307670 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007671 scsi_block_requests(shost);
7672 device_state = pci_choose_state(pdev, state);
7673 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
7674 "operating state [D%d]\n", ioc->name, pdev,
7675 pci_name(pdev), device_state);
7676
7677 mpt2sas_base_free_resources(ioc);
7678 pci_save_state(pdev);
7679 pci_disable_device(pdev);
7680 pci_set_power_state(pdev, device_state);
7681 return 0;
7682}
7683
7684/**
Eric Moored5d135b2009-05-18 13:02:08 -06007685 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06007686 * @pdev: PCI device struct
7687 *
7688 * Returns 0 success, anything else error.
7689 */
7690static int
Eric Moored5d135b2009-05-18 13:02:08 -06007691_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007692{
7693 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7694 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05307695 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06007696 int r;
7697
7698 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7699 "operating state [D%d]\n", ioc->name, pdev,
7700 pci_name(pdev), device_state);
7701
7702 pci_set_power_state(pdev, PCI_D0);
7703 pci_enable_wake(pdev, PCI_D0, 0);
7704 pci_restore_state(pdev);
7705 ioc->pdev = pdev;
7706 r = mpt2sas_base_map_resources(ioc);
7707 if (r)
7708 return r;
7709
7710 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7711 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307712 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007713 return 0;
7714}
7715#endif /* CONFIG_PM */
7716
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307717/**
7718 * _scsih_pci_error_detected - Called when a PCI error is detected.
7719 * @pdev: PCI device struct
7720 * @state: PCI channel state
7721 *
7722 * Description: Called when a PCI error is detected.
7723 *
7724 * Return value:
7725 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7726 */
7727static pci_ers_result_t
7728_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7729{
7730 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7731 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7732
7733 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7734 ioc->name, state);
7735
7736 switch (state) {
7737 case pci_channel_io_normal:
7738 return PCI_ERS_RESULT_CAN_RECOVER;
7739 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06007740 /* Fatal error, prepare for slot reset */
7741 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307742 scsi_block_requests(ioc->shost);
7743 mpt2sas_base_stop_watchdog(ioc);
7744 mpt2sas_base_free_resources(ioc);
7745 return PCI_ERS_RESULT_NEED_RESET;
7746 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06007747 /* Permanent error, prepare for device removal */
7748 ioc->pci_error_recovery = 1;
7749 mpt2sas_base_stop_watchdog(ioc);
7750 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307751 return PCI_ERS_RESULT_DISCONNECT;
7752 }
7753 return PCI_ERS_RESULT_NEED_RESET;
7754}
7755
7756/**
7757 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7758 * @pdev: PCI device struct
7759 *
7760 * Description: This routine is called by the pci error recovery
7761 * code after the PCI slot has been reset, just before we
7762 * should resume normal operations.
7763 */
7764static pci_ers_result_t
7765_scsih_pci_slot_reset(struct pci_dev *pdev)
7766{
7767 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7768 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7769 int rc;
7770
7771 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7772 ioc->name);
7773
Eric Moore3cb54692010-07-08 14:44:34 -06007774 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307775 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06007776 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307777 rc = mpt2sas_base_map_resources(ioc);
7778 if (rc)
7779 return PCI_ERS_RESULT_DISCONNECT;
7780
7781
7782 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7783 FORCE_BIG_HAMMER);
7784
7785 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7786 (rc == 0) ? "success" : "failed");
7787
7788 if (!rc)
7789 return PCI_ERS_RESULT_RECOVERED;
7790 else
7791 return PCI_ERS_RESULT_DISCONNECT;
7792}
7793
7794/**
7795 * _scsih_pci_resume() - resume normal ops after PCI reset
7796 * @pdev: pointer to PCI device
7797 *
7798 * Called when the error recovery driver tells us that its
7799 * OK to resume normal operation. Use completion to allow
7800 * halted scsi ops to resume.
7801 */
7802static void
7803_scsih_pci_resume(struct pci_dev *pdev)
7804{
7805 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7806 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7807
7808 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7809
7810 pci_cleanup_aer_uncorrect_error_status(pdev);
7811 mpt2sas_base_start_watchdog(ioc);
7812 scsi_unblock_requests(ioc->shost);
7813}
7814
7815/**
7816 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7817 * @pdev: pointer to PCI device
7818 */
7819static pci_ers_result_t
7820_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7821{
7822 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7823 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7824
7825 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7826 ioc->name);
7827
7828 /* TODO - dump whatever for debugging purposes */
7829
7830 /* Request a slot reset. */
7831 return PCI_ERS_RESULT_NEED_RESET;
7832}
7833
7834static struct pci_error_handlers _scsih_err_handler = {
7835 .error_detected = _scsih_pci_error_detected,
7836 .mmio_enabled = _scsih_pci_mmio_enabled,
7837 .slot_reset = _scsih_pci_slot_reset,
7838 .resume = _scsih_pci_resume,
7839};
Eric Moore635374e2009-03-09 01:21:12 -06007840
7841static struct pci_driver scsih_driver = {
7842 .name = MPT2SAS_DRIVER_NAME,
7843 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007844 .probe = _scsih_probe,
7845 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307846 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307847 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007848#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007849 .suspend = _scsih_suspend,
7850 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007851#endif
7852};
7853
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307854/* raid transport support */
7855static struct raid_function_template mpt2sas_raid_functions = {
7856 .cookie = &scsih_driver_template,
7857 .is_raid = _scsih_is_raid,
7858 .get_resync = _scsih_get_resync,
7859 .get_state = _scsih_get_state,
7860};
Eric Moore635374e2009-03-09 01:21:12 -06007861
7862/**
Eric Moored5d135b2009-05-18 13:02:08 -06007863 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007864 *
7865 * Returns 0 success, anything else error.
7866 */
7867static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007868_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007869{
7870 int error;
7871
7872 mpt_ids = 0;
7873 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7874 MPT2SAS_DRIVER_VERSION);
7875
7876 mpt2sas_transport_template =
7877 sas_attach_transport(&mpt2sas_transport_functions);
7878 if (!mpt2sas_transport_template)
7879 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307880 /* raid transport support */
7881 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7882 if (!mpt2sas_raid_template) {
7883 sas_release_transport(mpt2sas_transport_template);
7884 return -ENODEV;
7885 }
Eric Moore635374e2009-03-09 01:21:12 -06007886
7887 mpt2sas_base_initialize_callback_handler();
7888
7889 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007890 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007891
Uwe Kleine-König65155b32010-06-11 12:17:01 +02007892 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007893 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007894
7895 /* base internal commands callback handler */
7896 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7897
7898 /* transport internal commands callback handler */
7899 transport_cb_idx = mpt2sas_base_register_callback_handler(
7900 mpt2sas_transport_done);
7901
Kashyap, Desai744090d2009-10-05 15:56:56 +05307902 /* scsih internal commands callback handler */
7903 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7904
Eric Moore635374e2009-03-09 01:21:12 -06007905 /* configuration page API internal commands callback handler */
7906 config_cb_idx = mpt2sas_base_register_callback_handler(
7907 mpt2sas_config_done);
7908
7909 /* ctl module callback handler */
7910 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7911
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307912 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7913 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307914
7915 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7916 _scsih_tm_volume_tr_complete);
7917
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307918 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7919 _scsih_sas_control_complete);
7920
Eric Moore635374e2009-03-09 01:21:12 -06007921 mpt2sas_ctl_init();
7922
7923 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307924 if (error) {
7925 /* raid transport support */
7926 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007927 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307928 }
Eric Moore635374e2009-03-09 01:21:12 -06007929
7930 return error;
7931}
7932
7933/**
Eric Moored5d135b2009-05-18 13:02:08 -06007934 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007935 *
7936 * Returns 0 success, anything else error.
7937 */
7938static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007939_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007940{
7941 printk(KERN_INFO "mpt2sas version %s unloading\n",
7942 MPT2SAS_DRIVER_VERSION);
7943
7944 pci_unregister_driver(&scsih_driver);
7945
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307946 mpt2sas_ctl_exit();
7947
Eric Moore635374e2009-03-09 01:21:12 -06007948 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7949 mpt2sas_base_release_callback_handler(tm_cb_idx);
7950 mpt2sas_base_release_callback_handler(base_cb_idx);
7951 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307952 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007953 mpt2sas_base_release_callback_handler(config_cb_idx);
7954 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7955
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307956 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307957 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307958 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7959
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307960 /* raid transport support */
7961 raid_class_release(mpt2sas_raid_template);
7962 sas_release_transport(mpt2sas_transport_template);
7963
Eric Moore635374e2009-03-09 01:21:12 -06007964}
7965
Eric Moored5d135b2009-05-18 13:02:08 -06007966module_init(_scsih_init);
7967module_exit(_scsih_exit);