blob: 6d9f99720b4843dbb7ecd35430a100ae9a15bbeb [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, Desai19d3ebe2009-09-14 11:01:36 +05305 * Copyright (C) 2007-2009 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
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
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
72/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060073LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060074
75/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060076static u8 scsi_io_cb_idx = -1;
77static u8 tm_cb_idx = -1;
78static u8 ctl_cb_idx = -1;
79static u8 base_cb_idx = -1;
80static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053081static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060082static u8 config_cb_idx = -1;
83static int mpt_ids;
84
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053085static u8 tm_tr_cb_idx = -1 ;
86static u8 tm_sas_control_cb_idx = -1;
87
Eric Moore635374e2009-03-09 01:21:12 -060088/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060089static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060090MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
91 "(default=0)");
92
93/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
94#define MPT2SAS_MAX_LUN (16895)
95static int max_lun = MPT2SAS_MAX_LUN;
96module_param(max_lun, int, 0);
97MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
98
99/**
100 * struct sense_info - common structure for obtaining sense keys
101 * @skey: sense key
102 * @asc: additional sense code
103 * @ascq: additional sense code qualifier
104 */
105struct sense_info {
106 u8 skey;
107 u8 asc;
108 u8 ascq;
109};
110
111
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530112#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
113
Eric Moore635374e2009-03-09 01:21:12 -0600114/**
115 * struct fw_event_work - firmware event struct
116 * @list: link list framework
117 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530118 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600119 * @ioc: per adapter object
120 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530121 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600122 * @ignore: flag meaning this event has been marked to ignore
123 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
124 * @event_data: reply event data payload follows
125 *
126 * This object stored on ioc->fw_event_list.
127 */
128struct fw_event_work {
129 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530130 u8 cancel_pending_work;
131 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600132 struct MPT2SAS_ADAPTER *ioc;
133 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530134 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600135 u8 ignore;
136 u16 event;
137 void *event_data;
138};
139
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530140/* raid transport support */
141static struct raid_template *mpt2sas_raid_template;
142
Eric Moore635374e2009-03-09 01:21:12 -0600143/**
144 * struct _scsi_io_transfer - scsi io transfer
145 * @handle: sas device handle (assigned by firmware)
146 * @is_raid: flag set for hidden raid components
147 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
148 * @data_length: data transfer length
149 * @data_dma: dma pointer to data
150 * @sense: sense data
151 * @lun: lun number
152 * @cdb_length: cdb length
153 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600154 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530155 * @VF_ID: virtual function id
156 * @VP_ID: virtual port id
157 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @sense_length: sense length
159 * @ioc_status: ioc status
160 * @scsi_state: scsi state
161 * @scsi_status: scsi staus
162 * @log_info: log information
163 * @transfer_length: data length transfer when there is a reply message
164 *
165 * Used for sending internal scsi commands to devices within this module.
166 * Refer to _scsi_send_scsi_io().
167 */
168struct _scsi_io_transfer {
169 u16 handle;
170 u8 is_raid;
171 enum dma_data_direction dir;
172 u32 data_length;
173 dma_addr_t data_dma;
174 u8 sense[SCSI_SENSE_BUFFERSIZE];
175 u32 lun;
176 u8 cdb_length;
177 u8 cdb[32];
178 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530179 u8 VF_ID;
180 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600181 u8 valid_reply;
182 /* the following bits are only valid when 'valid_reply = 1' */
183 u32 sense_length;
184 u16 ioc_status;
185 u8 scsi_state;
186 u8 scsi_status;
187 u32 log_info;
188 u32 transfer_length;
189};
190
191/*
192 * The pci device ids are defined in mpi/mpi2_cnfg.h.
193 */
194static struct pci_device_id scsih_pci_table[] = {
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
196 PCI_ANY_ID, PCI_ANY_ID },
197 /* Falcon ~ 2008*/
198 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
199 PCI_ANY_ID, PCI_ANY_ID },
200 /* Liberator ~ 2108 */
201 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
202 PCI_ANY_ID, PCI_ANY_ID },
203 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
204 PCI_ANY_ID, PCI_ANY_ID },
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
206 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530207 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
209 PCI_ANY_ID, PCI_ANY_ID },
210 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
211 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530212 /* Thunderbolt ~ 2208 */
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
228 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600229 {0} /* Terminating entry */
230};
231MODULE_DEVICE_TABLE(pci, scsih_pci_table);
232
233/**
Eric Moored5d135b2009-05-18 13:02:08 -0600234 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600235 *
236 * Note: The logging levels are defined in mpt2sas_debug.h.
237 */
238static int
Eric Moored5d135b2009-05-18 13:02:08 -0600239_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600240{
241 int ret = param_set_int(val, kp);
242 struct MPT2SAS_ADAPTER *ioc;
243
244 if (ret)
245 return ret;
246
247 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600248 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600249 ioc->logging_level = logging_level;
250 return 0;
251}
Eric Moored5d135b2009-05-18 13:02:08 -0600252module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600253 &logging_level, 0644);
254
255/**
256 * _scsih_srch_boot_sas_address - search based on sas_address
257 * @sas_address: sas address
258 * @boot_device: boot device object from bios page 2
259 *
260 * Returns 1 when there's a match, 0 means no match.
261 */
262static inline int
263_scsih_srch_boot_sas_address(u64 sas_address,
264 Mpi2BootDeviceSasWwid_t *boot_device)
265{
266 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
267}
268
269/**
270 * _scsih_srch_boot_device_name - search based on device name
271 * @device_name: device name specified in INDENTIFY fram
272 * @boot_device: boot device object from bios page 2
273 *
274 * Returns 1 when there's a match, 0 means no match.
275 */
276static inline int
277_scsih_srch_boot_device_name(u64 device_name,
278 Mpi2BootDeviceDeviceName_t *boot_device)
279{
280 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
281}
282
283/**
284 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
285 * @enclosure_logical_id: enclosure logical id
286 * @slot_number: slot number
287 * @boot_device: boot device object from bios page 2
288 *
289 * Returns 1 when there's a match, 0 means no match.
290 */
291static inline int
292_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
293 Mpi2BootDeviceEnclosureSlot_t *boot_device)
294{
295 return (enclosure_logical_id == le64_to_cpu(boot_device->
296 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
297 SlotNumber)) ? 1 : 0;
298}
299
300/**
301 * _scsih_is_boot_device - search for matching boot device.
302 * @sas_address: sas address
303 * @device_name: device name specified in INDENTIFY fram
304 * @enclosure_logical_id: enclosure logical id
305 * @slot_number: slot number
306 * @form: specifies boot device form
307 * @boot_device: boot device object from bios page 2
308 *
309 * Returns 1 when there's a match, 0 means no match.
310 */
311static int
312_scsih_is_boot_device(u64 sas_address, u64 device_name,
313 u64 enclosure_logical_id, u16 slot, u8 form,
314 Mpi2BiosPage2BootDevice_t *boot_device)
315{
316 int rc = 0;
317
318 switch (form) {
319 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
320 if (!sas_address)
321 break;
322 rc = _scsih_srch_boot_sas_address(
323 sas_address, &boot_device->SasWwid);
324 break;
325 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
326 if (!enclosure_logical_id)
327 break;
328 rc = _scsih_srch_boot_encl_slot(
329 enclosure_logical_id,
330 slot, &boot_device->EnclosureSlot);
331 break;
332 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
333 if (!device_name)
334 break;
335 rc = _scsih_srch_boot_device_name(
336 device_name, &boot_device->DeviceName);
337 break;
338 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
339 break;
340 }
341
342 return rc;
343}
344
345/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530346 * _scsih_get_sas_address - set the sas_address for given device handle
347 * @handle: device handle
348 * @sas_address: sas address
349 *
350 * Returns 0 success, non-zero when failure
351 */
352static int
353_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
354 u64 *sas_address)
355{
356 Mpi2SasDevicePage0_t sas_device_pg0;
357 Mpi2ConfigReply_t mpi_reply;
358 u32 ioc_status;
359
360 if (handle <= ioc->sas_hba.num_phys) {
361 *sas_address = ioc->sas_hba.sas_address;
362 return 0;
363 } else
364 *sas_address = 0;
365
366 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
367 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
368 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
369 ioc->name, __FILE__, __LINE__, __func__);
370 return -ENXIO;
371 }
372
373 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
374 MPI2_IOCSTATUS_MASK;
375 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
376 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
377 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
378 __FILE__, __LINE__, __func__);
379 return -EIO;
380 }
381
382 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
383 return 0;
384}
385
386/**
Eric Moore635374e2009-03-09 01:21:12 -0600387 * _scsih_determine_boot_device - determine boot device.
388 * @ioc: per adapter object
389 * @device: either sas_device or raid_device object
390 * @is_raid: [flag] 1 = raid object, 0 = sas object
391 *
392 * Determines whether this device should be first reported device to
393 * to scsi-ml or sas transport, this purpose is for persistant boot device.
394 * There are primary, alternate, and current entries in bios page 2. The order
395 * priority is primary, alternate, then current. This routine saves
396 * the corresponding device object and is_raid flag in the ioc object.
397 * The saved data to be used later in _scsih_probe_boot_devices().
398 */
399static void
400_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
401 void *device, u8 is_raid)
402{
403 struct _sas_device *sas_device;
404 struct _raid_device *raid_device;
405 u64 sas_address;
406 u64 device_name;
407 u64 enclosure_logical_id;
408 u16 slot;
409
410 /* only process this function when driver loads */
411 if (!ioc->wait_for_port_enable_to_complete)
412 return;
413
414 if (!is_raid) {
415 sas_device = device;
416 sas_address = sas_device->sas_address;
417 device_name = sas_device->device_name;
418 enclosure_logical_id = sas_device->enclosure_logical_id;
419 slot = sas_device->slot;
420 } else {
421 raid_device = device;
422 sas_address = raid_device->wwid;
423 device_name = 0;
424 enclosure_logical_id = 0;
425 slot = 0;
426 }
427
428 if (!ioc->req_boot_device.device) {
429 if (_scsih_is_boot_device(sas_address, device_name,
430 enclosure_logical_id, slot,
431 (ioc->bios_pg2.ReqBootDeviceForm &
432 MPI2_BIOSPAGE2_FORM_MASK),
433 &ioc->bios_pg2.RequestedBootDevice)) {
434 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
435 "%s: req_boot_device(0x%016llx)\n",
436 ioc->name, __func__,
437 (unsigned long long)sas_address));
438 ioc->req_boot_device.device = device;
439 ioc->req_boot_device.is_raid = is_raid;
440 }
441 }
442
443 if (!ioc->req_alt_boot_device.device) {
444 if (_scsih_is_boot_device(sas_address, device_name,
445 enclosure_logical_id, slot,
446 (ioc->bios_pg2.ReqAltBootDeviceForm &
447 MPI2_BIOSPAGE2_FORM_MASK),
448 &ioc->bios_pg2.RequestedAltBootDevice)) {
449 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
450 "%s: req_alt_boot_device(0x%016llx)\n",
451 ioc->name, __func__,
452 (unsigned long long)sas_address));
453 ioc->req_alt_boot_device.device = device;
454 ioc->req_alt_boot_device.is_raid = is_raid;
455 }
456 }
457
458 if (!ioc->current_boot_device.device) {
459 if (_scsih_is_boot_device(sas_address, device_name,
460 enclosure_logical_id, slot,
461 (ioc->bios_pg2.CurrentBootDeviceForm &
462 MPI2_BIOSPAGE2_FORM_MASK),
463 &ioc->bios_pg2.CurrentBootDevice)) {
464 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
465 "%s: current_boot_device(0x%016llx)\n",
466 ioc->name, __func__,
467 (unsigned long long)sas_address));
468 ioc->current_boot_device.device = device;
469 ioc->current_boot_device.is_raid = is_raid;
470 }
471 }
472}
473
474/**
475 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
476 * @ioc: per adapter object
477 * @sas_address: sas address
478 * Context: Calling function should acquire ioc->sas_device_lock
479 *
480 * This searches for sas_device based on sas_address, then return sas_device
481 * object.
482 */
483struct _sas_device *
484mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
485 u64 sas_address)
486{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530487 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600488
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530489 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
490 if (sas_device->sas_address == sas_address)
491 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600492
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530493 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
494 if (sas_device->sas_address == sas_address)
495 return sas_device;
496
497 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600498}
499
500/**
501 * _scsih_sas_device_find_by_handle - sas device search
502 * @ioc: per adapter object
503 * @handle: sas device handle (assigned by firmware)
504 * Context: Calling function should acquire ioc->sas_device_lock
505 *
506 * This searches for sas_device based on sas_address, then return sas_device
507 * object.
508 */
509static struct _sas_device *
510_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
511{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530512 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600513
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530514 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
515 if (sas_device->handle == handle)
516 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600517
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530518 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
519 if (sas_device->handle == handle)
520 return sas_device;
521
522 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600523}
524
525/**
526 * _scsih_sas_device_remove - remove sas_device from list.
527 * @ioc: per adapter object
528 * @sas_device: the sas_device object
529 * Context: This function will acquire ioc->sas_device_lock.
530 *
531 * Removing object and freeing associated memory from the ioc->sas_device_list.
532 */
533static void
534_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
535 struct _sas_device *sas_device)
536{
537 unsigned long flags;
538
539 spin_lock_irqsave(&ioc->sas_device_lock, flags);
540 list_del(&sas_device->list);
541 memset(sas_device, 0, sizeof(struct _sas_device));
542 kfree(sas_device);
543 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
544}
545
546/**
547 * _scsih_sas_device_add - insert sas_device to the list.
548 * @ioc: per adapter object
549 * @sas_device: the sas_device object
550 * Context: This function will acquire ioc->sas_device_lock.
551 *
552 * Adding new object to the ioc->sas_device_list.
553 */
554static void
555_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
556 struct _sas_device *sas_device)
557{
558 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600559
560 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
561 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
562 sas_device->handle, (unsigned long long)sas_device->sas_address));
563
564 spin_lock_irqsave(&ioc->sas_device_lock, flags);
565 list_add_tail(&sas_device->list, &ioc->sas_device_list);
566 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
567
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530568 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
569 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600570 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600571}
572
573/**
574 * _scsih_sas_device_init_add - insert sas_device to the list.
575 * @ioc: per adapter object
576 * @sas_device: the sas_device object
577 * Context: This function will acquire ioc->sas_device_lock.
578 *
579 * Adding new object at driver load time to the ioc->sas_device_init_list.
580 */
581static void
582_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
583 struct _sas_device *sas_device)
584{
585 unsigned long flags;
586
587 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
588 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
589 sas_device->handle, (unsigned long long)sas_device->sas_address));
590
591 spin_lock_irqsave(&ioc->sas_device_lock, flags);
592 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
593 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
594 _scsih_determine_boot_device(ioc, sas_device, 0);
595}
596
597/**
Eric Moore635374e2009-03-09 01:21:12 -0600598 * _scsih_raid_device_find_by_id - raid device search
599 * @ioc: per adapter object
600 * @id: sas device target id
601 * @channel: sas device channel
602 * Context: Calling function should acquire ioc->raid_device_lock
603 *
604 * This searches for raid_device based on target id, then return raid_device
605 * object.
606 */
607static struct _raid_device *
608_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
609{
610 struct _raid_device *raid_device, *r;
611
612 r = NULL;
613 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
614 if (raid_device->id == id && raid_device->channel == channel) {
615 r = raid_device;
616 goto out;
617 }
618 }
619
620 out:
621 return r;
622}
623
624/**
625 * _scsih_raid_device_find_by_handle - raid device search
626 * @ioc: per adapter object
627 * @handle: sas device handle (assigned by firmware)
628 * Context: Calling function should acquire ioc->raid_device_lock
629 *
630 * This searches for raid_device based on handle, then return raid_device
631 * object.
632 */
633static struct _raid_device *
634_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
635{
636 struct _raid_device *raid_device, *r;
637
638 r = NULL;
639 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
640 if (raid_device->handle != handle)
641 continue;
642 r = raid_device;
643 goto out;
644 }
645
646 out:
647 return r;
648}
649
650/**
651 * _scsih_raid_device_find_by_wwid - raid device search
652 * @ioc: per adapter object
653 * @handle: sas device handle (assigned by firmware)
654 * Context: Calling function should acquire ioc->raid_device_lock
655 *
656 * This searches for raid_device based on wwid, then return raid_device
657 * object.
658 */
659static struct _raid_device *
660_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
661{
662 struct _raid_device *raid_device, *r;
663
664 r = NULL;
665 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
666 if (raid_device->wwid != wwid)
667 continue;
668 r = raid_device;
669 goto out;
670 }
671
672 out:
673 return r;
674}
675
676/**
677 * _scsih_raid_device_add - add raid_device object
678 * @ioc: per adapter object
679 * @raid_device: raid_device object
680 *
681 * This is added to the raid_device_list link list.
682 */
683static void
684_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
685 struct _raid_device *raid_device)
686{
687 unsigned long flags;
688
689 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
690 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
691 raid_device->handle, (unsigned long long)raid_device->wwid));
692
693 spin_lock_irqsave(&ioc->raid_device_lock, flags);
694 list_add_tail(&raid_device->list, &ioc->raid_device_list);
695 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
696}
697
698/**
699 * _scsih_raid_device_remove - delete raid_device object
700 * @ioc: per adapter object
701 * @raid_device: raid_device object
702 *
703 * This is removed from the raid_device_list link list.
704 */
705static void
706_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
707 struct _raid_device *raid_device)
708{
709 unsigned long flags;
710
711 spin_lock_irqsave(&ioc->raid_device_lock, flags);
712 list_del(&raid_device->list);
713 memset(raid_device, 0, sizeof(struct _raid_device));
714 kfree(raid_device);
715 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
716}
717
718/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530719 * mpt2sas_scsih_expander_find_by_handle - expander device search
720 * @ioc: per adapter object
721 * @handle: expander handle (assigned by firmware)
722 * Context: Calling function should acquire ioc->sas_device_lock
723 *
724 * This searches for expander device based on handle, then returns the
725 * sas_node object.
726 */
727struct _sas_node *
728mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
729{
730 struct _sas_node *sas_expander, *r;
731
732 r = NULL;
733 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
734 if (sas_expander->handle != handle)
735 continue;
736 r = sas_expander;
737 goto out;
738 }
739 out:
740 return r;
741}
742
743/**
Eric Moore635374e2009-03-09 01:21:12 -0600744 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
745 * @ioc: per adapter object
746 * @sas_address: sas address
747 * Context: Calling function should acquire ioc->sas_node_lock.
748 *
749 * This searches for expander device based on sas_address, then returns the
750 * sas_node object.
751 */
752struct _sas_node *
753mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
754 u64 sas_address)
755{
756 struct _sas_node *sas_expander, *r;
757
758 r = NULL;
759 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
760 if (sas_expander->sas_address != sas_address)
761 continue;
762 r = sas_expander;
763 goto out;
764 }
765 out:
766 return r;
767}
768
769/**
770 * _scsih_expander_node_add - insert expander device to the list.
771 * @ioc: per adapter object
772 * @sas_expander: the sas_device object
773 * Context: This function will acquire ioc->sas_node_lock.
774 *
775 * Adding new object to the ioc->sas_expander_list.
776 *
777 * Return nothing.
778 */
779static void
780_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
781 struct _sas_node *sas_expander)
782{
783 unsigned long flags;
784
785 spin_lock_irqsave(&ioc->sas_node_lock, flags);
786 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
787 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
788}
789
790/**
791 * _scsih_is_end_device - determines if device is an end device
792 * @device_info: bitfield providing information about the device.
793 * Context: none
794 *
795 * Returns 1 if end device.
796 */
797static int
798_scsih_is_end_device(u32 device_info)
799{
800 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
801 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
802 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
803 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
804 return 1;
805 else
806 return 0;
807}
808
809/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530810 * mptscsih_get_scsi_lookup - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600811 * @ioc: per adapter object
812 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600813 *
814 * Returns the smid stored scmd pointer.
815 */
816static struct scsi_cmnd *
817_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
818{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530819 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600820}
821
822/**
823 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
824 * @ioc: per adapter object
825 * @smid: system request message index
826 * @scmd: pointer to scsi command object
827 * Context: This function will acquire ioc->scsi_lookup_lock.
828 *
829 * This will search for a scmd pointer in the scsi_lookup array,
830 * returning the revelent smid. A returned value of zero means invalid.
831 */
832static u16
833_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
834 *scmd)
835{
836 u16 smid;
837 unsigned long flags;
838 int i;
839
840 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
841 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530842 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600843 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530844 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600845 goto out;
846 }
847 }
848 out:
849 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
850 return smid;
851}
852
853/**
854 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
855 * @ioc: per adapter object
856 * @id: target id
857 * @channel: channel
858 * Context: This function will acquire ioc->scsi_lookup_lock.
859 *
860 * This will search for a matching channel:id in the scsi_lookup array,
861 * returning 1 if found.
862 */
863static u8
864_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
865 int channel)
866{
867 u8 found;
868 unsigned long flags;
869 int i;
870
871 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
872 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530873 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600874 if (ioc->scsi_lookup[i].scmd &&
875 (ioc->scsi_lookup[i].scmd->device->id == id &&
876 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
877 found = 1;
878 goto out;
879 }
880 }
881 out:
882 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
883 return found;
884}
885
886/**
Eric Moore993e0da2009-05-18 13:00:45 -0600887 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
888 * @ioc: per adapter object
889 * @id: target id
890 * @lun: lun number
891 * @channel: channel
892 * Context: This function will acquire ioc->scsi_lookup_lock.
893 *
894 * This will search for a matching channel:id:lun in the scsi_lookup array,
895 * returning 1 if found.
896 */
897static u8
898_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
899 unsigned int lun, int channel)
900{
901 u8 found;
902 unsigned long flags;
903 int i;
904
905 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
906 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530907 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600908 if (ioc->scsi_lookup[i].scmd &&
909 (ioc->scsi_lookup[i].scmd->device->id == id &&
910 ioc->scsi_lookup[i].scmd->device->channel == channel &&
911 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
912 found = 1;
913 goto out;
914 }
915 }
916 out:
917 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
918 return found;
919}
920
921/**
Eric Moore635374e2009-03-09 01:21:12 -0600922 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
923 * @ioc: per adapter object
924 * @smid: system request message index
925 *
926 * Returns phys pointer to chain buffer.
927 */
928static dma_addr_t
929_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
930{
931 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
932 ioc->chains_needed_per_io));
933}
934
935/**
936 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
937 * @ioc: per adapter object
938 * @smid: system request message index
939 *
940 * Returns virt pointer to chain buffer.
941 */
942static void *
943_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
944{
945 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
946 ioc->chains_needed_per_io)));
947}
948
949/**
950 * _scsih_build_scatter_gather - main sg creation routine
951 * @ioc: per adapter object
952 * @scmd: scsi command
953 * @smid: system request message index
954 * Context: none.
955 *
956 * The main routine that builds scatter gather table from a given
957 * scsi request sent via the .queuecommand main handler.
958 *
959 * Returns 0 success, anything else error
960 */
961static int
962_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
963 struct scsi_cmnd *scmd, u16 smid)
964{
965 Mpi2SCSIIORequest_t *mpi_request;
966 dma_addr_t chain_dma;
967 struct scatterlist *sg_scmd;
968 void *sg_local, *chain;
969 u32 chain_offset;
970 u32 chain_length;
971 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900972 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -0600973 u32 sges_in_segment;
974 u32 sgl_flags;
975 u32 sgl_flags_last_element;
976 u32 sgl_flags_end_buffer;
977
978 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
979
980 /* init scatter gather flags */
981 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
982 if (scmd->sc_data_direction == DMA_TO_DEVICE)
983 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
984 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
985 << MPI2_SGE_FLAGS_SHIFT;
986 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
987 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
988 << MPI2_SGE_FLAGS_SHIFT;
989 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
990
991 sg_scmd = scsi_sglist(scmd);
992 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900993 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -0600994 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
995 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
996 return -ENOMEM;
997 }
998
999 sg_local = &mpi_request->SGL;
1000 sges_in_segment = ioc->max_sges_in_main_message;
1001 if (sges_left <= sges_in_segment)
1002 goto fill_in_last_segment;
1003
1004 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1005 (sges_in_segment * ioc->sge_size))/4;
1006
1007 /* fill in main message segment when there is a chain following */
1008 while (sges_in_segment) {
1009 if (sges_in_segment == 1)
1010 ioc->base_add_sg_single(sg_local,
1011 sgl_flags_last_element | sg_dma_len(sg_scmd),
1012 sg_dma_address(sg_scmd));
1013 else
1014 ioc->base_add_sg_single(sg_local, sgl_flags |
1015 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1016 sg_scmd = sg_next(sg_scmd);
1017 sg_local += ioc->sge_size;
1018 sges_left--;
1019 sges_in_segment--;
1020 }
1021
1022 /* initializing the chain flags and pointers */
1023 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1024 chain = _scsih_get_chain_buffer(ioc, smid);
1025 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1026 do {
1027 sges_in_segment = (sges_left <=
1028 ioc->max_sges_in_chain_message) ? sges_left :
1029 ioc->max_sges_in_chain_message;
1030 chain_offset = (sges_left == sges_in_segment) ?
1031 0 : (sges_in_segment * ioc->sge_size)/4;
1032 chain_length = sges_in_segment * ioc->sge_size;
1033 if (chain_offset) {
1034 chain_offset = chain_offset <<
1035 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1036 chain_length += ioc->sge_size;
1037 }
1038 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1039 chain_length, chain_dma);
1040 sg_local = chain;
1041 if (!chain_offset)
1042 goto fill_in_last_segment;
1043
1044 /* fill in chain segments */
1045 while (sges_in_segment) {
1046 if (sges_in_segment == 1)
1047 ioc->base_add_sg_single(sg_local,
1048 sgl_flags_last_element |
1049 sg_dma_len(sg_scmd),
1050 sg_dma_address(sg_scmd));
1051 else
1052 ioc->base_add_sg_single(sg_local, sgl_flags |
1053 sg_dma_len(sg_scmd),
1054 sg_dma_address(sg_scmd));
1055 sg_scmd = sg_next(sg_scmd);
1056 sg_local += ioc->sge_size;
1057 sges_left--;
1058 sges_in_segment--;
1059 }
1060
1061 chain_dma += ioc->request_sz;
1062 chain += ioc->request_sz;
1063 } while (1);
1064
1065
1066 fill_in_last_segment:
1067
1068 /* fill the last segment */
1069 while (sges_left) {
1070 if (sges_left == 1)
1071 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1072 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1073 else
1074 ioc->base_add_sg_single(sg_local, sgl_flags |
1075 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1076 sg_scmd = sg_next(sg_scmd);
1077 sg_local += ioc->sge_size;
1078 sges_left--;
1079 }
1080
1081 return 0;
1082}
1083
1084/**
Eric Moored5d135b2009-05-18 13:02:08 -06001085 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001086 * @sdev: scsi device struct
1087 * @qdepth: requested queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001088 * @reason: calling context
Eric Moore635374e2009-03-09 01:21:12 -06001089 *
1090 * Returns queue depth.
1091 */
1092static int
Mike Christiee881a172009-10-15 17:46:39 -07001093_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Eric Moore635374e2009-03-09 01:21:12 -06001094{
1095 struct Scsi_Host *shost = sdev->host;
1096 int max_depth;
1097 int tag_type;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301098 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1099 struct MPT2SAS_DEVICE *sas_device_priv_data;
1100 struct MPT2SAS_TARGET *sas_target_priv_data;
1101 struct _sas_device *sas_device;
1102 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001103
Mike Christiee881a172009-10-15 17:46:39 -07001104 if (reason != SCSI_QDEPTH_DEFAULT)
1105 return -EOPNOTSUPP;
1106
Eric Moore635374e2009-03-09 01:21:12 -06001107 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301108
1109 /* limit max device queue for SATA to 32 */
1110 sas_device_priv_data = sdev->hostdata;
1111 if (!sas_device_priv_data)
1112 goto not_sata;
1113 sas_target_priv_data = sas_device_priv_data->sas_target;
1114 if (!sas_target_priv_data)
1115 goto not_sata;
1116 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1117 goto not_sata;
1118 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1119 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1120 sas_device_priv_data->sas_target->sas_address);
1121 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1122 if (sas_device && sas_device->device_info &
1123 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1124 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1125
1126 not_sata:
1127
Eric Moore635374e2009-03-09 01:21:12 -06001128 if (!sdev->tagged_supported)
1129 max_depth = 1;
1130 if (qdepth > max_depth)
1131 qdepth = max_depth;
1132 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1133 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1134
1135 if (sdev->inquiry_len > 7)
1136 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1137 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1138 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1139 sdev->ordered_tags, sdev->scsi_level,
1140 (sdev->inquiry[7] & 2) >> 1);
1141
1142 return sdev->queue_depth;
1143}
1144
1145/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301146 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001147 * @sdev: scsi device struct
1148 * @tag_type: requested tag type
1149 *
1150 * Returns queue tag type.
1151 */
1152static int
Eric Moored5d135b2009-05-18 13:02:08 -06001153_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001154{
1155 if (sdev->tagged_supported) {
1156 scsi_set_tag_type(sdev, tag_type);
1157 if (tag_type)
1158 scsi_activate_tcq(sdev, sdev->queue_depth);
1159 else
1160 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1161 } else
1162 tag_type = 0;
1163
1164 return tag_type;
1165}
1166
1167/**
Eric Moored5d135b2009-05-18 13:02:08 -06001168 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001169 * @starget: scsi target struct
1170 *
1171 * Returns 0 if ok. Any other return is assumed to be an error and
1172 * the device is ignored.
1173 */
1174static int
Eric Moored5d135b2009-05-18 13:02:08 -06001175_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001176{
1177 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1178 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1179 struct MPT2SAS_TARGET *sas_target_priv_data;
1180 struct _sas_device *sas_device;
1181 struct _raid_device *raid_device;
1182 unsigned long flags;
1183 struct sas_rphy *rphy;
1184
1185 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1186 if (!sas_target_priv_data)
1187 return -ENOMEM;
1188
1189 starget->hostdata = sas_target_priv_data;
1190 sas_target_priv_data->starget = starget;
1191 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1192
1193 /* RAID volumes */
1194 if (starget->channel == RAID_CHANNEL) {
1195 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1196 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1197 starget->channel);
1198 if (raid_device) {
1199 sas_target_priv_data->handle = raid_device->handle;
1200 sas_target_priv_data->sas_address = raid_device->wwid;
1201 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1202 raid_device->starget = starget;
1203 }
1204 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1205 return 0;
1206 }
1207
1208 /* sas/sata devices */
1209 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1210 rphy = dev_to_rphy(starget->dev.parent);
1211 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1212 rphy->identify.sas_address);
1213
1214 if (sas_device) {
1215 sas_target_priv_data->handle = sas_device->handle;
1216 sas_target_priv_data->sas_address = sas_device->sas_address;
1217 sas_device->starget = starget;
1218 sas_device->id = starget->id;
1219 sas_device->channel = starget->channel;
1220 if (sas_device->hidden_raid_component)
1221 sas_target_priv_data->flags |=
1222 MPT_TARGET_FLAGS_RAID_COMPONENT;
1223 }
1224 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1225
1226 return 0;
1227}
1228
1229/**
Eric Moored5d135b2009-05-18 13:02:08 -06001230 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001231 * @starget: scsi target struct
1232 *
1233 * Returns nothing.
1234 */
1235static void
Eric Moored5d135b2009-05-18 13:02:08 -06001236_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001237{
1238 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1239 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1240 struct MPT2SAS_TARGET *sas_target_priv_data;
1241 struct _sas_device *sas_device;
1242 struct _raid_device *raid_device;
1243 unsigned long flags;
1244 struct sas_rphy *rphy;
1245
1246 sas_target_priv_data = starget->hostdata;
1247 if (!sas_target_priv_data)
1248 return;
1249
1250 if (starget->channel == RAID_CHANNEL) {
1251 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1252 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1253 starget->channel);
1254 if (raid_device) {
1255 raid_device->starget = NULL;
1256 raid_device->sdev = NULL;
1257 }
1258 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1259 goto out;
1260 }
1261
1262 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1263 rphy = dev_to_rphy(starget->dev.parent);
1264 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1265 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001266 if (sas_device && (sas_device->starget == starget) &&
1267 (sas_device->id == starget->id) &&
1268 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001269 sas_device->starget = NULL;
1270
1271 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1272
1273 out:
1274 kfree(sas_target_priv_data);
1275 starget->hostdata = NULL;
1276}
1277
1278/**
Eric Moored5d135b2009-05-18 13:02:08 -06001279 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001280 * @sdev: scsi device struct
1281 *
1282 * Returns 0 if ok. Any other return is assumed to be an error and
1283 * the device is ignored.
1284 */
1285static int
Eric Moored5d135b2009-05-18 13:02:08 -06001286_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001287{
1288 struct Scsi_Host *shost;
1289 struct MPT2SAS_ADAPTER *ioc;
1290 struct MPT2SAS_TARGET *sas_target_priv_data;
1291 struct MPT2SAS_DEVICE *sas_device_priv_data;
1292 struct scsi_target *starget;
1293 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001294 unsigned long flags;
1295
1296 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1297 if (!sas_device_priv_data)
1298 return -ENOMEM;
1299
1300 sas_device_priv_data->lun = sdev->lun;
1301 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1302
1303 starget = scsi_target(sdev);
1304 sas_target_priv_data = starget->hostdata;
1305 sas_target_priv_data->num_luns++;
1306 sas_device_priv_data->sas_target = sas_target_priv_data;
1307 sdev->hostdata = sas_device_priv_data;
1308 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1309 sdev->no_uld_attach = 1;
1310
1311 shost = dev_to_shost(&starget->dev);
1312 ioc = shost_priv(shost);
1313 if (starget->channel == RAID_CHANNEL) {
1314 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1315 raid_device = _scsih_raid_device_find_by_id(ioc,
1316 starget->id, starget->channel);
1317 if (raid_device)
1318 raid_device->sdev = sdev; /* raid is single lun */
1319 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001320 }
1321
Eric Moore635374e2009-03-09 01:21:12 -06001322 return 0;
1323}
1324
1325/**
Eric Moored5d135b2009-05-18 13:02:08 -06001326 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001327 * @sdev: scsi device struct
1328 *
1329 * Returns nothing.
1330 */
1331static void
Eric Moored5d135b2009-05-18 13:02:08 -06001332_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001333{
1334 struct MPT2SAS_TARGET *sas_target_priv_data;
1335 struct scsi_target *starget;
1336
1337 if (!sdev->hostdata)
1338 return;
1339
1340 starget = scsi_target(sdev);
1341 sas_target_priv_data = starget->hostdata;
1342 sas_target_priv_data->num_luns--;
1343 kfree(sdev->hostdata);
1344 sdev->hostdata = NULL;
1345}
1346
1347/**
Eric Moored5d135b2009-05-18 13:02:08 -06001348 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001349 * @ioc: per adapter object
1350 * @sas_device: the sas_device object
1351 * @sdev: scsi device struct
1352 */
1353static void
Eric Moored5d135b2009-05-18 13:02:08 -06001354_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001355 struct _sas_device *sas_device, struct scsi_device *sdev)
1356{
1357 Mpi2ConfigReply_t mpi_reply;
1358 Mpi2SasDevicePage0_t sas_device_pg0;
1359 u32 ioc_status;
1360 u16 flags;
1361 u32 device_info;
1362
1363 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1364 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1365 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1366 ioc->name, __FILE__, __LINE__, __func__);
1367 return;
1368 }
1369
1370 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1371 MPI2_IOCSTATUS_MASK;
1372 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1373 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1374 ioc->name, __FILE__, __LINE__, __func__);
1375 return;
1376 }
1377
1378 flags = le16_to_cpu(sas_device_pg0.Flags);
1379 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1380
1381 sdev_printk(KERN_INFO, sdev,
1382 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1383 "sw_preserve(%s)\n",
1384 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1385 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1386 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1387 "n",
1388 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1389 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1390 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1391}
1392
1393/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301394 * _scsih_is_raid - return boolean indicating device is raid volume
1395 * @dev the device struct object
1396 */
1397static int
1398_scsih_is_raid(struct device *dev)
1399{
1400 struct scsi_device *sdev = to_scsi_device(dev);
1401
1402 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1403}
1404
1405/**
1406 * _scsih_get_resync - get raid volume resync percent complete
1407 * @dev the device struct object
1408 */
1409static void
1410_scsih_get_resync(struct device *dev)
1411{
1412 struct scsi_device *sdev = to_scsi_device(dev);
1413 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1414 static struct _raid_device *raid_device;
1415 unsigned long flags;
1416 Mpi2RaidVolPage0_t vol_pg0;
1417 Mpi2ConfigReply_t mpi_reply;
1418 u32 volume_status_flags;
1419 u8 percent_complete = 0;
1420
1421 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1422 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1423 sdev->channel);
1424 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1425
1426 if (!raid_device)
1427 goto out;
1428
1429 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1430 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1431 sizeof(Mpi2RaidVolPage0_t))) {
1432 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1433 ioc->name, __FILE__, __LINE__, __func__);
1434 goto out;
1435 }
1436
1437 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1438 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1439 percent_complete = raid_device->percent_complete;
1440 out:
1441 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1442}
1443
1444/**
1445 * _scsih_get_state - get raid volume level
1446 * @dev the device struct object
1447 */
1448static void
1449_scsih_get_state(struct device *dev)
1450{
1451 struct scsi_device *sdev = to_scsi_device(dev);
1452 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1453 static struct _raid_device *raid_device;
1454 unsigned long flags;
1455 Mpi2RaidVolPage0_t vol_pg0;
1456 Mpi2ConfigReply_t mpi_reply;
1457 u32 volstate;
1458 enum raid_state state = RAID_STATE_UNKNOWN;
1459
1460 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1461 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1462 sdev->channel);
1463 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1464
1465 if (!raid_device)
1466 goto out;
1467
1468 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1469 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1470 sizeof(Mpi2RaidVolPage0_t))) {
1471 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1472 ioc->name, __FILE__, __LINE__, __func__);
1473 goto out;
1474 }
1475
1476 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1477 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1478 state = RAID_STATE_RESYNCING;
1479 goto out;
1480 }
1481
1482 switch (vol_pg0.VolumeState) {
1483 case MPI2_RAID_VOL_STATE_OPTIMAL:
1484 case MPI2_RAID_VOL_STATE_ONLINE:
1485 state = RAID_STATE_ACTIVE;
1486 break;
1487 case MPI2_RAID_VOL_STATE_DEGRADED:
1488 state = RAID_STATE_DEGRADED;
1489 break;
1490 case MPI2_RAID_VOL_STATE_FAILED:
1491 case MPI2_RAID_VOL_STATE_MISSING:
1492 state = RAID_STATE_OFFLINE;
1493 break;
1494 }
1495 out:
1496 raid_set_state(mpt2sas_raid_template, dev, state);
1497}
1498
1499/**
1500 * _scsih_set_level - set raid level
1501 * @sdev: scsi device struct
1502 * @raid_device: raid_device object
1503 */
1504static void
1505_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1506{
1507 enum raid_level level = RAID_LEVEL_UNKNOWN;
1508
1509 switch (raid_device->volume_type) {
1510 case MPI2_RAID_VOL_TYPE_RAID0:
1511 level = RAID_LEVEL_0;
1512 break;
1513 case MPI2_RAID_VOL_TYPE_RAID10:
1514 level = RAID_LEVEL_10;
1515 break;
1516 case MPI2_RAID_VOL_TYPE_RAID1E:
1517 level = RAID_LEVEL_1E;
1518 break;
1519 case MPI2_RAID_VOL_TYPE_RAID1:
1520 level = RAID_LEVEL_1;
1521 break;
1522 }
1523
1524 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1525}
1526
1527/**
Eric Moore635374e2009-03-09 01:21:12 -06001528 * _scsih_get_volume_capabilities - volume capabilities
1529 * @ioc: per adapter object
1530 * @sas_device: the raid_device object
1531 */
1532static void
1533_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1534 struct _raid_device *raid_device)
1535{
1536 Mpi2RaidVolPage0_t *vol_pg0;
1537 Mpi2RaidPhysDiskPage0_t pd_pg0;
1538 Mpi2SasDevicePage0_t sas_device_pg0;
1539 Mpi2ConfigReply_t mpi_reply;
1540 u16 sz;
1541 u8 num_pds;
1542
1543 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1544 &num_pds)) || !num_pds) {
1545 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1546 ioc->name, __FILE__, __LINE__, __func__);
1547 return;
1548 }
1549
1550 raid_device->num_pds = num_pds;
1551 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1552 sizeof(Mpi2RaidVol0PhysDisk_t));
1553 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1554 if (!vol_pg0) {
1555 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1556 ioc->name, __FILE__, __LINE__, __func__);
1557 return;
1558 }
1559
1560 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1561 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1562 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1563 ioc->name, __FILE__, __LINE__, __func__);
1564 kfree(vol_pg0);
1565 return;
1566 }
1567
1568 raid_device->volume_type = vol_pg0->VolumeType;
1569
1570 /* figure out what the underlying devices are by
1571 * obtaining the device_info bits for the 1st device
1572 */
1573 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1574 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1575 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1576 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1577 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1578 le16_to_cpu(pd_pg0.DevHandle)))) {
1579 raid_device->device_info =
1580 le32_to_cpu(sas_device_pg0.DeviceInfo);
1581 }
1582 }
1583
1584 kfree(vol_pg0);
1585}
1586
1587/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301588 * _scsih_enable_tlr - setting TLR flags
1589 * @ioc: per adapter object
1590 * @sdev: scsi device struct
1591 *
1592 * Enabling Transaction Layer Retries for tape devices when
1593 * vpd page 0x90 is present
1594 *
1595 */
1596static void
1597_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1598{
1599 /* only for TAPE */
1600 if (sdev->type != TYPE_TAPE)
1601 return;
1602
1603 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1604 return;
1605
1606 sas_enable_tlr(sdev);
1607 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1608 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1609 return;
1610
1611}
1612
1613/**
Eric Moored5d135b2009-05-18 13:02:08 -06001614 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001615 * @sdev: scsi device struct
1616 *
1617 * Returns 0 if ok. Any other return is assumed to be an error and
1618 * the device is ignored.
1619 */
1620static int
Eric Moored5d135b2009-05-18 13:02:08 -06001621_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001622{
1623 struct Scsi_Host *shost = sdev->host;
1624 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1625 struct MPT2SAS_DEVICE *sas_device_priv_data;
1626 struct MPT2SAS_TARGET *sas_target_priv_data;
1627 struct _sas_device *sas_device;
1628 struct _raid_device *raid_device;
1629 unsigned long flags;
1630 int qdepth;
1631 u8 ssp_target = 0;
1632 char *ds = "";
1633 char *r_level = "";
1634
1635 qdepth = 1;
1636 sas_device_priv_data = sdev->hostdata;
1637 sas_device_priv_data->configured_lun = 1;
1638 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1639 sas_target_priv_data = sas_device_priv_data->sas_target;
1640
1641 /* raid volume handling */
1642 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1643
1644 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1645 raid_device = _scsih_raid_device_find_by_handle(ioc,
1646 sas_target_priv_data->handle);
1647 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1648 if (!raid_device) {
1649 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1650 ioc->name, __FILE__, __LINE__, __func__);
1651 return 0;
1652 }
1653
1654 _scsih_get_volume_capabilities(ioc, raid_device);
1655
1656 /* RAID Queue Depth Support
1657 * IS volume = underlying qdepth of drive type, either
1658 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1659 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1660 */
1661 if (raid_device->device_info &
1662 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1663 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1664 ds = "SSP";
1665 } else {
1666 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1667 if (raid_device->device_info &
1668 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1669 ds = "SATA";
1670 else
1671 ds = "STP";
1672 }
1673
1674 switch (raid_device->volume_type) {
1675 case MPI2_RAID_VOL_TYPE_RAID0:
1676 r_level = "RAID0";
1677 break;
1678 case MPI2_RAID_VOL_TYPE_RAID1E:
1679 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301680 if (ioc->manu_pg10.OEMIdentifier &&
1681 (ioc->manu_pg10.GenericFlags0 &
1682 MFG10_GF0_R10_DISPLAY) &&
1683 !(raid_device->num_pds % 2))
1684 r_level = "RAID10";
1685 else
1686 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001687 break;
1688 case MPI2_RAID_VOL_TYPE_RAID1:
1689 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1690 r_level = "RAID1";
1691 break;
1692 case MPI2_RAID_VOL_TYPE_RAID10:
1693 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1694 r_level = "RAID10";
1695 break;
1696 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1697 default:
1698 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1699 r_level = "RAIDX";
1700 break;
1701 }
1702
1703 sdev_printk(KERN_INFO, sdev, "%s: "
1704 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1705 r_level, raid_device->handle,
1706 (unsigned long long)raid_device->wwid,
1707 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001708 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301709 /* raid transport support */
1710 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001711 return 0;
1712 }
1713
1714 /* non-raid handling */
1715 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1716 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1717 sas_device_priv_data->sas_target->sas_address);
1718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1719 if (sas_device) {
1720 if (sas_target_priv_data->flags &
1721 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1722 mpt2sas_config_get_volume_handle(ioc,
1723 sas_device->handle, &sas_device->volume_handle);
1724 mpt2sas_config_get_volume_wwid(ioc,
1725 sas_device->volume_handle,
1726 &sas_device->volume_wwid);
1727 }
1728 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1729 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1730 ssp_target = 1;
1731 ds = "SSP";
1732 } else {
1733 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1734 if (sas_device->device_info &
1735 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1736 ds = "STP";
1737 else if (sas_device->device_info &
1738 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1739 ds = "SATA";
1740 }
1741
1742 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1743 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1744 ds, sas_device->handle,
1745 (unsigned long long)sas_device->sas_address,
1746 (unsigned long long)sas_device->device_name);
1747 sdev_printk(KERN_INFO, sdev, "%s: "
1748 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1749 (unsigned long long) sas_device->enclosure_logical_id,
1750 sas_device->slot);
1751
1752 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001753 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001754 }
1755
Mike Christiee881a172009-10-15 17:46:39 -07001756 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001757
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301758 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001759 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301760 _scsih_enable_tlr(ioc, sdev);
1761 }
Eric Moore635374e2009-03-09 01:21:12 -06001762 return 0;
1763}
1764
1765/**
Eric Moored5d135b2009-05-18 13:02:08 -06001766 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001767 * @sdev: scsi device struct
1768 * @bdev: pointer to block device context
1769 * @capacity: device size (in 512 byte sectors)
1770 * @params: three element array to place output:
1771 * params[0] number of heads (max 255)
1772 * params[1] number of sectors (max 63)
1773 * params[2] number of cylinders
1774 *
1775 * Return nothing.
1776 */
1777static int
Eric Moored5d135b2009-05-18 13:02:08 -06001778_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001779 sector_t capacity, int params[])
1780{
1781 int heads;
1782 int sectors;
1783 sector_t cylinders;
1784 ulong dummy;
1785
1786 heads = 64;
1787 sectors = 32;
1788
1789 dummy = heads * sectors;
1790 cylinders = capacity;
1791 sector_div(cylinders, dummy);
1792
1793 /*
1794 * Handle extended translation size for logical drives
1795 * > 1Gb
1796 */
1797 if ((ulong)capacity >= 0x200000) {
1798 heads = 255;
1799 sectors = 63;
1800 dummy = heads * sectors;
1801 cylinders = capacity;
1802 sector_div(cylinders, dummy);
1803 }
1804
1805 /* return result */
1806 params[0] = heads;
1807 params[1] = sectors;
1808 params[2] = cylinders;
1809
1810 return 0;
1811}
1812
1813/**
1814 * _scsih_response_code - translation of device response code
1815 * @ioc: per adapter object
1816 * @response_code: response code returned by the device
1817 *
1818 * Return nothing.
1819 */
1820static void
1821_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1822{
1823 char *desc;
1824
1825 switch (response_code) {
1826 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1827 desc = "task management request completed";
1828 break;
1829 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1830 desc = "invalid frame";
1831 break;
1832 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1833 desc = "task management request not supported";
1834 break;
1835 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1836 desc = "task management request failed";
1837 break;
1838 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1839 desc = "task management request succeeded";
1840 break;
1841 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1842 desc = "invalid lun";
1843 break;
1844 case 0xA:
1845 desc = "overlapped tag attempted";
1846 break;
1847 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1848 desc = "task queued, however not sent to target";
1849 break;
1850 default:
1851 desc = "unknown";
1852 break;
1853 }
1854 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1855 ioc->name, response_code, desc);
1856}
1857
1858/**
Eric Moored5d135b2009-05-18 13:02:08 -06001859 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001860 * @ioc: per adapter object
1861 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301862 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001863 * @reply: reply message frame(lower 32bit addr)
1864 * Context: none.
1865 *
1866 * The callback handler when using scsih_issue_tm.
1867 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301868 * Return 1 meaning mf should be freed from _base_interrupt
1869 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001870 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301871static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301872_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001873{
1874 MPI2DefaultReply_t *mpi_reply;
1875
1876 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301877 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001878 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301879 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001880 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1881 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1882 if (mpi_reply) {
1883 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1884 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1885 }
1886 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1887 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301888 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001889}
1890
1891/**
1892 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1893 * @ioc: per adapter object
1894 * @handle: device handle
1895 *
1896 * During taskmangement request, we need to freeze the device queue.
1897 */
1898void
1899mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1900{
1901 struct MPT2SAS_DEVICE *sas_device_priv_data;
1902 struct scsi_device *sdev;
1903 u8 skip = 0;
1904
1905 shost_for_each_device(sdev, ioc->shost) {
1906 if (skip)
1907 continue;
1908 sas_device_priv_data = sdev->hostdata;
1909 if (!sas_device_priv_data)
1910 continue;
1911 if (sas_device_priv_data->sas_target->handle == handle) {
1912 sas_device_priv_data->sas_target->tm_busy = 1;
1913 skip = 1;
1914 ioc->ignore_loginfos = 1;
1915 }
1916 }
1917}
1918
1919/**
1920 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1921 * @ioc: per adapter object
1922 * @handle: device handle
1923 *
1924 * During taskmangement request, we need to freeze the device queue.
1925 */
1926void
1927mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1928{
1929 struct MPT2SAS_DEVICE *sas_device_priv_data;
1930 struct scsi_device *sdev;
1931 u8 skip = 0;
1932
1933 shost_for_each_device(sdev, ioc->shost) {
1934 if (skip)
1935 continue;
1936 sas_device_priv_data = sdev->hostdata;
1937 if (!sas_device_priv_data)
1938 continue;
1939 if (sas_device_priv_data->sas_target->handle == handle) {
1940 sas_device_priv_data->sas_target->tm_busy = 0;
1941 skip = 1;
1942 ioc->ignore_loginfos = 0;
1943 }
1944 }
1945}
1946
1947/**
1948 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1949 * @ioc: per adapter struct
1950 * @device_handle: device handle
1951 * @lun: lun number
1952 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1953 * @smid_task: smid assigned to the task
1954 * @timeout: timeout in seconds
1955 * Context: The calling function needs to acquire the tm_cmds.mutex
1956 *
1957 * A generic API for sending task management requests to firmware.
1958 *
1959 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1960 * this API.
1961 *
1962 * The callback index is set inside `ioc->tm_cb_idx`.
1963 *
1964 * Return nothing.
1965 */
1966void
1967mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1968 u8 type, u16 smid_task, ulong timeout)
1969{
1970 Mpi2SCSITaskManagementRequest_t *mpi_request;
1971 Mpi2SCSITaskManagementReply_t *mpi_reply;
1972 u16 smid = 0;
1973 u32 ioc_state;
1974 unsigned long timeleft;
Eric Moore635374e2009-03-09 01:21:12 -06001975
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301976 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1977 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1978 __func__, ioc->name);
1979 return;
1980 }
1981
1982 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001983 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1984 __func__, ioc->name);
1985 return;
1986 }
Eric Moore635374e2009-03-09 01:21:12 -06001987
1988 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1989 if (ioc_state & MPI2_DOORBELL_USED) {
1990 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1991 "active!\n", ioc->name));
1992 goto issue_host_reset;
1993 }
1994
1995 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1996 mpt2sas_base_fault_info(ioc, ioc_state &
1997 MPI2_DOORBELL_DATA_MASK);
1998 goto issue_host_reset;
1999 }
2000
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302001 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002002 if (!smid) {
2003 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2004 ioc->name, __func__);
2005 return;
2006 }
2007
2008 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302009 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2010 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002011 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2012 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2013 ioc->tm_cmds.smid = smid;
2014 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2015 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2016 mpi_request->DevHandle = cpu_to_le16(handle);
2017 mpi_request->TaskType = type;
2018 mpi_request->TaskMID = cpu_to_le16(smid_task);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302019 mpi_request->VP_ID = 0; /* TODO */
2020 mpi_request->VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002021 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2022 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302023 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302024 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002025 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2026 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2027 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2028 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2029 ioc->name, __func__);
2030 _debug_dump_mf(mpi_request,
2031 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2032 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
2033 goto issue_host_reset;
2034 }
2035
2036 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2037 mpi_reply = ioc->tm_cmds.reply;
2038 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2039 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2040 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2041 le32_to_cpu(mpi_reply->IOCLogInfo),
2042 le32_to_cpu(mpi_reply->TerminationCount)));
2043 if (ioc->logging_level & MPT_DEBUG_TM)
2044 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2045 }
2046 return;
2047 issue_host_reset:
2048 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
2049}
2050
2051/**
Eric Moored5d135b2009-05-18 13:02:08 -06002052 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06002053 * @sdev: scsi device struct
2054 *
2055 * Returns SUCCESS if command aborted else FAILED
2056 */
2057static int
Eric Moored5d135b2009-05-18 13:02:08 -06002058_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002059{
2060 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2061 struct MPT2SAS_DEVICE *sas_device_priv_data;
2062 u16 smid;
2063 u16 handle;
2064 int r;
2065 struct scsi_cmnd *scmd_lookup;
2066
2067 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
2068 ioc->name, scmd);
2069 scsi_print_command(scmd);
2070
2071 sas_device_priv_data = scmd->device->hostdata;
2072 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2073 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2074 ioc->name, scmd);
2075 scmd->result = DID_NO_CONNECT << 16;
2076 scmd->scsi_done(scmd);
2077 r = SUCCESS;
2078 goto out;
2079 }
2080
2081 /* search for the command */
2082 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2083 if (!smid) {
2084 scmd->result = DID_RESET << 16;
2085 r = SUCCESS;
2086 goto out;
2087 }
2088
2089 /* for hidden raid components and volumes this is not supported */
2090 if (sas_device_priv_data->sas_target->flags &
2091 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2092 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2093 scmd->result = DID_RESET << 16;
2094 r = FAILED;
2095 goto out;
2096 }
2097
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302098 mpt2sas_halt_firmware(ioc);
2099
Eric Moore635374e2009-03-09 01:21:12 -06002100 mutex_lock(&ioc->tm_cmds.mutex);
2101 handle = sas_device_priv_data->sas_target->handle;
2102 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
2103 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
2104
2105 /* sanity check - see whether command actually completed */
2106 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
2107 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
2108 r = FAILED;
2109 else
2110 r = SUCCESS;
2111 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2112 mutex_unlock(&ioc->tm_cmds.mutex);
2113
2114 out:
2115 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
2116 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2117 return r;
2118}
2119
Eric Moore635374e2009-03-09 01:21:12 -06002120/**
Eric Moored5d135b2009-05-18 13:02:08 -06002121 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002122 * @sdev: scsi device struct
2123 *
2124 * Returns SUCCESS if command aborted else FAILED
2125 */
2126static int
Eric Moored5d135b2009-05-18 13:02:08 -06002127_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002128{
2129 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2130 struct MPT2SAS_DEVICE *sas_device_priv_data;
2131 struct _sas_device *sas_device;
2132 unsigned long flags;
2133 u16 handle;
2134 int r;
2135
Eric Moore993e0da2009-05-18 13:00:45 -06002136 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002137 ioc->name, scmd);
2138 scsi_print_command(scmd);
2139
2140 sas_device_priv_data = scmd->device->hostdata;
2141 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2142 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2143 ioc->name, scmd);
2144 scmd->result = DID_NO_CONNECT << 16;
2145 scmd->scsi_done(scmd);
2146 r = SUCCESS;
2147 goto out;
2148 }
2149
2150 /* for hidden raid components obtain the volume_handle */
2151 handle = 0;
2152 if (sas_device_priv_data->sas_target->flags &
2153 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2154 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2155 sas_device = _scsih_sas_device_find_by_handle(ioc,
2156 sas_device_priv_data->sas_target->handle);
2157 if (sas_device)
2158 handle = sas_device->volume_handle;
2159 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2160 } else
2161 handle = sas_device_priv_data->sas_target->handle;
2162
2163 if (!handle) {
2164 scmd->result = DID_RESET << 16;
2165 r = FAILED;
2166 goto out;
2167 }
2168
2169 mutex_lock(&ioc->tm_cmds.mutex);
2170 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06002171 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2172 30);
2173
2174 /*
2175 * sanity check see whether all commands to this device been
2176 * completed
2177 */
2178 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2179 scmd->device->lun, scmd->device->channel))
2180 r = FAILED;
2181 else
2182 r = SUCCESS;
2183 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2184 mutex_unlock(&ioc->tm_cmds.mutex);
2185
2186 out:
2187 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2188 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2189 return r;
2190}
2191
2192/**
Eric Moored5d135b2009-05-18 13:02:08 -06002193 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002194 * @sdev: scsi device struct
2195 *
2196 * Returns SUCCESS if command aborted else FAILED
2197 */
2198static int
Eric Moored5d135b2009-05-18 13:02:08 -06002199_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002200{
2201 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2202 struct MPT2SAS_DEVICE *sas_device_priv_data;
2203 struct _sas_device *sas_device;
2204 unsigned long flags;
2205 u16 handle;
2206 int r;
2207
2208 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2209 ioc->name, scmd);
2210 scsi_print_command(scmd);
2211
2212 sas_device_priv_data = scmd->device->hostdata;
2213 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2214 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2215 ioc->name, scmd);
2216 scmd->result = DID_NO_CONNECT << 16;
2217 scmd->scsi_done(scmd);
2218 r = SUCCESS;
2219 goto out;
2220 }
2221
2222 /* for hidden raid components obtain the volume_handle */
2223 handle = 0;
2224 if (sas_device_priv_data->sas_target->flags &
2225 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2226 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2227 sas_device = _scsih_sas_device_find_by_handle(ioc,
2228 sas_device_priv_data->sas_target->handle);
2229 if (sas_device)
2230 handle = sas_device->volume_handle;
2231 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2232 } else
2233 handle = sas_device_priv_data->sas_target->handle;
2234
2235 if (!handle) {
2236 scmd->result = DID_RESET << 16;
2237 r = FAILED;
2238 goto out;
2239 }
2240
2241 mutex_lock(&ioc->tm_cmds.mutex);
2242 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002243 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2244
2245 /*
2246 * sanity check see whether all commands to this target been
2247 * completed
2248 */
2249 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2250 scmd->device->channel))
2251 r = FAILED;
2252 else
2253 r = SUCCESS;
2254 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2255 mutex_unlock(&ioc->tm_cmds.mutex);
2256
2257 out:
2258 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2259 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2260 return r;
2261}
2262
2263/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302264 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002265 * @sdev: scsi device struct
2266 *
2267 * Returns SUCCESS if command aborted else FAILED
2268 */
2269static int
Eric Moored5d135b2009-05-18 13:02:08 -06002270_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002271{
2272 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2273 int r, retval;
2274
2275 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2276 ioc->name, scmd);
2277 scsi_print_command(scmd);
2278
2279 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2280 FORCE_BIG_HAMMER);
2281 r = (retval < 0) ? FAILED : SUCCESS;
2282 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2283 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2284
2285 return r;
2286}
2287
2288/**
2289 * _scsih_fw_event_add - insert and queue up fw_event
2290 * @ioc: per adapter object
2291 * @fw_event: object describing the event
2292 * Context: This function will acquire ioc->fw_event_lock.
2293 *
2294 * This adds the firmware event object into link list, then queues it up to
2295 * be processed from user context.
2296 *
2297 * Return nothing.
2298 */
2299static void
2300_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2301{
2302 unsigned long flags;
2303
2304 if (ioc->firmware_event_thread == NULL)
2305 return;
2306
2307 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2308 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302309 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2310 queue_delayed_work(ioc->firmware_event_thread,
2311 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002312 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2313}
2314
2315/**
2316 * _scsih_fw_event_free - delete fw_event
2317 * @ioc: per adapter object
2318 * @fw_event: object describing the event
2319 * Context: This function will acquire ioc->fw_event_lock.
2320 *
2321 * This removes firmware event object from link list, frees associated memory.
2322 *
2323 * Return nothing.
2324 */
2325static void
2326_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2327 *fw_event)
2328{
2329 unsigned long flags;
2330
2331 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2332 list_del(&fw_event->list);
2333 kfree(fw_event->event_data);
2334 kfree(fw_event);
2335 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2336}
2337
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302338
Eric Moore635374e2009-03-09 01:21:12 -06002339/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302340 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002341 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002342 *
2343 * Return nothing.
2344 */
2345static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302346_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002347{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302348 struct fw_event_work *fw_event;
2349
2350 if (ioc->wait_for_port_enable_to_complete)
2351 return;
2352 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2353 if (!fw_event)
2354 return;
2355 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2356 fw_event->ioc = ioc;
2357 _scsih_fw_event_add(ioc, fw_event);
2358}
2359
2360/**
2361 * _scsih_fw_event_cleanup_queue - cleanup event queue
2362 * @ioc: per adapter object
2363 *
2364 * Walk the firmware event queue, either killing timers, or waiting
2365 * for outstanding events to complete
2366 *
2367 * Return nothing.
2368 */
2369static void
2370_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2371{
2372 struct fw_event_work *fw_event, *next;
2373
2374 if (list_empty(&ioc->fw_event_list) ||
2375 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002376 return;
2377
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302378 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2379 if (cancel_delayed_work(&fw_event->delayed_work)) {
2380 _scsih_fw_event_free(ioc, fw_event);
2381 continue;
2382 }
2383 fw_event->cancel_pending_work = 1;
2384 }
Eric Moore635374e2009-03-09 01:21:12 -06002385}
2386
Eric Moore635374e2009-03-09 01:21:12 -06002387
2388/**
2389 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2390 * @ioc: per adapter object
2391 * @handle: device handle
2392 *
2393 * During device pull we need to appropiately set the sdev state.
2394 */
2395static void
2396_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2397{
2398 struct MPT2SAS_DEVICE *sas_device_priv_data;
2399 struct scsi_device *sdev;
2400
2401 shost_for_each_device(sdev, ioc->shost) {
2402 sas_device_priv_data = sdev->hostdata;
2403 if (!sas_device_priv_data)
2404 continue;
2405 if (!sas_device_priv_data->block)
2406 continue;
2407 if (sas_device_priv_data->sas_target->handle == handle) {
2408 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2409 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2410 "handle(0x%04x)\n", ioc->name, handle));
2411 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302412 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002413 }
2414 }
2415}
2416
2417/**
2418 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2419 * @ioc: per adapter object
2420 * @handle: device handle
2421 *
2422 * During device pull we need to appropiately set the sdev state.
2423 */
2424static void
2425_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2426{
2427 struct MPT2SAS_DEVICE *sas_device_priv_data;
2428 struct scsi_device *sdev;
2429
2430 shost_for_each_device(sdev, ioc->shost) {
2431 sas_device_priv_data = sdev->hostdata;
2432 if (!sas_device_priv_data)
2433 continue;
2434 if (sas_device_priv_data->block)
2435 continue;
2436 if (sas_device_priv_data->sas_target->handle == handle) {
2437 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2438 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2439 "handle(0x%04x)\n", ioc->name, handle));
2440 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302441 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002442 }
2443 }
2444}
2445
2446/**
2447 * _scsih_block_io_to_children_attached_to_ex
2448 * @ioc: per adapter object
2449 * @sas_expander: the sas_device object
2450 *
2451 * This routine set sdev state to SDEV_BLOCK for all devices
2452 * attached to this expander. This function called when expander is
2453 * pulled.
2454 */
2455static void
2456_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2457 struct _sas_node *sas_expander)
2458{
2459 struct _sas_port *mpt2sas_port;
2460 struct _sas_device *sas_device;
2461 struct _sas_node *expander_sibling;
2462 unsigned long flags;
2463
2464 if (!sas_expander)
2465 return;
2466
2467 list_for_each_entry(mpt2sas_port,
2468 &sas_expander->sas_port_list, port_list) {
2469 if (mpt2sas_port->remote_identify.device_type ==
2470 SAS_END_DEVICE) {
2471 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2472 sas_device =
2473 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2474 mpt2sas_port->remote_identify.sas_address);
2475 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2476 if (!sas_device)
2477 continue;
2478 _scsih_block_io_device(ioc, sas_device->handle);
2479 }
2480 }
2481
2482 list_for_each_entry(mpt2sas_port,
2483 &sas_expander->sas_port_list, port_list) {
2484
2485 if (mpt2sas_port->remote_identify.device_type ==
2486 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2487 mpt2sas_port->remote_identify.device_type ==
2488 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2489
2490 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2491 expander_sibling =
2492 mpt2sas_scsih_expander_find_by_sas_address(
2493 ioc, mpt2sas_port->remote_identify.sas_address);
2494 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2495 _scsih_block_io_to_children_attached_to_ex(ioc,
2496 expander_sibling);
2497 }
2498 }
2499}
2500
2501/**
2502 * _scsih_block_io_to_children_attached_directly
2503 * @ioc: per adapter object
2504 * @event_data: topology change event data
2505 *
2506 * This routine set sdev state to SDEV_BLOCK for all devices
2507 * direct attached during device pull.
2508 */
2509static void
2510_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2511 Mpi2EventDataSasTopologyChangeList_t *event_data)
2512{
2513 int i;
2514 u16 handle;
2515 u16 reason_code;
2516 u8 phy_number;
2517
2518 for (i = 0; i < event_data->NumEntries; i++) {
2519 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2520 if (!handle)
2521 continue;
2522 phy_number = event_data->StartPhyNum + i;
2523 reason_code = event_data->PHY[i].PhyStatus &
2524 MPI2_EVENT_SAS_TOPO_RC_MASK;
2525 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2526 _scsih_block_io_device(ioc, handle);
2527 }
2528}
2529
2530/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302531 * _scsih_tm_tr_send - send task management request
2532 * @ioc: per adapter object
2533 * @handle: device handle
2534 * Context: interrupt time.
2535 *
2536 * This code is to initiate the device removal handshake protocal
2537 * with controller firmware. This function will issue target reset
2538 * using high priority request queue. It will send a sas iounit
2539 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2540 *
2541 * This is designed to send muliple task management request at the same
2542 * time to the fifo. If the fifo is full, we will append the request,
2543 * and process it in a future completion.
2544 */
2545static void
2546_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2547{
2548 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302549 u16 smid;
2550 struct _sas_device *sas_device;
2551 unsigned long flags;
2552 struct _tr_list *delayed_tr;
2553
Kashyap, Desai1278b112010-03-09 17:34:13 +05302554 if (ioc->shost_recovery || ioc->remove_host) {
2555 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2556 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302557 return;
2558 }
2559
2560 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2561 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302562 if (sas_device && sas_device->hidden_raid_component) {
2563 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302564 return;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302565 }
2566 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302567
2568 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2569 if (!smid) {
2570 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2571 if (!delayed_tr)
2572 return;
2573 INIT_LIST_HEAD(&delayed_tr->list);
2574 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302575 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2576 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2577 "DELAYED:tr:handle(0x%04x), (open)\n",
2578 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302579 return;
2580 }
2581
Kashyap, Desai1278b112010-03-09 17:34:13 +05302582 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2583 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2584 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302585 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2586 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2587 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2588 mpi_request->DevHandle = cpu_to_le16(handle);
2589 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302590 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2591}
2592
2593
2594
2595/**
2596 * _scsih_sas_control_complete - completion routine
2597 * @ioc: per adapter object
2598 * @smid: system request message index
2599 * @msix_index: MSIX table index supplied by the OS
2600 * @reply: reply message frame(lower 32bit addr)
2601 * Context: interrupt time.
2602 *
2603 * This is the sas iounit controll completion routine.
2604 * This code is part of the code to initiate the device removal
2605 * handshake protocal with controller firmware.
2606 *
2607 * Return 1 meaning mf should be freed from _base_interrupt
2608 * 0 means the mf is freed from this function.
2609 */
2610static u8
2611_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2612 u8 msix_index, u32 reply)
2613{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302614 Mpi2SasIoUnitControlReply_t *mpi_reply =
2615 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2616
Kashyap, Desai1278b112010-03-09 17:34:13 +05302617 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2618 "sc_complete:handle(0x%04x), (open) "
2619 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2620 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2621 le16_to_cpu(mpi_reply->IOCStatus),
2622 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302623 return 1;
2624}
2625
2626/**
2627 * _scsih_tm_tr_complete -
2628 * @ioc: per adapter object
2629 * @smid: system request message index
2630 * @msix_index: MSIX table index supplied by the OS
2631 * @reply: reply message frame(lower 32bit addr)
2632 * Context: interrupt time.
2633 *
2634 * This is the target reset completion routine.
2635 * This code is part of the code to initiate the device removal
2636 * handshake protocal with controller firmware.
2637 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2638 *
2639 * Return 1 meaning mf should be freed from _base_interrupt
2640 * 0 means the mf is freed from this function.
2641 */
2642static u8
2643_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2644 u32 reply)
2645{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302646 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302647 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302648 Mpi2SCSITaskManagementReply_t *mpi_reply =
2649 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2650 Mpi2SasIoUnitControlRequest_t *mpi_request;
2651 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302652 struct _tr_list *delayed_tr;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302653
Kashyap, Desai1278b112010-03-09 17:34:13 +05302654 if (ioc->shost_recovery || ioc->remove_host) {
2655 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2656 "progress!\n", __func__, ioc->name));
2657 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302658 }
2659
Kashyap, Desai1278b112010-03-09 17:34:13 +05302660 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2661 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2662 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2663 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2664 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2665 le16_to_cpu(mpi_reply->DevHandle), smid));
2666 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302667 }
2668
Kashyap, Desai1278b112010-03-09 17:34:13 +05302669 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2670 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2671 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2672 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2673 le32_to_cpu(mpi_reply->IOCLogInfo),
2674 le32_to_cpu(mpi_reply->TerminationCount)));
2675
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302676 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2677 if (!smid_sas_ctrl) {
2678 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2679 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302680 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302681 }
2682
Kashyap, Desai1278b112010-03-09 17:34:13 +05302683 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2684 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2685 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302686 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2687 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2688 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2689 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302690 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302691 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302692
2693 if (!list_empty(&ioc->delayed_tr_list)) {
2694 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2695 struct _tr_list, list);
2696 mpt2sas_base_free_smid(ioc, smid);
2697 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2698 list_del(&delayed_tr->list);
2699 kfree(delayed_tr);
2700 return 0; /* tells base_interrupt not to free mf */
2701 }
2702 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302703}
2704
2705/**
Eric Moore635374e2009-03-09 01:21:12 -06002706 * _scsih_check_topo_delete_events - sanity check on topo events
2707 * @ioc: per adapter object
2708 * @event_data: the event data payload
2709 *
2710 * This routine added to better handle cable breaker.
2711 *
2712 * This handles the case where driver recieves multiple expander
2713 * add and delete events in a single shot. When there is a delete event
2714 * the routine will void any pending add events waiting in the event queue.
2715 *
2716 * Return nothing.
2717 */
2718static void
2719_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2720 Mpi2EventDataSasTopologyChangeList_t *event_data)
2721{
2722 struct fw_event_work *fw_event;
2723 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2724 u16 expander_handle;
2725 struct _sas_node *sas_expander;
2726 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302727 int i, reason_code;
2728 u16 handle;
2729
2730 for (i = 0 ; i < event_data->NumEntries; i++) {
2731 if (event_data->PHY[i].PhyStatus &
2732 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2733 continue;
2734 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2735 if (!handle)
2736 continue;
2737 reason_code = event_data->PHY[i].PhyStatus &
2738 MPI2_EVENT_SAS_TOPO_RC_MASK;
2739 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2740 _scsih_tm_tr_send(ioc, handle);
2741 }
Eric Moore635374e2009-03-09 01:21:12 -06002742
2743 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2744 if (expander_handle < ioc->sas_hba.num_phys) {
2745 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2746 return;
2747 }
2748
2749 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2750 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2751 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2752 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2753 expander_handle);
2754 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2755 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2756 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2757 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2758
2759 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2760 return;
2761
2762 /* mark ignore flag for pending events */
2763 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2764 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2765 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2766 fw_event->ignore)
2767 continue;
2768 local_event_data = fw_event->event_data;
2769 if (local_event_data->ExpStatus ==
2770 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2771 local_event_data->ExpStatus ==
2772 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2773 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2774 expander_handle) {
2775 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2776 "setting ignoring flag\n", ioc->name));
2777 fw_event->ignore = 1;
2778 }
2779 }
2780 }
2781 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2782}
2783
2784/**
Eric Moore635374e2009-03-09 01:21:12 -06002785 * _scsih_flush_running_cmds - completing outstanding commands.
2786 * @ioc: per adapter object
2787 *
2788 * The flushing out of all pending scmd commands following host reset,
2789 * where all IO is dropped to the floor.
2790 *
2791 * Return nothing.
2792 */
2793static void
2794_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2795{
2796 struct scsi_cmnd *scmd;
2797 u16 smid;
2798 u16 count = 0;
2799
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302800 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2801 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002802 if (!scmd)
2803 continue;
2804 count++;
2805 mpt2sas_base_free_smid(ioc, smid);
2806 scsi_dma_unmap(scmd);
2807 scmd->result = DID_RESET << 16;
2808 scmd->scsi_done(scmd);
2809 }
2810 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2811 ioc->name, count));
2812}
2813
2814/**
Eric Moore3c621b32009-05-18 12:59:41 -06002815 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2816 * @scmd: pointer to scsi command object
2817 * @mpi_request: pointer to the SCSI_IO reqest message frame
2818 *
2819 * Supporting protection 1 and 3.
2820 *
2821 * Returns nothing
2822 */
2823static void
2824_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2825{
2826 u16 eedp_flags;
2827 unsigned char prot_op = scsi_get_prot_op(scmd);
2828 unsigned char prot_type = scsi_get_prot_type(scmd);
2829
2830 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2831 prot_type == SCSI_PROT_DIF_TYPE2 ||
2832 prot_op == SCSI_PROT_NORMAL)
2833 return;
2834
2835 if (prot_op == SCSI_PROT_READ_STRIP)
2836 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2837 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2838 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2839 else
2840 return;
2841
Eric Moore3c621b32009-05-18 12:59:41 -06002842 switch (prot_type) {
2843 case SCSI_PROT_DIF_TYPE1:
2844
2845 /*
2846 * enable ref/guard checking
2847 * auto increment ref tag
2848 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302849 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06002850 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2851 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2852 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2853 cpu_to_be32(scsi_get_lba(scmd));
2854
2855 break;
2856
2857 case SCSI_PROT_DIF_TYPE3:
2858
2859 /*
2860 * enable guard checking
2861 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302862 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06002863 break;
2864 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05302865 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
2866 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06002867}
2868
2869/**
2870 * _scsih_eedp_error_handling - return sense code for EEDP errors
2871 * @scmd: pointer to scsi command object
2872 * @ioc_status: ioc status
2873 *
2874 * Returns nothing
2875 */
2876static void
2877_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2878{
2879 u8 ascq;
2880 u8 sk;
2881 u8 host_byte;
2882
2883 switch (ioc_status) {
2884 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2885 ascq = 0x01;
2886 break;
2887 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2888 ascq = 0x02;
2889 break;
2890 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2891 ascq = 0x03;
2892 break;
2893 default:
2894 ascq = 0x00;
2895 break;
2896 }
2897
2898 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2899 sk = ILLEGAL_REQUEST;
2900 host_byte = DID_ABORT;
2901 } else {
2902 sk = ABORTED_COMMAND;
2903 host_byte = DID_OK;
2904 }
2905
2906 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2907 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2908 SAM_STAT_CHECK_CONDITION;
2909}
2910
2911/**
Eric Moored5d135b2009-05-18 13:02:08 -06002912 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002913 * @scmd: pointer to scsi command object
2914 * @done: function pointer to be invoked on completion
2915 *
2916 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2917 *
2918 * Returns 0 on success. If there's a failure, return either:
2919 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2920 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2921 */
2922static int
Eric Moored5d135b2009-05-18 13:02:08 -06002923_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002924{
2925 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2926 struct MPT2SAS_DEVICE *sas_device_priv_data;
2927 struct MPT2SAS_TARGET *sas_target_priv_data;
2928 Mpi2SCSIIORequest_t *mpi_request;
2929 u32 mpi_control;
2930 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002931
2932 scmd->scsi_done = done;
2933 sas_device_priv_data = scmd->device->hostdata;
2934 if (!sas_device_priv_data) {
2935 scmd->result = DID_NO_CONNECT << 16;
2936 scmd->scsi_done(scmd);
2937 return 0;
2938 }
2939
2940 sas_target_priv_data = sas_device_priv_data->sas_target;
2941 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2942 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2943 scmd->result = DID_NO_CONNECT << 16;
2944 scmd->scsi_done(scmd);
2945 return 0;
2946 }
2947
2948 /* see if we are busy with task managment stuff */
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05302949 if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302950 return SCSI_MLQUEUE_DEVICE_BUSY;
2951 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002952 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06002953
2954 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2955 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2956 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2957 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2958 else
2959 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2960
2961 /* set tags */
2962 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2963 if (scmd->device->tagged_supported) {
2964 if (scmd->device->ordered_tags)
2965 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2966 else
2967 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2968 } else
2969/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2970/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2971 */
2972 mpi_control |= (0x500);
2973
2974 } else
2975 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05302976 /* Make sure Device is not raid volume */
2977 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
2978 sas_is_tlr_enabled(scmd->device))
Eric Moore635374e2009-03-09 01:21:12 -06002979 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2980
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302981 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002982 if (!smid) {
2983 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2984 ioc->name, __func__);
2985 goto out;
2986 }
2987 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2988 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06002989 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06002990 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2991 if (sas_device_priv_data->sas_target->flags &
2992 MPT_TARGET_FLAGS_RAID_COMPONENT)
2993 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2994 else
2995 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2996 mpi_request->DevHandle =
2997 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2998 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2999 mpi_request->Control = cpu_to_le32(mpi_control);
3000 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3001 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3002 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3003 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303004 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003005 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3006 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3007 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303008 mpi_request->VF_ID = 0; /* TODO */
3009 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003010 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3011 mpi_request->LUN);
3012 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3013
3014 if (!mpi_request->DataLength) {
3015 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3016 } else {
3017 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3018 mpt2sas_base_free_smid(ioc, smid);
3019 goto out;
3020 }
3021 }
3022
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303023 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Eric Moore635374e2009-03-09 01:21:12 -06003024 sas_device_priv_data->sas_target->handle);
3025 return 0;
3026
3027 out:
3028 return SCSI_MLQUEUE_HOST_BUSY;
3029}
3030
3031/**
3032 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3033 * @sense_buffer: sense data returned by target
3034 * @data: normalized skey/asc/ascq
3035 *
3036 * Return nothing.
3037 */
3038static void
3039_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3040{
3041 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3042 /* descriptor format */
3043 data->skey = sense_buffer[1] & 0x0F;
3044 data->asc = sense_buffer[2];
3045 data->ascq = sense_buffer[3];
3046 } else {
3047 /* fixed format */
3048 data->skey = sense_buffer[2] & 0x0F;
3049 data->asc = sense_buffer[12];
3050 data->ascq = sense_buffer[13];
3051 }
3052}
3053
3054#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3055/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003056 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003057 * @ioc: per adapter object
3058 * @scmd: pointer to scsi command object
3059 * @mpi_reply: reply mf payload returned from firmware
3060 *
3061 * scsi_status - SCSI Status code returned from target device
3062 * scsi_state - state info associated with SCSI_IO determined by ioc
3063 * ioc_status - ioc supplied status info
3064 *
3065 * Return nothing.
3066 */
3067static void
3068_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3069 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3070{
3071 u32 response_info;
3072 u8 *response_bytes;
3073 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3074 MPI2_IOCSTATUS_MASK;
3075 u8 scsi_state = mpi_reply->SCSIState;
3076 u8 scsi_status = mpi_reply->SCSIStatus;
3077 char *desc_ioc_state = NULL;
3078 char *desc_scsi_status = NULL;
3079 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303080 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3081
3082 if (log_info == 0x31170000)
3083 return;
Eric Moore635374e2009-03-09 01:21:12 -06003084
3085 switch (ioc_status) {
3086 case MPI2_IOCSTATUS_SUCCESS:
3087 desc_ioc_state = "success";
3088 break;
3089 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3090 desc_ioc_state = "invalid function";
3091 break;
3092 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3093 desc_ioc_state = "scsi recovered error";
3094 break;
3095 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3096 desc_ioc_state = "scsi invalid dev handle";
3097 break;
3098 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3099 desc_ioc_state = "scsi device not there";
3100 break;
3101 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3102 desc_ioc_state = "scsi data overrun";
3103 break;
3104 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3105 desc_ioc_state = "scsi data underrun";
3106 break;
3107 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3108 desc_ioc_state = "scsi io data error";
3109 break;
3110 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3111 desc_ioc_state = "scsi protocol error";
3112 break;
3113 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3114 desc_ioc_state = "scsi task terminated";
3115 break;
3116 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3117 desc_ioc_state = "scsi residual mismatch";
3118 break;
3119 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3120 desc_ioc_state = "scsi task mgmt failed";
3121 break;
3122 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3123 desc_ioc_state = "scsi ioc terminated";
3124 break;
3125 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3126 desc_ioc_state = "scsi ext terminated";
3127 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003128 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3129 desc_ioc_state = "eedp guard error";
3130 break;
3131 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3132 desc_ioc_state = "eedp ref tag error";
3133 break;
3134 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3135 desc_ioc_state = "eedp app tag error";
3136 break;
Eric Moore635374e2009-03-09 01:21:12 -06003137 default:
3138 desc_ioc_state = "unknown";
3139 break;
3140 }
3141
3142 switch (scsi_status) {
3143 case MPI2_SCSI_STATUS_GOOD:
3144 desc_scsi_status = "good";
3145 break;
3146 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3147 desc_scsi_status = "check condition";
3148 break;
3149 case MPI2_SCSI_STATUS_CONDITION_MET:
3150 desc_scsi_status = "condition met";
3151 break;
3152 case MPI2_SCSI_STATUS_BUSY:
3153 desc_scsi_status = "busy";
3154 break;
3155 case MPI2_SCSI_STATUS_INTERMEDIATE:
3156 desc_scsi_status = "intermediate";
3157 break;
3158 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3159 desc_scsi_status = "intermediate condmet";
3160 break;
3161 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3162 desc_scsi_status = "reservation conflict";
3163 break;
3164 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3165 desc_scsi_status = "command terminated";
3166 break;
3167 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3168 desc_scsi_status = "task set full";
3169 break;
3170 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3171 desc_scsi_status = "aca active";
3172 break;
3173 case MPI2_SCSI_STATUS_TASK_ABORTED:
3174 desc_scsi_status = "task aborted";
3175 break;
3176 default:
3177 desc_scsi_status = "unknown";
3178 break;
3179 }
3180
3181 desc_scsi_state[0] = '\0';
3182 if (!scsi_state)
3183 desc_scsi_state = " ";
3184 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3185 strcat(desc_scsi_state, "response info ");
3186 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3187 strcat(desc_scsi_state, "state terminated ");
3188 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3189 strcat(desc_scsi_state, "no status ");
3190 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3191 strcat(desc_scsi_state, "autosense failed ");
3192 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3193 strcat(desc_scsi_state, "autosense valid ");
3194
3195 scsi_print_command(scmd);
3196 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3197 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3198 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3199 ioc_status, smid);
3200 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3201 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3202 scsi_get_resid(scmd));
3203 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3204 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3205 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3206 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3207 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3208 scsi_status, desc_scsi_state, scsi_state);
3209
3210 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3211 struct sense_info data;
3212 _scsih_normalize_sense(scmd->sense_buffer, &data);
3213 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3214 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3215 data.asc, data.ascq);
3216 }
3217
3218 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3219 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3220 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303221 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003222 }
3223}
3224#endif
3225
3226/**
3227 * _scsih_smart_predicted_fault - illuminate Fault LED
3228 * @ioc: per adapter object
3229 * @handle: device handle
3230 *
3231 * Return nothing.
3232 */
3233static void
3234_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3235{
3236 Mpi2SepReply_t mpi_reply;
3237 Mpi2SepRequest_t mpi_request;
3238 struct scsi_target *starget;
3239 struct MPT2SAS_TARGET *sas_target_priv_data;
3240 Mpi2EventNotificationReply_t *event_reply;
3241 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3242 struct _sas_device *sas_device;
3243 ssize_t sz;
3244 unsigned long flags;
3245
3246 /* only handle non-raid devices */
3247 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3248 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3249 if (!sas_device) {
3250 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3251 return;
3252 }
3253 starget = sas_device->starget;
3254 sas_target_priv_data = starget->hostdata;
3255
3256 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3257 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3258 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3259 return;
3260 }
3261 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3262 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3263
3264 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3265 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3266 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3267 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3268 mpi_request.SlotStatus =
3269 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3270 mpi_request.DevHandle = cpu_to_le16(handle);
3271 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3272 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3273 &mpi_request)) != 0) {
3274 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3275 ioc->name, __FILE__, __LINE__, __func__);
3276 return;
3277 }
3278
3279 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3280 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3281 "enclosure_processor: ioc_status (0x%04x), "
3282 "loginfo(0x%08x)\n", ioc->name,
3283 le16_to_cpu(mpi_reply.IOCStatus),
3284 le32_to_cpu(mpi_reply.IOCLogInfo)));
3285 return;
3286 }
3287 }
3288
3289 /* insert into event log */
3290 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3291 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3292 event_reply = kzalloc(sz, GFP_KERNEL);
3293 if (!event_reply) {
3294 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3295 ioc->name, __FILE__, __LINE__, __func__);
3296 return;
3297 }
3298
3299 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3300 event_reply->Event =
3301 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3302 event_reply->MsgLength = sz/4;
3303 event_reply->EventDataLength =
3304 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3305 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3306 event_reply->EventData;
3307 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3308 event_data->ASC = 0x5D;
3309 event_data->DevHandle = cpu_to_le16(handle);
3310 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3311 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3312 kfree(event_reply);
3313}
3314
3315/**
Eric Moored5d135b2009-05-18 13:02:08 -06003316 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003317 * @ioc: per adapter object
3318 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303319 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003320 * @reply: reply message frame(lower 32bit addr)
3321 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303322 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003323 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303324 * Return 1 meaning mf should be freed from _base_interrupt
3325 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003326 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303327static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303328_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003329{
3330 Mpi2SCSIIORequest_t *mpi_request;
3331 Mpi2SCSIIOReply_t *mpi_reply;
3332 struct scsi_cmnd *scmd;
3333 u16 ioc_status;
3334 u32 xfer_cnt;
3335 u8 scsi_state;
3336 u8 scsi_status;
3337 u32 log_info;
3338 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303339 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003340
3341 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303342 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003343 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303344 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003345
3346 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3347
3348 if (mpi_reply == NULL) {
3349 scmd->result = DID_OK << 16;
3350 goto out;
3351 }
3352
3353 sas_device_priv_data = scmd->device->hostdata;
3354 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3355 sas_device_priv_data->sas_target->deleted) {
3356 scmd->result = DID_NO_CONNECT << 16;
3357 goto out;
3358 }
3359
3360 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303361 scsi_state = mpi_reply->SCSIState;
3362 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3363 response_code =
3364 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003365 if (!sas_device_priv_data->tlr_snoop_check) {
3366 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303367 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3368 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303369 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3370 sas_disable_tlr(scmd->device);
3371 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3372 }
Eric Moore635374e2009-03-09 01:21:12 -06003373 }
3374
3375 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3376 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3377 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3378 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3379 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3380 else
3381 log_info = 0;
3382 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003383 scsi_status = mpi_reply->SCSIStatus;
3384
3385 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3386 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3387 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3388 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3389 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3390 }
3391
3392 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3393 struct sense_info data;
3394 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3395 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003396 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003397 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003398 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003399 _scsih_normalize_sense(scmd->sense_buffer, &data);
3400 /* failure prediction threshold exceeded */
3401 if (data.asc == 0x5D)
3402 _scsih_smart_predicted_fault(ioc,
3403 le16_to_cpu(mpi_reply->DevHandle));
3404 }
3405
3406 switch (ioc_status) {
3407 case MPI2_IOCSTATUS_BUSY:
3408 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3409 scmd->result = SAM_STAT_BUSY;
3410 break;
3411
3412 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3413 scmd->result = DID_NO_CONNECT << 16;
3414 break;
3415
3416 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3417 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303418 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3419 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003420 }
Eric Moore635374e2009-03-09 01:21:12 -06003421 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3422 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3423 scmd->result = DID_RESET << 16;
3424 break;
3425
3426 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3427 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3428 scmd->result = DID_SOFT_ERROR << 16;
3429 else
3430 scmd->result = (DID_OK << 16) | scsi_status;
3431 break;
3432
3433 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3434 scmd->result = (DID_OK << 16) | scsi_status;
3435
3436 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3437 break;
3438
3439 if (xfer_cnt < scmd->underflow) {
3440 if (scsi_status == SAM_STAT_BUSY)
3441 scmd->result = SAM_STAT_BUSY;
3442 else
3443 scmd->result = DID_SOFT_ERROR << 16;
3444 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3445 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3446 scmd->result = DID_SOFT_ERROR << 16;
3447 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3448 scmd->result = DID_RESET << 16;
3449 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3450 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3451 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3452 scmd->result = (DRIVER_SENSE << 24) |
3453 SAM_STAT_CHECK_CONDITION;
3454 scmd->sense_buffer[0] = 0x70;
3455 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3456 scmd->sense_buffer[12] = 0x20;
3457 scmd->sense_buffer[13] = 0;
3458 }
3459 break;
3460
3461 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3462 scsi_set_resid(scmd, 0);
3463 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3464 case MPI2_IOCSTATUS_SUCCESS:
3465 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303466 if (response_code ==
3467 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3468 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3469 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003470 scmd->result = DID_SOFT_ERROR << 16;
3471 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3472 scmd->result = DID_RESET << 16;
3473 break;
3474
Eric Moore3c621b32009-05-18 12:59:41 -06003475 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3476 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3477 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3478 _scsih_eedp_error_handling(scmd, ioc_status);
3479 break;
Eric Moore635374e2009-03-09 01:21:12 -06003480 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3481 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3482 case MPI2_IOCSTATUS_INVALID_SGL:
3483 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3484 case MPI2_IOCSTATUS_INVALID_FIELD:
3485 case MPI2_IOCSTATUS_INVALID_STATE:
3486 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3487 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3488 default:
3489 scmd->result = DID_SOFT_ERROR << 16;
3490 break;
3491
3492 }
3493
3494#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3495 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3496 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3497#endif
3498
3499 out:
3500 scsi_dma_unmap(scmd);
3501 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303502 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003503}
3504
3505/**
Eric Moore635374e2009-03-09 01:21:12 -06003506 * _scsih_sas_host_refresh - refreshing sas host object contents
3507 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003508 * Context: user
3509 *
3510 * During port enable, fw will send topology events for every device. Its
3511 * possible that the handles may change from the previous setting, so this
3512 * code keeping handles updating if changed.
3513 *
3514 * Return nothing.
3515 */
3516static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303517_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003518{
3519 u16 sz;
3520 u16 ioc_status;
3521 int i;
3522 Mpi2ConfigReply_t mpi_reply;
3523 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303524 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003525
3526 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3527 "updating handles for sas_host(0x%016llx)\n",
3528 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3529
3530 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3531 * sizeof(Mpi2SasIOUnit0PhyData_t));
3532 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3533 if (!sas_iounit_pg0) {
3534 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3535 ioc->name, __FILE__, __LINE__, __func__);
3536 return;
3537 }
Eric Moore635374e2009-03-09 01:21:12 -06003538
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303539 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3540 sas_iounit_pg0, sz)) != 0)
3541 goto out;
3542 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3543 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3544 goto out;
3545 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3546 if (i == 0)
3547 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3548 PhyData[0].ControllerDevHandle);
3549 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3550 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3551 AttachedDevHandle);
3552 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3553 attached_handle, i, sas_iounit_pg0->PhyData[i].
3554 NegotiatedLinkRate >> 4);
3555 }
Eric Moore635374e2009-03-09 01:21:12 -06003556 out:
3557 kfree(sas_iounit_pg0);
3558}
3559
3560/**
3561 * _scsih_sas_host_add - create sas host object
3562 * @ioc: per adapter object
3563 *
3564 * Creating host side data object, stored in ioc->sas_hba
3565 *
3566 * Return nothing.
3567 */
3568static void
3569_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3570{
3571 int i;
3572 Mpi2ConfigReply_t mpi_reply;
3573 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3574 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3575 Mpi2SasPhyPage0_t phy_pg0;
3576 Mpi2SasDevicePage0_t sas_device_pg0;
3577 Mpi2SasEnclosurePage0_t enclosure_pg0;
3578 u16 ioc_status;
3579 u16 sz;
3580 u16 device_missing_delay;
3581
3582 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3583 if (!ioc->sas_hba.num_phys) {
3584 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3585 ioc->name, __FILE__, __LINE__, __func__);
3586 return;
3587 }
3588
3589 /* sas_iounit page 0 */
3590 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3591 sizeof(Mpi2SasIOUnit0PhyData_t));
3592 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3593 if (!sas_iounit_pg0) {
3594 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3595 ioc->name, __FILE__, __LINE__, __func__);
3596 return;
3597 }
3598 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3599 sas_iounit_pg0, sz))) {
3600 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3601 ioc->name, __FILE__, __LINE__, __func__);
3602 goto out;
3603 }
3604 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3605 MPI2_IOCSTATUS_MASK;
3606 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3607 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3608 ioc->name, __FILE__, __LINE__, __func__);
3609 goto out;
3610 }
3611
3612 /* sas_iounit page 1 */
3613 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3614 sizeof(Mpi2SasIOUnit1PhyData_t));
3615 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3616 if (!sas_iounit_pg1) {
3617 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3618 ioc->name, __FILE__, __LINE__, __func__);
3619 goto out;
3620 }
3621 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3622 sas_iounit_pg1, sz))) {
3623 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3624 ioc->name, __FILE__, __LINE__, __func__);
3625 goto out;
3626 }
3627 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3628 MPI2_IOCSTATUS_MASK;
3629 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3630 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3631 ioc->name, __FILE__, __LINE__, __func__);
3632 goto out;
3633 }
3634
3635 ioc->io_missing_delay =
3636 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3637 device_missing_delay =
3638 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3639 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3640 ioc->device_missing_delay = (device_missing_delay &
3641 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3642 else
3643 ioc->device_missing_delay = device_missing_delay &
3644 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3645
3646 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3647 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3648 sizeof(struct _sas_phy), GFP_KERNEL);
3649 if (!ioc->sas_hba.phy) {
3650 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3651 ioc->name, __FILE__, __LINE__, __func__);
3652 goto out;
3653 }
3654 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3655 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3656 i))) {
3657 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3658 ioc->name, __FILE__, __LINE__, __func__);
3659 goto out;
3660 }
3661 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3662 MPI2_IOCSTATUS_MASK;
3663 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3664 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3665 ioc->name, __FILE__, __LINE__, __func__);
3666 goto out;
3667 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303668
3669 if (i == 0)
3670 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3671 PhyData[0].ControllerDevHandle);
3672 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06003673 ioc->sas_hba.phy[i].phy_id = i;
3674 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3675 phy_pg0, ioc->sas_hba.parent_dev);
3676 }
3677 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303678 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06003679 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3680 ioc->name, __FILE__, __LINE__, __func__);
3681 goto out;
3682 }
Eric Moore635374e2009-03-09 01:21:12 -06003683 ioc->sas_hba.enclosure_handle =
3684 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3685 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3686 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3687 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3688 (unsigned long long) ioc->sas_hba.sas_address,
3689 ioc->sas_hba.num_phys) ;
3690
3691 if (ioc->sas_hba.enclosure_handle) {
3692 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3693 &enclosure_pg0,
3694 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3695 ioc->sas_hba.enclosure_handle))) {
3696 ioc->sas_hba.enclosure_logical_id =
3697 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3698 }
3699 }
3700
3701 out:
3702 kfree(sas_iounit_pg1);
3703 kfree(sas_iounit_pg0);
3704}
3705
3706/**
3707 * _scsih_expander_add - creating expander object
3708 * @ioc: per adapter object
3709 * @handle: expander handle
3710 *
3711 * Creating expander object, stored in ioc->sas_expander_list.
3712 *
3713 * Return 0 for success, else error.
3714 */
3715static int
3716_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3717{
3718 struct _sas_node *sas_expander;
3719 Mpi2ConfigReply_t mpi_reply;
3720 Mpi2ExpanderPage0_t expander_pg0;
3721 Mpi2ExpanderPage1_t expander_pg1;
3722 Mpi2SasEnclosurePage0_t enclosure_pg0;
3723 u32 ioc_status;
3724 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303725 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003726 int i;
3727 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303728 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003729 int rc = 0;
3730
3731 if (!handle)
3732 return -1;
3733
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303734 if (ioc->shost_recovery)
3735 return -1;
3736
Eric Moore635374e2009-03-09 01:21:12 -06003737 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3738 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3739 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3740 ioc->name, __FILE__, __LINE__, __func__);
3741 return -1;
3742 }
3743
3744 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3745 MPI2_IOCSTATUS_MASK;
3746 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3747 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3748 ioc->name, __FILE__, __LINE__, __func__);
3749 return -1;
3750 }
3751
3752 /* handle out of order topology events */
3753 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303754 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3755 != 0) {
3756 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3757 ioc->name, __FILE__, __LINE__, __func__);
3758 return -1;
3759 }
3760 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06003761 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303762 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3763 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003764 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3765 if (!sas_expander) {
3766 rc = _scsih_expander_add(ioc, parent_handle);
3767 if (rc != 0)
3768 return rc;
3769 }
3770 }
3771
Eric Moore635374e2009-03-09 01:21:12 -06003772 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303773 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06003774 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3775 sas_address);
3776 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3777
3778 if (sas_expander)
3779 return 0;
3780
3781 sas_expander = kzalloc(sizeof(struct _sas_node),
3782 GFP_KERNEL);
3783 if (!sas_expander) {
3784 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3785 ioc->name, __FILE__, __LINE__, __func__);
3786 return -1;
3787 }
3788
3789 sas_expander->handle = handle;
3790 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303791 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06003792 sas_expander->sas_address = sas_address;
3793
3794 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3795 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303796 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06003797 sas_expander->sas_address, sas_expander->num_phys);
3798
3799 if (!sas_expander->num_phys)
3800 goto out_fail;
3801 sas_expander->phy = kcalloc(sas_expander->num_phys,
3802 sizeof(struct _sas_phy), GFP_KERNEL);
3803 if (!sas_expander->phy) {
3804 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3805 ioc->name, __FILE__, __LINE__, __func__);
3806 rc = -1;
3807 goto out_fail;
3808 }
3809
3810 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3811 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303812 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003813 if (!mpt2sas_port) {
3814 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3815 ioc->name, __FILE__, __LINE__, __func__);
3816 rc = -1;
3817 goto out_fail;
3818 }
3819 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3820
3821 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3822 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3823 &expander_pg1, i, handle))) {
3824 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3825 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303826 rc = -1;
3827 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003828 }
3829 sas_expander->phy[i].handle = handle;
3830 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303831
3832 if ((mpt2sas_transport_add_expander_phy(ioc,
3833 &sas_expander->phy[i], expander_pg1,
3834 sas_expander->parent_dev))) {
3835 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3836 ioc->name, __FILE__, __LINE__, __func__);
3837 rc = -1;
3838 goto out_fail;
3839 }
Eric Moore635374e2009-03-09 01:21:12 -06003840 }
3841
3842 if (sas_expander->enclosure_handle) {
3843 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3844 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3845 sas_expander->enclosure_handle))) {
3846 sas_expander->enclosure_logical_id =
3847 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3848 }
3849 }
3850
3851 _scsih_expander_node_add(ioc, sas_expander);
3852 return 0;
3853
3854 out_fail:
3855
Kashyap, Desai20f58952009-08-07 19:34:26 +05303856 if (mpt2sas_port)
3857 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303858 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003859 kfree(sas_expander);
3860 return rc;
3861}
3862
3863/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05303864 * _scsih_done - scsih callback handler.
3865 * @ioc: per adapter object
3866 * @smid: system request message index
3867 * @msix_index: MSIX table index supplied by the OS
3868 * @reply: reply message frame(lower 32bit addr)
3869 *
3870 * Callback handler when sending internal generated message frames.
3871 * The callback index passed is `ioc->scsih_cb_idx`
3872 *
3873 * Return 1 meaning mf should be freed from _base_interrupt
3874 * 0 means the mf is freed from this function.
3875 */
3876static u8
3877_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3878{
3879 MPI2DefaultReply_t *mpi_reply;
3880
3881 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3882 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
3883 return 1;
3884 if (ioc->scsih_cmds.smid != smid)
3885 return 1;
3886 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
3887 if (mpi_reply) {
3888 memcpy(ioc->scsih_cmds.reply, mpi_reply,
3889 mpi_reply->MsgLength*4);
3890 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
3891 }
3892 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
3893 complete(&ioc->scsih_cmds.done);
3894 return 1;
3895}
3896
3897/**
Eric Moore635374e2009-03-09 01:21:12 -06003898 * _scsih_expander_remove - removing expander object
3899 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303900 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06003901 *
3902 * Return nothing.
3903 */
3904static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303905_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06003906{
3907 struct _sas_node *sas_expander;
3908 unsigned long flags;
3909
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303910 if (ioc->shost_recovery)
3911 return;
3912
Eric Moore635374e2009-03-09 01:21:12 -06003913 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303914 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3915 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003916 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3917 _scsih_expander_node_remove(ioc, sas_expander);
3918}
3919
3920/**
3921 * _scsih_add_device - creating sas device object
3922 * @ioc: per adapter object
3923 * @handle: sas device handle
3924 * @phy_num: phy number end device attached to
3925 * @is_pd: is this hidden raid component
3926 *
3927 * Creating end device object, stored in ioc->sas_device_list.
3928 *
3929 * Returns 0 for success, non-zero for failure.
3930 */
3931static int
3932_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3933{
3934 Mpi2ConfigReply_t mpi_reply;
3935 Mpi2SasDevicePage0_t sas_device_pg0;
3936 Mpi2SasEnclosurePage0_t enclosure_pg0;
3937 struct _sas_device *sas_device;
3938 u32 ioc_status;
3939 __le64 sas_address;
3940 u32 device_info;
3941 unsigned long flags;
3942
3943 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3944 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3945 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3946 ioc->name, __FILE__, __LINE__, __func__);
3947 return -1;
3948 }
3949
3950 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3951 MPI2_IOCSTATUS_MASK;
3952 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3953 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3954 ioc->name, __FILE__, __LINE__, __func__);
3955 return -1;
3956 }
3957
3958 /* check if device is present */
3959 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3960 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3961 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3962 ioc->name, __FILE__, __LINE__, __func__);
3963 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3964 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3965 return -1;
3966 }
3967
3968 /* check if there were any issus with discovery */
3969 if (sas_device_pg0.AccessStatus ==
3970 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3971 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3972 ioc->name, __FILE__, __LINE__, __func__);
3973 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3974 ioc->name, sas_device_pg0.AccessStatus);
3975 return -1;
3976 }
3977
3978 /* check if this is end device */
3979 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3980 if (!(_scsih_is_end_device(device_info))) {
3981 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3982 ioc->name, __FILE__, __LINE__, __func__);
3983 return -1;
3984 }
3985
3986 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3987
3988 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3989 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3990 sas_address);
3991 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3992
3993 if (sas_device) {
3994 _scsih_ublock_io_device(ioc, handle);
3995 return 0;
3996 }
3997
3998 sas_device = kzalloc(sizeof(struct _sas_device),
3999 GFP_KERNEL);
4000 if (!sas_device) {
4001 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4002 ioc->name, __FILE__, __LINE__, __func__);
4003 return -1;
4004 }
4005
4006 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304007 if (_scsih_get_sas_address(ioc, le16_to_cpu
4008 (sas_device_pg0.ParentDevHandle),
4009 &sas_device->sas_address_parent) != 0)
4010 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4011 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004012 sas_device->enclosure_handle =
4013 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4014 sas_device->slot =
4015 le16_to_cpu(sas_device_pg0.Slot);
4016 sas_device->device_info = device_info;
4017 sas_device->sas_address = sas_address;
4018 sas_device->hidden_raid_component = is_pd;
4019
4020 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304021 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4022 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4023 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004024 sas_device->enclosure_logical_id =
4025 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004026
4027 /* get device name */
4028 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4029
4030 if (ioc->wait_for_port_enable_to_complete)
4031 _scsih_sas_device_init_add(ioc, sas_device);
4032 else
4033 _scsih_sas_device_add(ioc, sas_device);
4034
4035 return 0;
4036}
4037
4038/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304039 * _scsih_remove_pd_device - removing sas device pd object
Eric Moore635374e2009-03-09 01:21:12 -06004040 * @ioc: per adapter object
Kashyap, Desai1278b112010-03-09 17:34:13 +05304041 * @sas_device_delete: the sas_device object
Eric Moore635374e2009-03-09 01:21:12 -06004042 *
Kashyap, Desai1278b112010-03-09 17:34:13 +05304043 * For hidden raid components, we do driver-fw handshake from
4044 * hotplug work threads.
Eric Moore635374e2009-03-09 01:21:12 -06004045 * Return nothing.
4046 */
4047static void
Kashyap, Desai1278b112010-03-09 17:34:13 +05304048_scsih_remove_pd_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
4049 sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004050{
Eric Moore635374e2009-03-09 01:21:12 -06004051 Mpi2SasIoUnitControlReply_t mpi_reply;
4052 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desai1278b112010-03-09 17:34:13 +05304053 u16 vol_handle, handle;
Eric Moore635374e2009-03-09 01:21:12 -06004054
Kashyap, Desai1278b112010-03-09 17:34:13 +05304055 handle = sas_device.handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304056 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
4057 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
Kashyap, Desai1278b112010-03-09 17:34:13 +05304058 (unsigned long long) sas_device.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004059
Kashyap, Desai1278b112010-03-09 17:34:13 +05304060 vol_handle = sas_device.volume_handle;
4061 if (!vol_handle)
4062 return;
4063 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
4064 "handle(0x%04x)\n", ioc->name, vol_handle));
4065 mpt2sas_scsih_issue_tm(ioc, vol_handle, 0,
4066 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
4067 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
4068 "done: handle(0x%04x)\n", ioc->name, vol_handle));
4069 if (ioc->shost_recovery)
4070 return;
Eric Moore635374e2009-03-09 01:21:12 -06004071
4072 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
4073 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
4074 "(0x%04x)\n", ioc->name, handle));
4075 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4076 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4077 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05304078 mpi_request.DevHandle = cpu_to_le16(handle);
Eric Moore635374e2009-03-09 01:21:12 -06004079 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
Kashyap, Desai1278b112010-03-09 17:34:13 +05304080 &mpi_request)) != 0)
Eric Moore635374e2009-03-09 01:21:12 -06004081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4082 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004083
4084 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
4085 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
4086 le16_to_cpu(mpi_reply.IOCStatus),
4087 le32_to_cpu(mpi_reply.IOCLogInfo)));
4088
Kashyap, Desai1278b112010-03-09 17:34:13 +05304089 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle(0x%04x),"
4090 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
4091 (unsigned long long) sas_device.sas_address));
4092}
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304093
Kashyap, Desai1278b112010-03-09 17:34:13 +05304094/**
4095 * _scsih_remove_device - removing sas device object
4096 * @ioc: per adapter object
4097 * @sas_device_delete: the sas_device object
4098 *
4099 * Return nothing.
4100 */
4101static void
4102_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4103 struct _sas_device *sas_device)
4104{
4105 struct _sas_device sas_device_backup;
4106 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304107
Kashyap, Desai1278b112010-03-09 17:34:13 +05304108 if (!sas_device)
4109 return;
Eric Moore635374e2009-03-09 01:21:12 -06004110
Kashyap, Desai1278b112010-03-09 17:34:13 +05304111 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004112 _scsih_sas_device_remove(ioc, sas_device);
4113
Kashyap, Desai1278b112010-03-09 17:34:13 +05304114 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4115 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4116 sas_device_backup.handle, (unsigned long long)
4117 sas_device_backup.sas_address));
4118
4119 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4120 sas_target_priv_data = sas_device_backup.starget->hostdata;
4121 sas_target_priv_data->deleted = 1;
4122 }
4123
4124 if (sas_device_backup.hidden_raid_component)
4125 _scsih_remove_pd_device(ioc, sas_device_backup);
4126
4127 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4128
4129 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4130 sas_device_backup.sas_address_parent);
4131
4132 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4133 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4134 (unsigned long long) sas_device_backup.sas_address);
4135
4136 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4137 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4138 sas_device_backup.handle, (unsigned long long)
4139 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004140}
4141
4142#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4143/**
4144 * _scsih_sas_topology_change_event_debug - debug for topology event
4145 * @ioc: per adapter object
4146 * @event_data: event data payload
4147 * Context: user.
4148 */
4149static void
4150_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4151 Mpi2EventDataSasTopologyChangeList_t *event_data)
4152{
4153 int i;
4154 u16 handle;
4155 u16 reason_code;
4156 u8 phy_number;
4157 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304158 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004159
4160 switch (event_data->ExpStatus) {
4161 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4162 status_str = "add";
4163 break;
4164 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4165 status_str = "remove";
4166 break;
4167 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304168 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004169 status_str = "responding";
4170 break;
4171 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4172 status_str = "remove delay";
4173 break;
4174 default:
4175 status_str = "unknown status";
4176 break;
4177 }
4178 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4179 ioc->name, status_str);
4180 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4181 "start_phy(%02d), count(%d)\n",
4182 le16_to_cpu(event_data->ExpanderDevHandle),
4183 le16_to_cpu(event_data->EnclosureHandle),
4184 event_data->StartPhyNum, event_data->NumEntries);
4185 for (i = 0; i < event_data->NumEntries; i++) {
4186 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4187 if (!handle)
4188 continue;
4189 phy_number = event_data->StartPhyNum + i;
4190 reason_code = event_data->PHY[i].PhyStatus &
4191 MPI2_EVENT_SAS_TOPO_RC_MASK;
4192 switch (reason_code) {
4193 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304194 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004195 break;
4196 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304197 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004198 break;
4199 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304200 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004201 break;
4202 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304203 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004204 break;
4205 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304206 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004207 break;
4208 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304209 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004210 break;
4211 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304212 link_rate = event_data->PHY[i].LinkRate >> 4;
4213 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4214 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
4215 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4216 handle, status_str, link_rate, prev_link_rate);
4217
Eric Moore635374e2009-03-09 01:21:12 -06004218 }
4219}
4220#endif
4221
4222/**
4223 * _scsih_sas_topology_change_event - handle topology changes
4224 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304225 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004226 * Context: user.
4227 *
4228 */
4229static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304230_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004231 struct fw_event_work *fw_event)
4232{
4233 int i;
4234 u16 parent_handle, handle;
4235 u16 reason_code;
4236 u8 phy_number;
4237 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304238 struct _sas_device *sas_device;
4239 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004240 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304241 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304242 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004243
4244#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4245 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4246 _scsih_sas_topology_change_event_debug(ioc, event_data);
4247#endif
4248
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304249 if (ioc->shost_recovery)
4250 return;
4251
Eric Moore635374e2009-03-09 01:21:12 -06004252 if (!ioc->sas_hba.num_phys)
4253 _scsih_sas_host_add(ioc);
4254 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304255 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004256
4257 if (fw_event->ignore) {
4258 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4259 "event\n", ioc->name));
4260 return;
4261 }
4262
4263 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4264
4265 /* handle expander add */
4266 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4267 if (_scsih_expander_add(ioc, parent_handle) != 0)
4268 return;
4269
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304270 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4271 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4272 parent_handle);
4273 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4274 if (sas_expander)
4275 sas_address = sas_expander->sas_address;
4276 else if (parent_handle < ioc->sas_hba.num_phys)
4277 sas_address = ioc->sas_hba.sas_address;
4278 else
4279 return;
4280
Eric Moore635374e2009-03-09 01:21:12 -06004281 /* handle siblings events */
4282 for (i = 0; i < event_data->NumEntries; i++) {
4283 if (fw_event->ignore) {
4284 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4285 "expander event\n", ioc->name));
4286 return;
4287 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304288 if (ioc->shost_recovery)
4289 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304290 phy_number = event_data->StartPhyNum + i;
4291 reason_code = event_data->PHY[i].PhyStatus &
4292 MPI2_EVENT_SAS_TOPO_RC_MASK;
4293 if ((event_data->PHY[i].PhyStatus &
4294 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4295 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004296 continue;
4297 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4298 if (!handle)
4299 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304300 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304301 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004302 switch (reason_code) {
4303 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304304
4305 if (link_rate == prev_link_rate)
4306 break;
4307
4308 mpt2sas_transport_update_links(ioc, sas_address,
4309 handle, phy_number, link_rate);
4310
4311 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
4312 _scsih_ublock_io_device(ioc, handle);
4313 break;
Eric Moore635374e2009-03-09 01:21:12 -06004314 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304315
4316 mpt2sas_transport_update_links(ioc, sas_address,
4317 handle, phy_number, link_rate);
4318
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304319 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004320 break;
4321 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304322
4323 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4324 sas_device = _scsih_sas_device_find_by_handle(ioc,
4325 handle);
4326 if (!sas_device) {
4327 spin_unlock_irqrestore(&ioc->sas_device_lock,
4328 flags);
4329 break;
4330 }
4331 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4332 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004333 break;
4334 }
4335 }
4336
4337 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304338 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4339 sas_expander)
4340 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004341
4342}
4343
4344#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4345/**
4346 * _scsih_sas_device_status_change_event_debug - debug for device event
4347 * @event_data: event data payload
4348 * Context: user.
4349 *
4350 * Return nothing.
4351 */
4352static void
4353_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4354 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4355{
4356 char *reason_str = NULL;
4357
4358 switch (event_data->ReasonCode) {
4359 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4360 reason_str = "smart data";
4361 break;
4362 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4363 reason_str = "unsupported device discovered";
4364 break;
4365 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4366 reason_str = "internal device reset";
4367 break;
4368 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4369 reason_str = "internal task abort";
4370 break;
4371 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4372 reason_str = "internal task abort set";
4373 break;
4374 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4375 reason_str = "internal clear task set";
4376 break;
4377 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4378 reason_str = "internal query task";
4379 break;
4380 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4381 reason_str = "sata init failure";
4382 break;
4383 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4384 reason_str = "internal device reset complete";
4385 break;
4386 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4387 reason_str = "internal task abort complete";
4388 break;
4389 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4390 reason_str = "internal async notification";
4391 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304392 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4393 reason_str = "expander reduced functionality";
4394 break;
4395 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4396 reason_str = "expander reduced functionality complete";
4397 break;
Eric Moore635374e2009-03-09 01:21:12 -06004398 default:
4399 reason_str = "unknown reason";
4400 break;
4401 }
4402 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4403 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4404 reason_str, le16_to_cpu(event_data->DevHandle),
4405 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4406 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4407 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4408 event_data->ASC, event_data->ASCQ);
4409 printk(KERN_INFO "\n");
4410}
4411#endif
4412
4413/**
4414 * _scsih_sas_device_status_change_event - handle device status change
4415 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304416 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004417 * Context: user.
4418 *
4419 * Return nothing.
4420 */
4421static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304422_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4423 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004424{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304425 struct MPT2SAS_TARGET *target_priv_data;
4426 struct _sas_device *sas_device;
4427 __le64 sas_address;
4428 unsigned long flags;
4429 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4430 fw_event->event_data;
4431
Eric Moore635374e2009-03-09 01:21:12 -06004432#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4433 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304434 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304435 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004436#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304437
4438 if (!(event_data->ReasonCode ==
4439 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
4440 event_data->ReasonCode ==
4441 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET))
4442 return;
4443
4444 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4445 sas_address = le64_to_cpu(event_data->SASAddress);
4446 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4447 sas_address);
4448 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4449
4450 if (!sas_device || !sas_device->starget)
4451 return;
4452
4453 target_priv_data = sas_device->starget->hostdata;
4454 if (!target_priv_data)
4455 return;
4456
4457 if (event_data->ReasonCode ==
4458 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4459 target_priv_data->tm_busy = 1;
4460 else
4461 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004462}
4463
4464#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4465/**
4466 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4467 * @ioc: per adapter object
4468 * @event_data: event data payload
4469 * Context: user.
4470 *
4471 * Return nothing.
4472 */
4473static void
4474_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4475 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4476{
4477 char *reason_str = NULL;
4478
4479 switch (event_data->ReasonCode) {
4480 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4481 reason_str = "enclosure add";
4482 break;
4483 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4484 reason_str = "enclosure remove";
4485 break;
4486 default:
4487 reason_str = "unknown reason";
4488 break;
4489 }
4490
4491 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4492 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4493 " number slots(%d)\n", ioc->name, reason_str,
4494 le16_to_cpu(event_data->EnclosureHandle),
4495 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4496 le16_to_cpu(event_data->StartSlot));
4497}
4498#endif
4499
4500/**
4501 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4502 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304503 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004504 * Context: user.
4505 *
4506 * Return nothing.
4507 */
4508static void
4509_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304510 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004511{
4512#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4513 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4514 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304515 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004516#endif
4517}
4518
4519/**
4520 * _scsih_sas_broadcast_primative_event - handle broadcast events
4521 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304522 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004523 * Context: user.
4524 *
4525 * Return nothing.
4526 */
4527static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304528_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4529 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004530{
4531 struct scsi_cmnd *scmd;
4532 u16 smid, handle;
4533 u32 lun;
4534 struct MPT2SAS_DEVICE *sas_device_priv_data;
4535 u32 termination_count;
4536 u32 query_count;
4537 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304538#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4539 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4540#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05304541 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004542 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4543 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4544 event_data->PortWidth));
Eric Moore635374e2009-03-09 01:21:12 -06004545 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4546 __func__));
4547
4548 mutex_lock(&ioc->tm_cmds.mutex);
4549 termination_count = 0;
4550 query_count = 0;
4551 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304552 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004553 scmd = _scsih_scsi_lookup_get(ioc, smid);
4554 if (!scmd)
4555 continue;
4556 sas_device_priv_data = scmd->device->hostdata;
4557 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4558 continue;
4559 /* skip hidden raid components */
4560 if (sas_device_priv_data->sas_target->flags &
4561 MPT_TARGET_FLAGS_RAID_COMPONENT)
4562 continue;
4563 /* skip volumes */
4564 if (sas_device_priv_data->sas_target->flags &
4565 MPT_TARGET_FLAGS_VOLUME)
4566 continue;
4567
4568 handle = sas_device_priv_data->sas_target->handle;
4569 lun = sas_device_priv_data->lun;
4570 query_count++;
4571
4572 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4573 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004574 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05304575 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
4576 & MPI2_IOCSTATUS_MASK;
4577 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06004578 (mpi_reply->ResponseCode ==
4579 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4580 mpi_reply->ResponseCode ==
4581 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4582 continue;
4583
4584 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004585 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4586 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004587 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4588 }
Eric Moore635374e2009-03-09 01:21:12 -06004589 ioc->broadcast_aen_busy = 0;
4590 mutex_unlock(&ioc->tm_cmds.mutex);
4591
4592 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4593 "%s - exit, query_count = %d termination_count = %d\n",
4594 ioc->name, __func__, query_count, termination_count));
4595}
4596
4597/**
4598 * _scsih_sas_discovery_event - handle discovery events
4599 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304600 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004601 * Context: user.
4602 *
4603 * Return nothing.
4604 */
4605static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304606_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4607 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004608{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304609 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4610
Eric Moore635374e2009-03-09 01:21:12 -06004611#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4612 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4613 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4614 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4615 "start" : "stop");
4616 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304617 printk("discovery_status(0x%08x)",
4618 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06004619 printk("\n");
4620 }
4621#endif
4622
4623 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4624 !ioc->sas_hba.num_phys)
4625 _scsih_sas_host_add(ioc);
4626}
4627
4628/**
4629 * _scsih_reprobe_lun - reprobing lun
4630 * @sdev: scsi device struct
4631 * @no_uld_attach: sdev->no_uld_attach flag setting
4632 *
4633 **/
4634static void
4635_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4636{
4637 int rc;
4638
4639 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4640 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4641 sdev->no_uld_attach ? "hidding" : "exposing");
4642 rc = scsi_device_reprobe(sdev);
4643}
4644
4645/**
4646 * _scsih_reprobe_target - reprobing target
4647 * @starget: scsi target struct
4648 * @no_uld_attach: sdev->no_uld_attach flag setting
4649 *
4650 * Note: no_uld_attach flag determines whether the disk device is attached
4651 * to block layer. A value of `1` means to not attach.
4652 **/
4653static void
4654_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4655{
4656 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4657
4658 if (no_uld_attach)
4659 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4660 else
4661 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4662
4663 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4664 _scsih_reprobe_lun);
4665}
4666/**
4667 * _scsih_sas_volume_add - add new volume
4668 * @ioc: per adapter object
4669 * @element: IR config element data
4670 * Context: user.
4671 *
4672 * Return nothing.
4673 */
4674static void
4675_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4676 Mpi2EventIrConfigElement_t *element)
4677{
4678 struct _raid_device *raid_device;
4679 unsigned long flags;
4680 u64 wwid;
4681 u16 handle = le16_to_cpu(element->VolDevHandle);
4682 int rc;
4683
Eric Moore635374e2009-03-09 01:21:12 -06004684 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4685 if (!wwid) {
4686 printk(MPT2SAS_ERR_FMT
4687 "failure at %s:%d/%s()!\n", ioc->name,
4688 __FILE__, __LINE__, __func__);
4689 return;
4690 }
4691
4692 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4693 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4694 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4695
4696 if (raid_device)
4697 return;
4698
4699 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4700 if (!raid_device) {
4701 printk(MPT2SAS_ERR_FMT
4702 "failure at %s:%d/%s()!\n", ioc->name,
4703 __FILE__, __LINE__, __func__);
4704 return;
4705 }
4706
4707 raid_device->id = ioc->sas_id++;
4708 raid_device->channel = RAID_CHANNEL;
4709 raid_device->handle = handle;
4710 raid_device->wwid = wwid;
4711 _scsih_raid_device_add(ioc, raid_device);
4712 if (!ioc->wait_for_port_enable_to_complete) {
4713 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4714 raid_device->id, 0);
4715 if (rc)
4716 _scsih_raid_device_remove(ioc, raid_device);
4717 } else
4718 _scsih_determine_boot_device(ioc, raid_device, 1);
4719}
4720
4721/**
4722 * _scsih_sas_volume_delete - delete volume
4723 * @ioc: per adapter object
4724 * @element: IR config element data
4725 * Context: user.
4726 *
4727 * Return nothing.
4728 */
4729static void
4730_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4731 Mpi2EventIrConfigElement_t *element)
4732{
4733 struct _raid_device *raid_device;
4734 u16 handle = le16_to_cpu(element->VolDevHandle);
4735 unsigned long flags;
4736 struct MPT2SAS_TARGET *sas_target_priv_data;
4737
Eric Moore635374e2009-03-09 01:21:12 -06004738 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4739 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4740 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4741 if (!raid_device)
4742 return;
4743 if (raid_device->starget) {
4744 sas_target_priv_data = raid_device->starget->hostdata;
4745 sas_target_priv_data->deleted = 1;
4746 scsi_remove_target(&raid_device->starget->dev);
4747 }
4748 _scsih_raid_device_remove(ioc, raid_device);
4749}
4750
4751/**
4752 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4753 * @ioc: per adapter object
4754 * @element: IR config element data
4755 * Context: user.
4756 *
4757 * Return nothing.
4758 */
4759static void
4760_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4761 Mpi2EventIrConfigElement_t *element)
4762{
4763 struct _sas_device *sas_device;
4764 unsigned long flags;
4765 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4766
4767 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4768 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4769 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4770 if (!sas_device)
4771 return;
4772
4773 /* exposing raid component */
4774 sas_device->volume_handle = 0;
4775 sas_device->volume_wwid = 0;
4776 sas_device->hidden_raid_component = 0;
4777 _scsih_reprobe_target(sas_device->starget, 0);
4778}
4779
4780/**
4781 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4782 * @ioc: per adapter object
4783 * @element: IR config element data
4784 * Context: user.
4785 *
4786 * Return nothing.
4787 */
4788static void
4789_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4790 Mpi2EventIrConfigElement_t *element)
4791{
4792 struct _sas_device *sas_device;
4793 unsigned long flags;
4794 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4795
4796 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4797 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4798 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4799 if (!sas_device)
4800 return;
4801
4802 /* hiding raid component */
4803 mpt2sas_config_get_volume_handle(ioc, handle,
4804 &sas_device->volume_handle);
4805 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4806 &sas_device->volume_wwid);
4807 sas_device->hidden_raid_component = 1;
4808 _scsih_reprobe_target(sas_device->starget, 1);
4809}
4810
4811/**
4812 * _scsih_sas_pd_delete - delete pd component
4813 * @ioc: per adapter object
4814 * @element: IR config element data
4815 * Context: user.
4816 *
4817 * Return nothing.
4818 */
4819static void
4820_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4821 Mpi2EventIrConfigElement_t *element)
4822{
4823 struct _sas_device *sas_device;
4824 unsigned long flags;
4825 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4826
4827 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4828 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4829 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4830 if (!sas_device)
4831 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304832 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004833}
4834
4835/**
4836 * _scsih_sas_pd_add - remove pd component
4837 * @ioc: per adapter object
4838 * @element: IR config element data
4839 * Context: user.
4840 *
4841 * Return nothing.
4842 */
4843static void
4844_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4845 Mpi2EventIrConfigElement_t *element)
4846{
4847 struct _sas_device *sas_device;
4848 unsigned long flags;
4849 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304850 Mpi2ConfigReply_t mpi_reply;
4851 Mpi2SasDevicePage0_t sas_device_pg0;
4852 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304853 u64 sas_address;
4854 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06004855
4856 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4857 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4858 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304859 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004860 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304861 return;
4862 }
4863
4864 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4865 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4866 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4867 ioc->name, __FILE__, __LINE__, __func__);
4868 return;
4869 }
4870
4871 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4872 MPI2_IOCSTATUS_MASK;
4873 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4874 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4875 ioc->name, __FILE__, __LINE__, __func__);
4876 return;
4877 }
4878
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304879 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4880 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4881 mpt2sas_transport_update_links(ioc, sas_address, handle,
4882 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304883
4884 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004885}
4886
4887#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4888/**
4889 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4890 * @ioc: per adapter object
4891 * @event_data: event data payload
4892 * Context: user.
4893 *
4894 * Return nothing.
4895 */
4896static void
4897_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4898 Mpi2EventDataIrConfigChangeList_t *event_data)
4899{
4900 Mpi2EventIrConfigElement_t *element;
4901 u8 element_type;
4902 int i;
4903 char *reason_str = NULL, *element_str = NULL;
4904
4905 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4906
4907 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4908 ioc->name, (le32_to_cpu(event_data->Flags) &
4909 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4910 "foreign" : "native", event_data->NumElements);
4911 for (i = 0; i < event_data->NumElements; i++, element++) {
4912 switch (element->ReasonCode) {
4913 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4914 reason_str = "add";
4915 break;
4916 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4917 reason_str = "remove";
4918 break;
4919 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4920 reason_str = "no change";
4921 break;
4922 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4923 reason_str = "hide";
4924 break;
4925 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4926 reason_str = "unhide";
4927 break;
4928 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4929 reason_str = "volume_created";
4930 break;
4931 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4932 reason_str = "volume_deleted";
4933 break;
4934 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4935 reason_str = "pd_created";
4936 break;
4937 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4938 reason_str = "pd_deleted";
4939 break;
4940 default:
4941 reason_str = "unknown reason";
4942 break;
4943 }
4944 element_type = le16_to_cpu(element->ElementFlags) &
4945 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4946 switch (element_type) {
4947 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4948 element_str = "volume";
4949 break;
4950 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4951 element_str = "phys disk";
4952 break;
4953 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4954 element_str = "hot spare";
4955 break;
4956 default:
4957 element_str = "unknown element";
4958 break;
4959 }
4960 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4961 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4962 reason_str, le16_to_cpu(element->VolDevHandle),
4963 le16_to_cpu(element->PhysDiskDevHandle),
4964 element->PhysDiskNum);
4965 }
4966}
4967#endif
4968
4969/**
4970 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4971 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304972 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004973 * Context: user.
4974 *
4975 * Return nothing.
4976 */
4977static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304978_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4979 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004980{
4981 Mpi2EventIrConfigElement_t *element;
4982 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304983 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304984 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004985
4986#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4987 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4988 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4989
4990#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05304991 foreign_config = (le32_to_cpu(event_data->Flags) &
4992 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06004993
4994 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4995 for (i = 0; i < event_data->NumElements; i++, element++) {
4996
4997 switch (element->ReasonCode) {
4998 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4999 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305000 if (!foreign_config)
5001 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005002 break;
5003 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5004 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305005 if (!foreign_config)
5006 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005007 break;
5008 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5009 _scsih_sas_pd_hide(ioc, element);
5010 break;
5011 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5012 _scsih_sas_pd_expose(ioc, element);
5013 break;
5014 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5015 _scsih_sas_pd_add(ioc, element);
5016 break;
5017 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5018 _scsih_sas_pd_delete(ioc, element);
5019 break;
5020 }
5021 }
5022}
5023
5024/**
5025 * _scsih_sas_ir_volume_event - IR volume event
5026 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305027 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005028 * Context: user.
5029 *
5030 * Return nothing.
5031 */
5032static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305033_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5034 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005035{
5036 u64 wwid;
5037 unsigned long flags;
5038 struct _raid_device *raid_device;
5039 u16 handle;
5040 u32 state;
5041 int rc;
5042 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305043 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005044
5045 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5046 return;
5047
5048 handle = le16_to_cpu(event_data->VolDevHandle);
5049 state = le32_to_cpu(event_data->NewValue);
5050 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5051 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5052 le32_to_cpu(event_data->PreviousValue), state));
5053
5054 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5055 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5056 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5057
5058 switch (state) {
5059 case MPI2_RAID_VOL_STATE_MISSING:
5060 case MPI2_RAID_VOL_STATE_FAILED:
5061 if (!raid_device)
5062 break;
5063 if (raid_device->starget) {
5064 sas_target_priv_data = raid_device->starget->hostdata;
5065 sas_target_priv_data->deleted = 1;
5066 scsi_remove_target(&raid_device->starget->dev);
5067 }
5068 _scsih_raid_device_remove(ioc, raid_device);
5069 break;
5070
5071 case MPI2_RAID_VOL_STATE_ONLINE:
5072 case MPI2_RAID_VOL_STATE_DEGRADED:
5073 case MPI2_RAID_VOL_STATE_OPTIMAL:
5074 if (raid_device)
5075 break;
5076
5077 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5078 if (!wwid) {
5079 printk(MPT2SAS_ERR_FMT
5080 "failure at %s:%d/%s()!\n", ioc->name,
5081 __FILE__, __LINE__, __func__);
5082 break;
5083 }
5084
5085 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5086 if (!raid_device) {
5087 printk(MPT2SAS_ERR_FMT
5088 "failure at %s:%d/%s()!\n", ioc->name,
5089 __FILE__, __LINE__, __func__);
5090 break;
5091 }
5092
5093 raid_device->id = ioc->sas_id++;
5094 raid_device->channel = RAID_CHANNEL;
5095 raid_device->handle = handle;
5096 raid_device->wwid = wwid;
5097 _scsih_raid_device_add(ioc, raid_device);
5098 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5099 raid_device->id, 0);
5100 if (rc)
5101 _scsih_raid_device_remove(ioc, raid_device);
5102 break;
5103
5104 case MPI2_RAID_VOL_STATE_INITIALIZING:
5105 default:
5106 break;
5107 }
5108}
5109
5110/**
5111 * _scsih_sas_ir_physical_disk_event - PD event
5112 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305113 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005114 * Context: user.
5115 *
5116 * Return nothing.
5117 */
5118static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305119_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5120 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005121{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305122 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005123 u32 state;
5124 struct _sas_device *sas_device;
5125 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305126 Mpi2ConfigReply_t mpi_reply;
5127 Mpi2SasDevicePage0_t sas_device_pg0;
5128 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305129 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305130 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005131
5132 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5133 return;
5134
5135 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5136 state = le32_to_cpu(event_data->NewValue);
5137
5138 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5139 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5140 le32_to_cpu(event_data->PreviousValue), state));
5141
5142 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5143 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5144 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5145
5146 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005147 case MPI2_RAID_PD_STATE_ONLINE:
5148 case MPI2_RAID_PD_STATE_DEGRADED:
5149 case MPI2_RAID_PD_STATE_REBUILDING:
5150 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305151 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06005152 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305153 return;
5154 }
5155
5156 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5157 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5158 handle))) {
5159 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5160 ioc->name, __FILE__, __LINE__, __func__);
5161 return;
5162 }
5163
5164 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5165 MPI2_IOCSTATUS_MASK;
5166 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5167 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5168 ioc->name, __FILE__, __LINE__, __func__);
5169 return;
5170 }
5171
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305172 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5173 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5174 mpt2sas_transport_update_links(ioc, sas_address, handle,
5175 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305176
5177 _scsih_add_device(ioc, handle, 0, 1);
5178
Eric Moore635374e2009-03-09 01:21:12 -06005179 break;
5180
Kashyap, Desai62727a72009-08-07 19:35:18 +05305181 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005182 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5183 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5184 case MPI2_RAID_PD_STATE_HOT_SPARE:
5185 default:
5186 break;
5187 }
5188}
5189
5190#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5191/**
5192 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5193 * @ioc: per adapter object
5194 * @event_data: event data payload
5195 * Context: user.
5196 *
5197 * Return nothing.
5198 */
5199static void
5200_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5201 Mpi2EventDataIrOperationStatus_t *event_data)
5202{
5203 char *reason_str = NULL;
5204
5205 switch (event_data->RAIDOperation) {
5206 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5207 reason_str = "resync";
5208 break;
5209 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5210 reason_str = "online capacity expansion";
5211 break;
5212 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5213 reason_str = "consistency check";
5214 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305215 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5216 reason_str = "background init";
5217 break;
5218 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5219 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005220 break;
5221 }
5222
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305223 if (!reason_str)
5224 return;
5225
Eric Moore635374e2009-03-09 01:21:12 -06005226 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5227 "\thandle(0x%04x), percent complete(%d)\n",
5228 ioc->name, reason_str,
5229 le16_to_cpu(event_data->VolDevHandle),
5230 event_data->PercentComplete);
5231}
5232#endif
5233
5234/**
5235 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5236 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305237 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005238 * Context: user.
5239 *
5240 * Return nothing.
5241 */
5242static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305243_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5244 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005245{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305246 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5247 static struct _raid_device *raid_device;
5248 unsigned long flags;
5249 u16 handle;
5250
Eric Moore635374e2009-03-09 01:21:12 -06005251#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5252 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305253 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305254 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005255#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305256
5257 /* code added for raid transport support */
5258 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5259
5260 handle = le16_to_cpu(event_data->VolDevHandle);
5261
5262 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5263 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5264 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5265
5266 if (!raid_device)
5267 return;
5268
5269 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5270 raid_device->percent_complete =
5271 event_data->PercentComplete;
5272 }
Eric Moore635374e2009-03-09 01:21:12 -06005273}
5274
5275/**
5276 * _scsih_task_set_full - handle task set full
5277 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305278 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005279 * Context: user.
5280 *
5281 * Throttle back qdepth.
5282 */
5283static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305284_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5285 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005286{
5287 unsigned long flags;
5288 struct _sas_device *sas_device;
5289 static struct _raid_device *raid_device;
5290 struct scsi_device *sdev;
5291 int depth;
5292 u16 current_depth;
5293 u16 handle;
5294 int id, channel;
5295 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305296 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005297
5298 current_depth = le16_to_cpu(event_data->CurrentDepth);
5299 handle = le16_to_cpu(event_data->DevHandle);
5300 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5301 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5302 if (!sas_device) {
5303 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5304 return;
5305 }
5306 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5307 id = sas_device->id;
5308 channel = sas_device->channel;
5309 sas_address = sas_device->sas_address;
5310
5311 /* if hidden raid component, then change to volume characteristics */
5312 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5313 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5314 raid_device = _scsih_raid_device_find_by_handle(
5315 ioc, sas_device->volume_handle);
5316 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5317 if (raid_device) {
5318 id = raid_device->id;
5319 channel = raid_device->channel;
5320 handle = raid_device->handle;
5321 sas_address = raid_device->wwid;
5322 }
5323 }
5324
5325 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5326 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5327 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5328 handle, (unsigned long long)sas_address, current_depth);
5329
5330 shost_for_each_device(sdev, ioc->shost) {
5331 if (sdev->id == id && sdev->channel == channel) {
5332 if (current_depth > sdev->queue_depth) {
5333 if (ioc->logging_level &
5334 MPT_DEBUG_TASK_SET_FULL)
5335 sdev_printk(KERN_INFO, sdev, "strange "
5336 "observation, the queue depth is"
5337 " (%d) meanwhile fw queue depth "
5338 "is (%d)\n", sdev->queue_depth,
5339 current_depth);
5340 continue;
5341 }
5342 depth = scsi_track_queue_full(sdev,
5343 current_depth - 1);
5344 if (depth > 0)
5345 sdev_printk(KERN_INFO, sdev, "Queue depth "
5346 "reduced to (%d)\n", depth);
5347 else if (depth < 0)
5348 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5349 "Queueing is being disabled\n");
5350 else if (depth == 0)
5351 if (ioc->logging_level &
5352 MPT_DEBUG_TASK_SET_FULL)
5353 sdev_printk(KERN_INFO, sdev,
5354 "Queue depth not changed yet\n");
5355 }
5356 }
5357}
5358
5359/**
5360 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5361 * @ioc: per adapter object
5362 * @sas_address: sas address
5363 * @slot: enclosure slot id
5364 * @handle: device handle
5365 *
5366 * After host reset, find out whether devices are still responding.
5367 * Used in _scsi_remove_unresponsive_sas_devices.
5368 *
5369 * Return nothing.
5370 */
5371static void
5372_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5373 u16 slot, u16 handle)
5374{
5375 struct MPT2SAS_TARGET *sas_target_priv_data;
5376 struct scsi_target *starget;
5377 struct _sas_device *sas_device;
5378 unsigned long flags;
5379
5380 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5381 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5382 if (sas_device->sas_address == sas_address &&
5383 sas_device->slot == slot && sas_device->starget) {
5384 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305385 starget = sas_device->starget;
5386 sas_target_priv_data = starget->hostdata;
5387 sas_target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005388 starget_printk(KERN_INFO, sas_device->starget,
5389 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5390 "logical id(0x%016llx), slot(%d)\n", handle,
5391 (unsigned long long)sas_device->sas_address,
5392 (unsigned long long)
5393 sas_device->enclosure_logical_id,
5394 sas_device->slot);
5395 if (sas_device->handle == handle)
5396 goto out;
5397 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5398 sas_device->handle);
5399 sas_device->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005400 sas_target_priv_data->handle = handle;
5401 goto out;
5402 }
5403 }
5404 out:
5405 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5406}
5407
5408/**
5409 * _scsih_search_responding_sas_devices -
5410 * @ioc: per adapter object
5411 *
5412 * After host reset, find out whether devices are still responding.
5413 * If not remove.
5414 *
5415 * Return nothing.
5416 */
5417static void
5418_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5419{
5420 Mpi2SasDevicePage0_t sas_device_pg0;
5421 Mpi2ConfigReply_t mpi_reply;
5422 u16 ioc_status;
5423 __le64 sas_address;
5424 u16 handle;
5425 u32 device_info;
5426 u16 slot;
5427
5428 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5429
5430 if (list_empty(&ioc->sas_device_list))
5431 return;
5432
5433 handle = 0xFFFF;
5434 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5435 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5436 handle))) {
5437 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5438 MPI2_IOCSTATUS_MASK;
5439 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5440 break;
5441 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5442 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5443 if (!(_scsih_is_end_device(device_info)))
5444 continue;
5445 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5446 slot = le16_to_cpu(sas_device_pg0.Slot);
5447 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5448 handle);
5449 }
5450}
5451
5452/**
5453 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5454 * @ioc: per adapter object
5455 * @wwid: world wide identifier for raid volume
5456 * @handle: device handle
5457 *
5458 * After host reset, find out whether devices are still responding.
5459 * Used in _scsi_remove_unresponsive_raid_devices.
5460 *
5461 * Return nothing.
5462 */
5463static void
5464_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5465 u16 handle)
5466{
5467 struct MPT2SAS_TARGET *sas_target_priv_data;
5468 struct scsi_target *starget;
5469 struct _raid_device *raid_device;
5470 unsigned long flags;
5471
5472 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5473 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5474 if (raid_device->wwid == wwid && raid_device->starget) {
5475 raid_device->responding = 1;
5476 starget_printk(KERN_INFO, raid_device->starget,
5477 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5478 (unsigned long long)raid_device->wwid);
5479 if (raid_device->handle == handle)
5480 goto out;
5481 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5482 raid_device->handle);
5483 raid_device->handle = handle;
5484 starget = raid_device->starget;
5485 sas_target_priv_data = starget->hostdata;
5486 sas_target_priv_data->handle = handle;
5487 goto out;
5488 }
5489 }
5490 out:
5491 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5492}
5493
5494/**
5495 * _scsih_search_responding_raid_devices -
5496 * @ioc: per adapter object
5497 *
5498 * After host reset, find out whether devices are still responding.
5499 * If not remove.
5500 *
5501 * Return nothing.
5502 */
5503static void
5504_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5505{
5506 Mpi2RaidVolPage1_t volume_pg1;
5507 Mpi2ConfigReply_t mpi_reply;
5508 u16 ioc_status;
5509 u16 handle;
5510
5511 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5512
5513 if (list_empty(&ioc->raid_device_list))
5514 return;
5515
5516 handle = 0xFFFF;
5517 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5518 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5519 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5520 MPI2_IOCSTATUS_MASK;
5521 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5522 break;
5523 handle = le16_to_cpu(volume_pg1.DevHandle);
5524 _scsih_mark_responding_raid_device(ioc,
5525 le64_to_cpu(volume_pg1.WWID), handle);
5526 }
5527}
5528
5529/**
5530 * _scsih_mark_responding_expander - mark a expander as responding
5531 * @ioc: per adapter object
5532 * @sas_address: sas address
5533 * @handle:
5534 *
5535 * After host reset, find out whether devices are still responding.
5536 * Used in _scsi_remove_unresponsive_expanders.
5537 *
5538 * Return nothing.
5539 */
5540static void
5541_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5542 u16 handle)
5543{
5544 struct _sas_node *sas_expander;
5545 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305546 int i;
Eric Moore635374e2009-03-09 01:21:12 -06005547
5548 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5549 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305550 if (sas_expander->sas_address != sas_address)
5551 continue;
5552 sas_expander->responding = 1;
5553 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06005554 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305555 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5556 " from(0x%04x) to (0x%04x)!!!\n",
5557 (unsigned long long)sas_expander->sas_address,
5558 sas_expander->handle, handle);
5559 sas_expander->handle = handle;
5560 for (i = 0 ; i < sas_expander->num_phys ; i++)
5561 sas_expander->phy[i].handle = handle;
5562 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005563 }
5564 out:
5565 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5566}
5567
5568/**
5569 * _scsih_search_responding_expanders -
5570 * @ioc: per adapter object
5571 *
5572 * After host reset, find out whether devices are still responding.
5573 * If not remove.
5574 *
5575 * Return nothing.
5576 */
5577static void
5578_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5579{
5580 Mpi2ExpanderPage0_t expander_pg0;
5581 Mpi2ConfigReply_t mpi_reply;
5582 u16 ioc_status;
5583 __le64 sas_address;
5584 u16 handle;
5585
5586 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5587
5588 if (list_empty(&ioc->sas_expander_list))
5589 return;
5590
5591 handle = 0xFFFF;
5592 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5593 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5594
5595 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5596 MPI2_IOCSTATUS_MASK;
5597 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5598 break;
5599
5600 handle = le16_to_cpu(expander_pg0.DevHandle);
5601 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5602 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5603 "sas_addr(0x%016llx)\n", handle,
5604 (unsigned long long)sas_address);
5605 _scsih_mark_responding_expander(ioc, sas_address, handle);
5606 }
5607
5608}
5609
5610/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305611 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06005612 * @ioc: per adapter object
5613 *
5614 * Return nothing.
5615 */
5616static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305617_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06005618{
5619 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305620 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005621 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005622
Eric Moore635374e2009-03-09 01:21:12 -06005623
5624 list_for_each_entry_safe(sas_device, sas_device_next,
5625 &ioc->sas_device_list, list) {
5626 if (sas_device->responding) {
5627 sas_device->responding = 0;
5628 continue;
5629 }
5630 if (sas_device->starget)
5631 starget_printk(KERN_INFO, sas_device->starget,
5632 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5633 "enclosure logical id(0x%016llx), slot(%d)\n",
5634 sas_device->handle,
5635 (unsigned long long)sas_device->sas_address,
5636 (unsigned long long)
5637 sas_device->enclosure_logical_id,
5638 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305639 /* invalidate the device handle */
5640 sas_device->handle = 0;
5641 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005642 }
5643
5644 list_for_each_entry_safe(raid_device, raid_device_next,
5645 &ioc->raid_device_list, list) {
5646 if (raid_device->responding) {
5647 raid_device->responding = 0;
5648 continue;
5649 }
5650 if (raid_device->starget) {
5651 starget_printk(KERN_INFO, raid_device->starget,
5652 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5653 raid_device->handle,
5654 (unsigned long long)raid_device->wwid);
5655 scsi_remove_target(&raid_device->starget->dev);
5656 }
5657 _scsih_raid_device_remove(ioc, raid_device);
5658 }
5659
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305660 retry_expander_search:
5661 sas_expander = NULL;
5662 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005663 if (sas_expander->responding) {
5664 sas_expander->responding = 0;
5665 continue;
5666 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305667 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305668 goto retry_expander_search;
5669 }
5670}
5671
5672/**
5673 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5674 * @ioc: per adapter object
5675 * @reset_phase: phase
5676 *
5677 * The handler for doing any required cleanup or initialization.
5678 *
5679 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5680 * MPT2_IOC_DONE_RESET
5681 *
5682 * Return nothing.
5683 */
5684void
5685mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5686{
5687 switch (reset_phase) {
5688 case MPT2_IOC_PRE_RESET:
5689 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5690 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305691 break;
5692 case MPT2_IOC_AFTER_RESET:
5693 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5694 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305695 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
5696 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
5697 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
5698 complete(&ioc->scsih_cmds.done);
5699 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305700 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5701 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5702 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5703 complete(&ioc->tm_cmds.done);
5704 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305705 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305706 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305707 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305708 break;
5709 case MPT2_IOC_DONE_RESET:
5710 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5711 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305712 _scsih_sas_host_refresh(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305713 break;
Eric Moore635374e2009-03-09 01:21:12 -06005714 }
5715}
5716
5717/**
5718 * _firmware_event_work - delayed task for processing firmware events
5719 * @ioc: per adapter object
5720 * @work: equal to the fw_event_work object
5721 * Context: user.
5722 *
5723 * Return nothing.
5724 */
5725static void
5726_firmware_event_work(struct work_struct *work)
5727{
5728 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305729 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06005730 unsigned long flags;
5731 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5732
Eric Moore635374e2009-03-09 01:21:12 -06005733 /* the queue is being flushed so ignore this event */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305734 if (ioc->remove_host || fw_event->cancel_pending_work) {
Eric Moore635374e2009-03-09 01:21:12 -06005735 _scsih_fw_event_free(ioc, fw_event);
5736 return;
5737 }
Eric Moore635374e2009-03-09 01:21:12 -06005738
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305739 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
5740 _scsih_fw_event_free(ioc, fw_event);
5741 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5742 if (ioc->shost_recovery) {
5743 init_completion(&ioc->shost_recovery_done);
5744 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5745 flags);
5746 wait_for_completion(&ioc->shost_recovery_done);
5747 } else
5748 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5749 flags);
5750 _scsih_search_responding_sas_devices(ioc);
5751 _scsih_search_responding_raid_devices(ioc);
5752 _scsih_search_responding_expanders(ioc);
5753 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005754 return;
5755 }
Eric Moore635374e2009-03-09 01:21:12 -06005756
5757 switch (fw_event->event) {
5758 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305759 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005760 break;
5761 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305762 _scsih_sas_device_status_change_event(ioc,
5763 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005764 break;
5765 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305766 _scsih_sas_discovery_event(ioc,
5767 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005768 break;
5769 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305770 _scsih_sas_broadcast_primative_event(ioc,
5771 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005772 break;
5773 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5774 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305775 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005776 break;
5777 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305778 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005779 break;
5780 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305781 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005782 break;
5783 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305784 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005785 break;
5786 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305787 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005788 break;
5789 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305790 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005791 break;
5792 }
5793 _scsih_fw_event_free(ioc, fw_event);
5794}
5795
5796/**
5797 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5798 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305799 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06005800 * @reply: reply message frame(lower 32bit addr)
5801 * Context: interrupt.
5802 *
5803 * This function merely adds a new work task into ioc->firmware_event_thread.
5804 * The tasks are worked from _firmware_event_work in user context.
5805 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305806 * Return 1 meaning mf should be freed from _base_interrupt
5807 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06005808 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305809u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305810mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5811 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06005812{
5813 struct fw_event_work *fw_event;
5814 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06005815 u16 event;
5816
5817 /* events turned off due to host reset or driver unloading */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305818 if (ioc->remove_host)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305819 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005820
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305821 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06005822 event = le16_to_cpu(mpi_reply->Event);
5823
5824 switch (event) {
5825 /* handle these */
5826 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5827 {
5828 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5829 (Mpi2EventDataSasBroadcastPrimitive_t *)
5830 mpi_reply->EventData;
5831
5832 if (baen_data->Primitive !=
5833 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5834 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305835 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005836 ioc->broadcast_aen_busy = 1;
5837 break;
5838 }
5839
5840 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5841 _scsih_check_topo_delete_events(ioc,
5842 (Mpi2EventDataSasTopologyChangeList_t *)
5843 mpi_reply->EventData);
5844 break;
5845
5846 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5847 case MPI2_EVENT_IR_OPERATION_STATUS:
5848 case MPI2_EVENT_SAS_DISCOVERY:
5849 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5850 case MPI2_EVENT_IR_VOLUME:
5851 case MPI2_EVENT_IR_PHYSICAL_DISK:
5852 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5853 case MPI2_EVENT_TASK_SET_FULL:
5854 break;
5855
5856 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305857 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005858 }
5859
5860 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5861 if (!fw_event) {
5862 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5863 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305864 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005865 }
5866 fw_event->event_data =
5867 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5868 if (!fw_event->event_data) {
5869 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5870 ioc->name, __FILE__, __LINE__, __func__);
5871 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305872 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005873 }
5874
5875 memcpy(fw_event->event_data, mpi_reply->EventData,
5876 mpi_reply->EventDataLength*4);
5877 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305878 fw_event->VF_ID = mpi_reply->VF_ID;
5879 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06005880 fw_event->event = event;
5881 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305882 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005883}
5884
5885/* shost template */
5886static struct scsi_host_template scsih_driver_template = {
5887 .module = THIS_MODULE,
5888 .name = "Fusion MPT SAS Host",
5889 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005890 .queuecommand = _scsih_qcmd,
5891 .target_alloc = _scsih_target_alloc,
5892 .slave_alloc = _scsih_slave_alloc,
5893 .slave_configure = _scsih_slave_configure,
5894 .target_destroy = _scsih_target_destroy,
5895 .slave_destroy = _scsih_slave_destroy,
5896 .change_queue_depth = _scsih_change_queue_depth,
5897 .change_queue_type = _scsih_change_queue_type,
5898 .eh_abort_handler = _scsih_abort,
5899 .eh_device_reset_handler = _scsih_dev_reset,
5900 .eh_target_reset_handler = _scsih_target_reset,
5901 .eh_host_reset_handler = _scsih_host_reset,
5902 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005903 .can_queue = 1,
5904 .this_id = -1,
5905 .sg_tablesize = MPT2SAS_SG_DEPTH,
5906 .max_sectors = 8192,
5907 .cmd_per_lun = 7,
5908 .use_clustering = ENABLE_CLUSTERING,
5909 .shost_attrs = mpt2sas_host_attrs,
5910 .sdev_attrs = mpt2sas_dev_attrs,
5911};
5912
5913/**
5914 * _scsih_expander_node_remove - removing expander device from list.
5915 * @ioc: per adapter object
5916 * @sas_expander: the sas_device object
5917 * Context: Calling function should acquire ioc->sas_node_lock.
5918 *
5919 * Removing object and freeing associated memory from the
5920 * ioc->sas_expander_list.
5921 *
5922 * Return nothing.
5923 */
5924static void
5925_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5926 struct _sas_node *sas_expander)
5927{
5928 struct _sas_port *mpt2sas_port;
5929 struct _sas_device *sas_device;
5930 struct _sas_node *expander_sibling;
5931 unsigned long flags;
5932
5933 if (!sas_expander)
5934 return;
5935
5936 /* remove sibling ports attached to this expander */
5937 retry_device_search:
5938 list_for_each_entry(mpt2sas_port,
5939 &sas_expander->sas_port_list, port_list) {
5940 if (mpt2sas_port->remote_identify.device_type ==
5941 SAS_END_DEVICE) {
5942 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5943 sas_device =
5944 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5945 mpt2sas_port->remote_identify.sas_address);
5946 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5947 if (!sas_device)
5948 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305949 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305950 if (ioc->shost_recovery)
5951 return;
Eric Moore635374e2009-03-09 01:21:12 -06005952 goto retry_device_search;
5953 }
5954 }
5955
5956 retry_expander_search:
5957 list_for_each_entry(mpt2sas_port,
5958 &sas_expander->sas_port_list, port_list) {
5959
5960 if (mpt2sas_port->remote_identify.device_type ==
5961 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5962 mpt2sas_port->remote_identify.device_type ==
5963 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5964
5965 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5966 expander_sibling =
5967 mpt2sas_scsih_expander_find_by_sas_address(
5968 ioc, mpt2sas_port->remote_identify.sas_address);
5969 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5970 if (!expander_sibling)
5971 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305972 _scsih_expander_remove(ioc,
5973 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305974 if (ioc->shost_recovery)
5975 return;
Eric Moore635374e2009-03-09 01:21:12 -06005976 goto retry_expander_search;
5977 }
5978 }
5979
5980 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305981 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005982
5983 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5984 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5985 sas_expander->handle, (unsigned long long)
5986 sas_expander->sas_address);
5987
5988 list_del(&sas_expander->list);
5989 kfree(sas_expander->phy);
5990 kfree(sas_expander);
5991}
5992
5993/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305994 * _scsih_ir_shutdown - IR shutdown notification
5995 * @ioc: per adapter object
5996 *
5997 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
5998 * the host system is shutting down.
5999 *
6000 * Return nothing.
6001 */
6002static void
6003_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6004{
6005 Mpi2RaidActionRequest_t *mpi_request;
6006 Mpi2RaidActionReply_t *mpi_reply;
6007 u16 smid;
6008
6009 /* is IR firmware build loaded ? */
6010 if (!ioc->ir_firmware)
6011 return;
6012
6013 /* are there any volumes ? */
6014 if (list_empty(&ioc->raid_device_list))
6015 return;
6016
6017 mutex_lock(&ioc->scsih_cmds.mutex);
6018
6019 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6020 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6021 ioc->name, __func__);
6022 goto out;
6023 }
6024 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6025
6026 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6027 if (!smid) {
6028 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6029 ioc->name, __func__);
6030 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6031 goto out;
6032 }
6033
6034 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6035 ioc->scsih_cmds.smid = smid;
6036 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6037
6038 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6039 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6040
6041 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6042 init_completion(&ioc->scsih_cmds.done);
6043 mpt2sas_base_put_smid_default(ioc, smid);
6044 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6045
6046 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6047 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6048 ioc->name, __func__);
6049 goto out;
6050 }
6051
6052 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6053 mpi_reply = ioc->scsih_cmds.reply;
6054
6055 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6056 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6057 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6058 le32_to_cpu(mpi_reply->IOCLogInfo));
6059 }
6060
6061 out:
6062 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6063 mutex_unlock(&ioc->scsih_cmds.mutex);
6064}
6065
6066/**
6067 * _scsih_shutdown - routine call during system shutdown
6068 * @pdev: PCI device struct
6069 *
6070 * Return nothing.
6071 */
6072static void
6073_scsih_shutdown(struct pci_dev *pdev)
6074{
6075 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6076 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306077 struct workqueue_struct *wq;
6078 unsigned long flags;
6079
6080 ioc->remove_host = 1;
6081 _scsih_fw_event_cleanup_queue(ioc);
6082
6083 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6084 wq = ioc->firmware_event_thread;
6085 ioc->firmware_event_thread = NULL;
6086 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6087 if (wq)
6088 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306089
6090 _scsih_ir_shutdown(ioc);
6091 mpt2sas_base_detach(ioc);
6092}
6093
6094/**
Eric Moored5d135b2009-05-18 13:02:08 -06006095 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006096 * @pdev: PCI device struct
6097 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306098 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006099 * Return nothing.
6100 */
6101static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006102_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006103{
6104 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6105 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6106 struct _sas_port *mpt2sas_port;
6107 struct _sas_device *sas_device;
6108 struct _sas_node *expander_sibling;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306109 struct _raid_device *raid_device, *next;
6110 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006111 struct workqueue_struct *wq;
6112 unsigned long flags;
6113
6114 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306115 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006116
6117 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6118 wq = ioc->firmware_event_thread;
6119 ioc->firmware_event_thread = NULL;
6120 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6121 if (wq)
6122 destroy_workqueue(wq);
6123
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306124 /* release all the volumes */
6125 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6126 list) {
6127 if (raid_device->starget) {
6128 sas_target_priv_data =
6129 raid_device->starget->hostdata;
6130 sas_target_priv_data->deleted = 1;
6131 scsi_remove_target(&raid_device->starget->dev);
6132 }
6133 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6134 "(0x%016llx)\n", ioc->name, raid_device->handle,
6135 (unsigned long long) raid_device->wwid);
6136 _scsih_raid_device_remove(ioc, raid_device);
6137 }
6138
Eric Moore635374e2009-03-09 01:21:12 -06006139 /* free ports attached to the sas_host */
6140 retry_again:
6141 list_for_each_entry(mpt2sas_port,
6142 &ioc->sas_hba.sas_port_list, port_list) {
6143 if (mpt2sas_port->remote_identify.device_type ==
6144 SAS_END_DEVICE) {
6145 sas_device =
6146 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6147 mpt2sas_port->remote_identify.sas_address);
6148 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306149 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006150 goto retry_again;
6151 }
6152 } else {
6153 expander_sibling =
6154 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6155 mpt2sas_port->remote_identify.sas_address);
6156 if (expander_sibling) {
6157 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306158 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006159 goto retry_again;
6160 }
6161 }
6162 }
6163
6164 /* free phys attached to the sas_host */
6165 if (ioc->sas_hba.num_phys) {
6166 kfree(ioc->sas_hba.phy);
6167 ioc->sas_hba.phy = NULL;
6168 ioc->sas_hba.num_phys = 0;
6169 }
6170
6171 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306172 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006173 list_del(&ioc->list);
6174 scsi_remove_host(shost);
6175 scsi_host_put(shost);
6176}
6177
6178/**
6179 * _scsih_probe_boot_devices - reports 1st device
6180 * @ioc: per adapter object
6181 *
6182 * If specified in bios page 2, this routine reports the 1st
6183 * device scsi-ml or sas transport for persistent boot device
6184 * purposes. Please refer to function _scsih_determine_boot_device()
6185 */
6186static void
6187_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6188{
6189 u8 is_raid;
6190 void *device;
6191 struct _sas_device *sas_device;
6192 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306193 u16 handle;
6194 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006195 u64 sas_address;
6196 unsigned long flags;
6197 int rc;
6198
6199 device = NULL;
6200 if (ioc->req_boot_device.device) {
6201 device = ioc->req_boot_device.device;
6202 is_raid = ioc->req_boot_device.is_raid;
6203 } else if (ioc->req_alt_boot_device.device) {
6204 device = ioc->req_alt_boot_device.device;
6205 is_raid = ioc->req_alt_boot_device.is_raid;
6206 } else if (ioc->current_boot_device.device) {
6207 device = ioc->current_boot_device.device;
6208 is_raid = ioc->current_boot_device.is_raid;
6209 }
6210
6211 if (!device)
6212 return;
6213
6214 if (is_raid) {
6215 raid_device = device;
6216 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6217 raid_device->id, 0);
6218 if (rc)
6219 _scsih_raid_device_remove(ioc, raid_device);
6220 } else {
6221 sas_device = device;
6222 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306223 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006224 sas_address = sas_device->sas_address;
6225 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6226 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6227 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6228 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306229 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006230 _scsih_sas_device_remove(ioc, sas_device);
6231 } else if (!sas_device->starget) {
6232 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306233 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006234 _scsih_sas_device_remove(ioc, sas_device);
6235 }
6236 }
6237}
6238
6239/**
6240 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6241 * @ioc: per adapter object
6242 *
6243 * Called during initial loading of the driver.
6244 */
6245static void
6246_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6247{
6248 struct _raid_device *raid_device, *raid_next;
6249 int rc;
6250
6251 list_for_each_entry_safe(raid_device, raid_next,
6252 &ioc->raid_device_list, list) {
6253 if (raid_device->starget)
6254 continue;
6255 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6256 raid_device->id, 0);
6257 if (rc)
6258 _scsih_raid_device_remove(ioc, raid_device);
6259 }
6260}
6261
6262/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306263 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006264 * @ioc: per adapter object
6265 *
6266 * Called during initial loading of the driver.
6267 */
6268static void
6269_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6270{
6271 struct _sas_device *sas_device, *next;
6272 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006273
6274 /* SAS Device List */
6275 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6276 list) {
6277 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6278 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6279 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6280
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306281 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6282 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006283 _scsih_sas_device_remove(ioc, sas_device);
6284 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306285 mpt2sas_transport_port_remove(ioc,
6286 sas_device->sas_address,
6287 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006288 _scsih_sas_device_remove(ioc, sas_device);
6289 }
6290 }
6291}
6292
6293/**
6294 * _scsih_probe_devices - probing for devices
6295 * @ioc: per adapter object
6296 *
6297 * Called during initial loading of the driver.
6298 */
6299static void
6300_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6301{
6302 u16 volume_mapping_flags =
6303 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6304 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6305
6306 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6307 return; /* return when IOC doesn't support initiator mode */
6308
6309 _scsih_probe_boot_devices(ioc);
6310
6311 if (ioc->ir_firmware) {
6312 if ((volume_mapping_flags &
6313 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6314 _scsih_probe_sas(ioc);
6315 _scsih_probe_raid(ioc);
6316 } else {
6317 _scsih_probe_raid(ioc);
6318 _scsih_probe_sas(ioc);
6319 }
6320 } else
6321 _scsih_probe_sas(ioc);
6322}
6323
6324/**
Eric Moored5d135b2009-05-18 13:02:08 -06006325 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006326 * @pdev: PCI device struct
6327 * @id: pci device id
6328 *
6329 * Returns 0 success, anything else error.
6330 */
6331static int
Eric Moored5d135b2009-05-18 13:02:08 -06006332_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006333{
6334 struct MPT2SAS_ADAPTER *ioc;
6335 struct Scsi_Host *shost;
6336
6337 shost = scsi_host_alloc(&scsih_driver_template,
6338 sizeof(struct MPT2SAS_ADAPTER));
6339 if (!shost)
6340 return -ENODEV;
6341
6342 /* init local params */
6343 ioc = shost_priv(shost);
6344 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6345 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006346 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006347 ioc->shost = shost;
6348 ioc->id = mpt_ids++;
6349 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6350 ioc->pdev = pdev;
6351 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6352 ioc->tm_cb_idx = tm_cb_idx;
6353 ioc->ctl_cb_idx = ctl_cb_idx;
6354 ioc->base_cb_idx = base_cb_idx;
6355 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306356 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006357 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306358 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6359 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006360 ioc->logging_level = logging_level;
6361 /* misc semaphores and spin locks */
6362 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6363 spin_lock_init(&ioc->scsi_lookup_lock);
6364 spin_lock_init(&ioc->sas_device_lock);
6365 spin_lock_init(&ioc->sas_node_lock);
6366 spin_lock_init(&ioc->fw_event_lock);
6367 spin_lock_init(&ioc->raid_device_lock);
6368
6369 INIT_LIST_HEAD(&ioc->sas_device_list);
6370 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6371 INIT_LIST_HEAD(&ioc->sas_expander_list);
6372 INIT_LIST_HEAD(&ioc->fw_event_list);
6373 INIT_LIST_HEAD(&ioc->raid_device_list);
6374 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306375 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Eric Moore635374e2009-03-09 01:21:12 -06006376
6377 /* init shost parameters */
6378 shost->max_cmd_len = 16;
6379 shost->max_lun = max_lun;
6380 shost->transportt = mpt2sas_transport_template;
6381 shost->unique_id = ioc->id;
6382
6383 if ((scsi_add_host(shost, &pdev->dev))) {
6384 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6385 ioc->name, __FILE__, __LINE__, __func__);
6386 list_del(&ioc->list);
6387 goto out_add_shost_fail;
6388 }
6389
Eric Moore3c621b32009-05-18 12:59:41 -06006390 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6391 | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306392 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006393
Eric Moore635374e2009-03-09 01:21:12 -06006394 /* event thread */
6395 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6396 "fw_event%d", ioc->id);
6397 ioc->firmware_event_thread = create_singlethread_workqueue(
6398 ioc->firmware_event_name);
6399 if (!ioc->firmware_event_thread) {
6400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6401 ioc->name, __FILE__, __LINE__, __func__);
6402 goto out_thread_fail;
6403 }
6404
6405 ioc->wait_for_port_enable_to_complete = 1;
6406 if ((mpt2sas_base_attach(ioc))) {
6407 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6408 ioc->name, __FILE__, __LINE__, __func__);
6409 goto out_attach_fail;
6410 }
6411
6412 ioc->wait_for_port_enable_to_complete = 0;
6413 _scsih_probe_devices(ioc);
6414 return 0;
6415
6416 out_attach_fail:
6417 destroy_workqueue(ioc->firmware_event_thread);
6418 out_thread_fail:
6419 list_del(&ioc->list);
6420 scsi_remove_host(shost);
6421 out_add_shost_fail:
6422 return -ENODEV;
6423}
6424
6425#ifdef CONFIG_PM
6426/**
Eric Moored5d135b2009-05-18 13:02:08 -06006427 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006428 * @pdev: PCI device struct
6429 * @state: PM state change to (usually PCI_D3)
6430 *
6431 * Returns 0 success, anything else error.
6432 */
6433static int
Eric Moored5d135b2009-05-18 13:02:08 -06006434_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006435{
6436 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6437 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6438 u32 device_state;
6439
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306440 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006441 flush_scheduled_work();
6442 scsi_block_requests(shost);
6443 device_state = pci_choose_state(pdev, state);
6444 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6445 "operating state [D%d]\n", ioc->name, pdev,
6446 pci_name(pdev), device_state);
6447
6448 mpt2sas_base_free_resources(ioc);
6449 pci_save_state(pdev);
6450 pci_disable_device(pdev);
6451 pci_set_power_state(pdev, device_state);
6452 return 0;
6453}
6454
6455/**
Eric Moored5d135b2009-05-18 13:02:08 -06006456 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006457 * @pdev: PCI device struct
6458 *
6459 * Returns 0 success, anything else error.
6460 */
6461static int
Eric Moored5d135b2009-05-18 13:02:08 -06006462_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006463{
6464 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6465 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6466 u32 device_state = pdev->current_state;
6467 int r;
6468
6469 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6470 "operating state [D%d]\n", ioc->name, pdev,
6471 pci_name(pdev), device_state);
6472
6473 pci_set_power_state(pdev, PCI_D0);
6474 pci_enable_wake(pdev, PCI_D0, 0);
6475 pci_restore_state(pdev);
6476 ioc->pdev = pdev;
6477 r = mpt2sas_base_map_resources(ioc);
6478 if (r)
6479 return r;
6480
6481 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6482 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306483 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006484 return 0;
6485}
6486#endif /* CONFIG_PM */
6487
6488
6489static struct pci_driver scsih_driver = {
6490 .name = MPT2SAS_DRIVER_NAME,
6491 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06006492 .probe = _scsih_probe,
6493 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05306494 .shutdown = _scsih_shutdown,
Eric Moore635374e2009-03-09 01:21:12 -06006495#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06006496 .suspend = _scsih_suspend,
6497 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06006498#endif
6499};
6500
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306501/* raid transport support */
6502static struct raid_function_template mpt2sas_raid_functions = {
6503 .cookie = &scsih_driver_template,
6504 .is_raid = _scsih_is_raid,
6505 .get_resync = _scsih_get_resync,
6506 .get_state = _scsih_get_state,
6507};
Eric Moore635374e2009-03-09 01:21:12 -06006508
6509/**
Eric Moored5d135b2009-05-18 13:02:08 -06006510 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06006511 *
6512 * Returns 0 success, anything else error.
6513 */
6514static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06006515_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06006516{
6517 int error;
6518
6519 mpt_ids = 0;
6520 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6521 MPT2SAS_DRIVER_VERSION);
6522
6523 mpt2sas_transport_template =
6524 sas_attach_transport(&mpt2sas_transport_functions);
6525 if (!mpt2sas_transport_template)
6526 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306527 /* raid transport support */
6528 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
6529 if (!mpt2sas_raid_template) {
6530 sas_release_transport(mpt2sas_transport_template);
6531 return -ENODEV;
6532 }
Eric Moore635374e2009-03-09 01:21:12 -06006533
6534 mpt2sas_base_initialize_callback_handler();
6535
6536 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06006537 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06006538
6539 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06006540 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06006541
6542 /* base internal commands callback handler */
6543 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6544
6545 /* transport internal commands callback handler */
6546 transport_cb_idx = mpt2sas_base_register_callback_handler(
6547 mpt2sas_transport_done);
6548
Kashyap, Desai744090d2009-10-05 15:56:56 +05306549 /* scsih internal commands callback handler */
6550 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
6551
Eric Moore635374e2009-03-09 01:21:12 -06006552 /* configuration page API internal commands callback handler */
6553 config_cb_idx = mpt2sas_base_register_callback_handler(
6554 mpt2sas_config_done);
6555
6556 /* ctl module callback handler */
6557 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6558
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306559 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6560 _scsih_tm_tr_complete);
6561 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6562 _scsih_sas_control_complete);
6563
Eric Moore635374e2009-03-09 01:21:12 -06006564 mpt2sas_ctl_init();
6565
6566 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306567 if (error) {
6568 /* raid transport support */
6569 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06006570 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306571 }
Eric Moore635374e2009-03-09 01:21:12 -06006572
6573 return error;
6574}
6575
6576/**
Eric Moored5d135b2009-05-18 13:02:08 -06006577 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06006578 *
6579 * Returns 0 success, anything else error.
6580 */
6581static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06006582_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06006583{
6584 printk(KERN_INFO "mpt2sas version %s unloading\n",
6585 MPT2SAS_DRIVER_VERSION);
6586
6587 pci_unregister_driver(&scsih_driver);
6588
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306589 mpt2sas_ctl_exit();
6590
Eric Moore635374e2009-03-09 01:21:12 -06006591 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6592 mpt2sas_base_release_callback_handler(tm_cb_idx);
6593 mpt2sas_base_release_callback_handler(base_cb_idx);
6594 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306595 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06006596 mpt2sas_base_release_callback_handler(config_cb_idx);
6597 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6598
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306599 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6600 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6601
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306602 /* raid transport support */
6603 raid_class_release(mpt2sas_raid_template);
6604 sas_release_transport(mpt2sas_transport_template);
6605
Eric Moore635374e2009-03-09 01:21:12 -06006606}
6607
Eric Moored5d135b2009-05-18 13:02:08 -06006608module_init(_scsih_init);
6609module_exit(_scsih_exit);