blob: 9ce2253039387ac2a2e548dc3b7ac8cef2ad5288 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
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, Desaief7c80c2010-04-05 14:20:07 +053055#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053056#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090057#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060058
59#include "mpt2sas_base.h"
60
61MODULE_AUTHOR(MPT2SAS_AUTHOR);
62MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
63MODULE_LICENSE("GPL");
64MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
65
66#define RAID_CHANNEL 1
67
68/* forward proto's */
69static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
70 struct _sas_node *sas_expander);
71static void _firmware_event_work(struct work_struct *work);
72
73/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060074LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060075
76/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060077static u8 scsi_io_cb_idx = -1;
78static u8 tm_cb_idx = -1;
79static u8 ctl_cb_idx = -1;
80static u8 base_cb_idx = -1;
81static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053082static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060083static u8 config_cb_idx = -1;
84static int mpt_ids;
85
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053086static u8 tm_tr_cb_idx = -1 ;
87static u8 tm_sas_control_cb_idx = -1;
88
Eric Moore635374e2009-03-09 01:21:12 -060089/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060090static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060091MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
92 "(default=0)");
93
94/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
95#define MPT2SAS_MAX_LUN (16895)
96static int max_lun = MPT2SAS_MAX_LUN;
97module_param(max_lun, int, 0);
98MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
99
100/**
101 * struct sense_info - common structure for obtaining sense keys
102 * @skey: sense key
103 * @asc: additional sense code
104 * @ascq: additional sense code qualifier
105 */
106struct sense_info {
107 u8 skey;
108 u8 asc;
109 u8 ascq;
110};
111
112
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530113#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
114
Eric Moore635374e2009-03-09 01:21:12 -0600115/**
116 * struct fw_event_work - firmware event struct
117 * @list: link list framework
118 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530119 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600120 * @ioc: per adapter object
121 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530122 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600123 * @ignore: flag meaning this event has been marked to ignore
124 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
125 * @event_data: reply event data payload follows
126 *
127 * This object stored on ioc->fw_event_list.
128 */
129struct fw_event_work {
130 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530131 u8 cancel_pending_work;
132 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600133 struct MPT2SAS_ADAPTER *ioc;
134 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530135 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600136 u8 ignore;
137 u16 event;
138 void *event_data;
139};
140
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530141/* raid transport support */
142static struct raid_template *mpt2sas_raid_template;
143
Eric Moore635374e2009-03-09 01:21:12 -0600144/**
145 * struct _scsi_io_transfer - scsi io transfer
146 * @handle: sas device handle (assigned by firmware)
147 * @is_raid: flag set for hidden raid components
148 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
149 * @data_length: data transfer length
150 * @data_dma: dma pointer to data
151 * @sense: sense data
152 * @lun: lun number
153 * @cdb_length: cdb length
154 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600155 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530156 * @VF_ID: virtual function id
157 * @VP_ID: virtual port id
158 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600159 * @sense_length: sense length
160 * @ioc_status: ioc status
161 * @scsi_state: scsi state
162 * @scsi_status: scsi staus
163 * @log_info: log information
164 * @transfer_length: data length transfer when there is a reply message
165 *
166 * Used for sending internal scsi commands to devices within this module.
167 * Refer to _scsi_send_scsi_io().
168 */
169struct _scsi_io_transfer {
170 u16 handle;
171 u8 is_raid;
172 enum dma_data_direction dir;
173 u32 data_length;
174 dma_addr_t data_dma;
175 u8 sense[SCSI_SENSE_BUFFERSIZE];
176 u32 lun;
177 u8 cdb_length;
178 u8 cdb[32];
179 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530180 u8 VF_ID;
181 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600182 u8 valid_reply;
183 /* the following bits are only valid when 'valid_reply = 1' */
184 u32 sense_length;
185 u16 ioc_status;
186 u8 scsi_state;
187 u8 scsi_status;
188 u32 log_info;
189 u32 transfer_length;
190};
191
192/*
193 * The pci device ids are defined in mpi/mpi2_cnfg.h.
194 */
195static struct pci_device_id scsih_pci_table[] = {
196 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
197 PCI_ANY_ID, PCI_ANY_ID },
198 /* Falcon ~ 2008*/
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
200 PCI_ANY_ID, PCI_ANY_ID },
201 /* Liberator ~ 2108 */
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
203 PCI_ANY_ID, PCI_ANY_ID },
204 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
205 PCI_ANY_ID, PCI_ANY_ID },
206 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
207 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530208 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
210 PCI_ANY_ID, PCI_ANY_ID },
211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
212 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530213 /* Thunderbolt ~ 2208 */
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
215 PCI_ANY_ID, PCI_ANY_ID },
216 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
217 PCI_ANY_ID, PCI_ANY_ID },
218 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
219 PCI_ANY_ID, PCI_ANY_ID },
220 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
221 PCI_ANY_ID, PCI_ANY_ID },
222 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
223 PCI_ANY_ID, PCI_ANY_ID },
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
225 PCI_ANY_ID, PCI_ANY_ID },
226 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
227 PCI_ANY_ID, PCI_ANY_ID },
228 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
229 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600230 {0} /* Terminating entry */
231};
232MODULE_DEVICE_TABLE(pci, scsih_pci_table);
233
234/**
Eric Moored5d135b2009-05-18 13:02:08 -0600235 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600236 *
237 * Note: The logging levels are defined in mpt2sas_debug.h.
238 */
239static int
Eric Moored5d135b2009-05-18 13:02:08 -0600240_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600241{
242 int ret = param_set_int(val, kp);
243 struct MPT2SAS_ADAPTER *ioc;
244
245 if (ret)
246 return ret;
247
248 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600249 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600250 ioc->logging_level = logging_level;
251 return 0;
252}
Eric Moored5d135b2009-05-18 13:02:08 -0600253module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600254 &logging_level, 0644);
255
256/**
257 * _scsih_srch_boot_sas_address - search based on sas_address
258 * @sas_address: sas address
259 * @boot_device: boot device object from bios page 2
260 *
261 * Returns 1 when there's a match, 0 means no match.
262 */
263static inline int
264_scsih_srch_boot_sas_address(u64 sas_address,
265 Mpi2BootDeviceSasWwid_t *boot_device)
266{
267 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
268}
269
270/**
271 * _scsih_srch_boot_device_name - search based on device name
272 * @device_name: device name specified in INDENTIFY fram
273 * @boot_device: boot device object from bios page 2
274 *
275 * Returns 1 when there's a match, 0 means no match.
276 */
277static inline int
278_scsih_srch_boot_device_name(u64 device_name,
279 Mpi2BootDeviceDeviceName_t *boot_device)
280{
281 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
282}
283
284/**
285 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
286 * @enclosure_logical_id: enclosure logical id
287 * @slot_number: slot number
288 * @boot_device: boot device object from bios page 2
289 *
290 * Returns 1 when there's a match, 0 means no match.
291 */
292static inline int
293_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
294 Mpi2BootDeviceEnclosureSlot_t *boot_device)
295{
296 return (enclosure_logical_id == le64_to_cpu(boot_device->
297 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
298 SlotNumber)) ? 1 : 0;
299}
300
301/**
302 * _scsih_is_boot_device - search for matching boot device.
303 * @sas_address: sas address
304 * @device_name: device name specified in INDENTIFY fram
305 * @enclosure_logical_id: enclosure logical id
306 * @slot_number: slot number
307 * @form: specifies boot device form
308 * @boot_device: boot device object from bios page 2
309 *
310 * Returns 1 when there's a match, 0 means no match.
311 */
312static int
313_scsih_is_boot_device(u64 sas_address, u64 device_name,
314 u64 enclosure_logical_id, u16 slot, u8 form,
315 Mpi2BiosPage2BootDevice_t *boot_device)
316{
317 int rc = 0;
318
319 switch (form) {
320 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
321 if (!sas_address)
322 break;
323 rc = _scsih_srch_boot_sas_address(
324 sas_address, &boot_device->SasWwid);
325 break;
326 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
327 if (!enclosure_logical_id)
328 break;
329 rc = _scsih_srch_boot_encl_slot(
330 enclosure_logical_id,
331 slot, &boot_device->EnclosureSlot);
332 break;
333 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
334 if (!device_name)
335 break;
336 rc = _scsih_srch_boot_device_name(
337 device_name, &boot_device->DeviceName);
338 break;
339 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
340 break;
341 }
342
343 return rc;
344}
345
346/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530347 * _scsih_get_sas_address - set the sas_address for given device handle
348 * @handle: device handle
349 * @sas_address: sas address
350 *
351 * Returns 0 success, non-zero when failure
352 */
353static int
354_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
355 u64 *sas_address)
356{
357 Mpi2SasDevicePage0_t sas_device_pg0;
358 Mpi2ConfigReply_t mpi_reply;
359 u32 ioc_status;
360
361 if (handle <= ioc->sas_hba.num_phys) {
362 *sas_address = ioc->sas_hba.sas_address;
363 return 0;
364 } else
365 *sas_address = 0;
366
367 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
368 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
369 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
370 ioc->name, __FILE__, __LINE__, __func__);
371 return -ENXIO;
372 }
373
374 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
375 MPI2_IOCSTATUS_MASK;
376 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
377 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
378 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
379 __FILE__, __LINE__, __func__);
380 return -EIO;
381 }
382
383 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
384 return 0;
385}
386
387/**
Eric Moore635374e2009-03-09 01:21:12 -0600388 * _scsih_determine_boot_device - determine boot device.
389 * @ioc: per adapter object
390 * @device: either sas_device or raid_device object
391 * @is_raid: [flag] 1 = raid object, 0 = sas object
392 *
393 * Determines whether this device should be first reported device to
394 * to scsi-ml or sas transport, this purpose is for persistant boot device.
395 * There are primary, alternate, and current entries in bios page 2. The order
396 * priority is primary, alternate, then current. This routine saves
397 * the corresponding device object and is_raid flag in the ioc object.
398 * The saved data to be used later in _scsih_probe_boot_devices().
399 */
400static void
401_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
402 void *device, u8 is_raid)
403{
404 struct _sas_device *sas_device;
405 struct _raid_device *raid_device;
406 u64 sas_address;
407 u64 device_name;
408 u64 enclosure_logical_id;
409 u16 slot;
410
411 /* only process this function when driver loads */
412 if (!ioc->wait_for_port_enable_to_complete)
413 return;
414
415 if (!is_raid) {
416 sas_device = device;
417 sas_address = sas_device->sas_address;
418 device_name = sas_device->device_name;
419 enclosure_logical_id = sas_device->enclosure_logical_id;
420 slot = sas_device->slot;
421 } else {
422 raid_device = device;
423 sas_address = raid_device->wwid;
424 device_name = 0;
425 enclosure_logical_id = 0;
426 slot = 0;
427 }
428
429 if (!ioc->req_boot_device.device) {
430 if (_scsih_is_boot_device(sas_address, device_name,
431 enclosure_logical_id, slot,
432 (ioc->bios_pg2.ReqBootDeviceForm &
433 MPI2_BIOSPAGE2_FORM_MASK),
434 &ioc->bios_pg2.RequestedBootDevice)) {
435 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
436 "%s: req_boot_device(0x%016llx)\n",
437 ioc->name, __func__,
438 (unsigned long long)sas_address));
439 ioc->req_boot_device.device = device;
440 ioc->req_boot_device.is_raid = is_raid;
441 }
442 }
443
444 if (!ioc->req_alt_boot_device.device) {
445 if (_scsih_is_boot_device(sas_address, device_name,
446 enclosure_logical_id, slot,
447 (ioc->bios_pg2.ReqAltBootDeviceForm &
448 MPI2_BIOSPAGE2_FORM_MASK),
449 &ioc->bios_pg2.RequestedAltBootDevice)) {
450 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
451 "%s: req_alt_boot_device(0x%016llx)\n",
452 ioc->name, __func__,
453 (unsigned long long)sas_address));
454 ioc->req_alt_boot_device.device = device;
455 ioc->req_alt_boot_device.is_raid = is_raid;
456 }
457 }
458
459 if (!ioc->current_boot_device.device) {
460 if (_scsih_is_boot_device(sas_address, device_name,
461 enclosure_logical_id, slot,
462 (ioc->bios_pg2.CurrentBootDeviceForm &
463 MPI2_BIOSPAGE2_FORM_MASK),
464 &ioc->bios_pg2.CurrentBootDevice)) {
465 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
466 "%s: current_boot_device(0x%016llx)\n",
467 ioc->name, __func__,
468 (unsigned long long)sas_address));
469 ioc->current_boot_device.device = device;
470 ioc->current_boot_device.is_raid = is_raid;
471 }
472 }
473}
474
475/**
476 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
477 * @ioc: per adapter object
478 * @sas_address: sas address
479 * Context: Calling function should acquire ioc->sas_device_lock
480 *
481 * This searches for sas_device based on sas_address, then return sas_device
482 * object.
483 */
484struct _sas_device *
485mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
486 u64 sas_address)
487{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530488 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600489
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530490 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
491 if (sas_device->sas_address == sas_address)
492 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600493
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530494 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
495 if (sas_device->sas_address == sas_address)
496 return sas_device;
497
498 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600499}
500
501/**
502 * _scsih_sas_device_find_by_handle - sas device search
503 * @ioc: per adapter object
504 * @handle: sas device handle (assigned by firmware)
505 * Context: Calling function should acquire ioc->sas_device_lock
506 *
507 * This searches for sas_device based on sas_address, then return sas_device
508 * object.
509 */
510static struct _sas_device *
511_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
512{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530513 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600514
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530515 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
516 if (sas_device->handle == handle)
517 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600518
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530519 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
520 if (sas_device->handle == handle)
521 return sas_device;
522
523 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600524}
525
526/**
527 * _scsih_sas_device_remove - remove sas_device from list.
528 * @ioc: per adapter object
529 * @sas_device: the sas_device object
530 * Context: This function will acquire ioc->sas_device_lock.
531 *
532 * Removing object and freeing associated memory from the ioc->sas_device_list.
533 */
534static void
535_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
536 struct _sas_device *sas_device)
537{
538 unsigned long flags;
539
Kashyap, Desai980ead32010-04-08 17:55:22 +0530540 if (!sas_device)
541 return;
542
Eric Moore635374e2009-03-09 01:21:12 -0600543 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530544 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
545 sas_device->sas_address)) {
546 list_del(&sas_device->list);
547 kfree(sas_device);
548 }
Eric Moore635374e2009-03-09 01:21:12 -0600549 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
550}
551
552/**
553 * _scsih_sas_device_add - insert sas_device to the list.
554 * @ioc: per adapter object
555 * @sas_device: the sas_device object
556 * Context: This function will acquire ioc->sas_device_lock.
557 *
558 * Adding new object to the ioc->sas_device_list.
559 */
560static void
561_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
562 struct _sas_device *sas_device)
563{
564 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600565
566 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
567 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
568 sas_device->handle, (unsigned long long)sas_device->sas_address));
569
570 spin_lock_irqsave(&ioc->sas_device_lock, flags);
571 list_add_tail(&sas_device->list, &ioc->sas_device_list);
572 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
573
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530574 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
575 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600576 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600577}
578
579/**
580 * _scsih_sas_device_init_add - insert sas_device to the list.
581 * @ioc: per adapter object
582 * @sas_device: the sas_device object
583 * Context: This function will acquire ioc->sas_device_lock.
584 *
585 * Adding new object at driver load time to the ioc->sas_device_init_list.
586 */
587static void
588_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
589 struct _sas_device *sas_device)
590{
591 unsigned long flags;
592
593 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
594 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
595 sas_device->handle, (unsigned long long)sas_device->sas_address));
596
597 spin_lock_irqsave(&ioc->sas_device_lock, flags);
598 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
599 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
600 _scsih_determine_boot_device(ioc, sas_device, 0);
601}
602
603/**
Eric Moore635374e2009-03-09 01:21:12 -0600604 * _scsih_raid_device_find_by_id - raid device search
605 * @ioc: per adapter object
606 * @id: sas device target id
607 * @channel: sas device channel
608 * Context: Calling function should acquire ioc->raid_device_lock
609 *
610 * This searches for raid_device based on target id, then return raid_device
611 * object.
612 */
613static struct _raid_device *
614_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
615{
616 struct _raid_device *raid_device, *r;
617
618 r = NULL;
619 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
620 if (raid_device->id == id && raid_device->channel == channel) {
621 r = raid_device;
622 goto out;
623 }
624 }
625
626 out:
627 return r;
628}
629
630/**
631 * _scsih_raid_device_find_by_handle - raid device search
632 * @ioc: per adapter object
633 * @handle: sas device handle (assigned by firmware)
634 * Context: Calling function should acquire ioc->raid_device_lock
635 *
636 * This searches for raid_device based on handle, then return raid_device
637 * object.
638 */
639static struct _raid_device *
640_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
641{
642 struct _raid_device *raid_device, *r;
643
644 r = NULL;
645 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
646 if (raid_device->handle != handle)
647 continue;
648 r = raid_device;
649 goto out;
650 }
651
652 out:
653 return r;
654}
655
656/**
657 * _scsih_raid_device_find_by_wwid - raid device search
658 * @ioc: per adapter object
659 * @handle: sas device handle (assigned by firmware)
660 * Context: Calling function should acquire ioc->raid_device_lock
661 *
662 * This searches for raid_device based on wwid, then return raid_device
663 * object.
664 */
665static struct _raid_device *
666_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
667{
668 struct _raid_device *raid_device, *r;
669
670 r = NULL;
671 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
672 if (raid_device->wwid != wwid)
673 continue;
674 r = raid_device;
675 goto out;
676 }
677
678 out:
679 return r;
680}
681
682/**
683 * _scsih_raid_device_add - add raid_device object
684 * @ioc: per adapter object
685 * @raid_device: raid_device object
686 *
687 * This is added to the raid_device_list link list.
688 */
689static void
690_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
691 struct _raid_device *raid_device)
692{
693 unsigned long flags;
694
695 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
696 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
697 raid_device->handle, (unsigned long long)raid_device->wwid));
698
699 spin_lock_irqsave(&ioc->raid_device_lock, flags);
700 list_add_tail(&raid_device->list, &ioc->raid_device_list);
701 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
702}
703
704/**
705 * _scsih_raid_device_remove - delete raid_device object
706 * @ioc: per adapter object
707 * @raid_device: raid_device object
708 *
709 * This is removed from the raid_device_list link list.
710 */
711static void
712_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
713 struct _raid_device *raid_device)
714{
715 unsigned long flags;
716
717 spin_lock_irqsave(&ioc->raid_device_lock, flags);
718 list_del(&raid_device->list);
719 memset(raid_device, 0, sizeof(struct _raid_device));
720 kfree(raid_device);
721 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
722}
723
724/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530725 * mpt2sas_scsih_expander_find_by_handle - expander device search
726 * @ioc: per adapter object
727 * @handle: expander handle (assigned by firmware)
728 * Context: Calling function should acquire ioc->sas_device_lock
729 *
730 * This searches for expander device based on handle, then returns the
731 * sas_node object.
732 */
733struct _sas_node *
734mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
735{
736 struct _sas_node *sas_expander, *r;
737
738 r = NULL;
739 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
740 if (sas_expander->handle != handle)
741 continue;
742 r = sas_expander;
743 goto out;
744 }
745 out:
746 return r;
747}
748
749/**
Eric Moore635374e2009-03-09 01:21:12 -0600750 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
751 * @ioc: per adapter object
752 * @sas_address: sas address
753 * Context: Calling function should acquire ioc->sas_node_lock.
754 *
755 * This searches for expander device based on sas_address, then returns the
756 * sas_node object.
757 */
758struct _sas_node *
759mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
760 u64 sas_address)
761{
762 struct _sas_node *sas_expander, *r;
763
764 r = NULL;
765 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
766 if (sas_expander->sas_address != sas_address)
767 continue;
768 r = sas_expander;
769 goto out;
770 }
771 out:
772 return r;
773}
774
775/**
776 * _scsih_expander_node_add - insert expander device to the list.
777 * @ioc: per adapter object
778 * @sas_expander: the sas_device object
779 * Context: This function will acquire ioc->sas_node_lock.
780 *
781 * Adding new object to the ioc->sas_expander_list.
782 *
783 * Return nothing.
784 */
785static void
786_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
787 struct _sas_node *sas_expander)
788{
789 unsigned long flags;
790
791 spin_lock_irqsave(&ioc->sas_node_lock, flags);
792 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
793 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
794}
795
796/**
797 * _scsih_is_end_device - determines if device is an end device
798 * @device_info: bitfield providing information about the device.
799 * Context: none
800 *
801 * Returns 1 if end device.
802 */
803static int
804_scsih_is_end_device(u32 device_info)
805{
806 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
807 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
808 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
809 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
810 return 1;
811 else
812 return 0;
813}
814
815/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530816 * mptscsih_get_scsi_lookup - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600817 * @ioc: per adapter object
818 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600819 *
820 * Returns the smid stored scmd pointer.
821 */
822static struct scsi_cmnd *
823_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
824{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530825 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600826}
827
828/**
829 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
830 * @ioc: per adapter object
831 * @smid: system request message index
832 * @scmd: pointer to scsi command object
833 * Context: This function will acquire ioc->scsi_lookup_lock.
834 *
835 * This will search for a scmd pointer in the scsi_lookup array,
836 * returning the revelent smid. A returned value of zero means invalid.
837 */
838static u16
839_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
840 *scmd)
841{
842 u16 smid;
843 unsigned long flags;
844 int i;
845
846 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
847 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530848 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600849 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530850 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600851 goto out;
852 }
853 }
854 out:
855 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
856 return smid;
857}
858
859/**
860 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
861 * @ioc: per adapter object
862 * @id: target id
863 * @channel: channel
864 * Context: This function will acquire ioc->scsi_lookup_lock.
865 *
866 * This will search for a matching channel:id in the scsi_lookup array,
867 * returning 1 if found.
868 */
869static u8
870_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
871 int channel)
872{
873 u8 found;
874 unsigned long flags;
875 int i;
876
877 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
878 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530879 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600880 if (ioc->scsi_lookup[i].scmd &&
881 (ioc->scsi_lookup[i].scmd->device->id == id &&
882 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
883 found = 1;
884 goto out;
885 }
886 }
887 out:
888 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
889 return found;
890}
891
892/**
Eric Moore993e0da2009-05-18 13:00:45 -0600893 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
894 * @ioc: per adapter object
895 * @id: target id
896 * @lun: lun number
897 * @channel: channel
898 * Context: This function will acquire ioc->scsi_lookup_lock.
899 *
900 * This will search for a matching channel:id:lun in the scsi_lookup array,
901 * returning 1 if found.
902 */
903static u8
904_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
905 unsigned int lun, int channel)
906{
907 u8 found;
908 unsigned long flags;
909 int i;
910
911 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
912 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530913 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600914 if (ioc->scsi_lookup[i].scmd &&
915 (ioc->scsi_lookup[i].scmd->device->id == id &&
916 ioc->scsi_lookup[i].scmd->device->channel == channel &&
917 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
918 found = 1;
919 goto out;
920 }
921 }
922 out:
923 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
924 return found;
925}
926
927/**
Eric Moore635374e2009-03-09 01:21:12 -0600928 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
929 * @ioc: per adapter object
930 * @smid: system request message index
931 *
932 * Returns phys pointer to chain buffer.
933 */
934static dma_addr_t
935_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
936{
937 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
938 ioc->chains_needed_per_io));
939}
940
941/**
942 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
943 * @ioc: per adapter object
944 * @smid: system request message index
945 *
946 * Returns virt pointer to chain buffer.
947 */
948static void *
949_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
950{
951 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
952 ioc->chains_needed_per_io)));
953}
954
955/**
956 * _scsih_build_scatter_gather - main sg creation routine
957 * @ioc: per adapter object
958 * @scmd: scsi command
959 * @smid: system request message index
960 * Context: none.
961 *
962 * The main routine that builds scatter gather table from a given
963 * scsi request sent via the .queuecommand main handler.
964 *
965 * Returns 0 success, anything else error
966 */
967static int
968_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
969 struct scsi_cmnd *scmd, u16 smid)
970{
971 Mpi2SCSIIORequest_t *mpi_request;
972 dma_addr_t chain_dma;
973 struct scatterlist *sg_scmd;
974 void *sg_local, *chain;
975 u32 chain_offset;
976 u32 chain_length;
977 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900978 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -0600979 u32 sges_in_segment;
980 u32 sgl_flags;
981 u32 sgl_flags_last_element;
982 u32 sgl_flags_end_buffer;
983
984 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
985
986 /* init scatter gather flags */
987 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
988 if (scmd->sc_data_direction == DMA_TO_DEVICE)
989 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
990 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
991 << MPI2_SGE_FLAGS_SHIFT;
992 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
993 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
994 << MPI2_SGE_FLAGS_SHIFT;
995 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
996
997 sg_scmd = scsi_sglist(scmd);
998 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900999 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001000 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1001 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1002 return -ENOMEM;
1003 }
1004
1005 sg_local = &mpi_request->SGL;
1006 sges_in_segment = ioc->max_sges_in_main_message;
1007 if (sges_left <= sges_in_segment)
1008 goto fill_in_last_segment;
1009
1010 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1011 (sges_in_segment * ioc->sge_size))/4;
1012
1013 /* fill in main message segment when there is a chain following */
1014 while (sges_in_segment) {
1015 if (sges_in_segment == 1)
1016 ioc->base_add_sg_single(sg_local,
1017 sgl_flags_last_element | sg_dma_len(sg_scmd),
1018 sg_dma_address(sg_scmd));
1019 else
1020 ioc->base_add_sg_single(sg_local, sgl_flags |
1021 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1022 sg_scmd = sg_next(sg_scmd);
1023 sg_local += ioc->sge_size;
1024 sges_left--;
1025 sges_in_segment--;
1026 }
1027
1028 /* initializing the chain flags and pointers */
1029 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1030 chain = _scsih_get_chain_buffer(ioc, smid);
1031 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1032 do {
1033 sges_in_segment = (sges_left <=
1034 ioc->max_sges_in_chain_message) ? sges_left :
1035 ioc->max_sges_in_chain_message;
1036 chain_offset = (sges_left == sges_in_segment) ?
1037 0 : (sges_in_segment * ioc->sge_size)/4;
1038 chain_length = sges_in_segment * ioc->sge_size;
1039 if (chain_offset) {
1040 chain_offset = chain_offset <<
1041 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1042 chain_length += ioc->sge_size;
1043 }
1044 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1045 chain_length, chain_dma);
1046 sg_local = chain;
1047 if (!chain_offset)
1048 goto fill_in_last_segment;
1049
1050 /* fill in chain segments */
1051 while (sges_in_segment) {
1052 if (sges_in_segment == 1)
1053 ioc->base_add_sg_single(sg_local,
1054 sgl_flags_last_element |
1055 sg_dma_len(sg_scmd),
1056 sg_dma_address(sg_scmd));
1057 else
1058 ioc->base_add_sg_single(sg_local, sgl_flags |
1059 sg_dma_len(sg_scmd),
1060 sg_dma_address(sg_scmd));
1061 sg_scmd = sg_next(sg_scmd);
1062 sg_local += ioc->sge_size;
1063 sges_left--;
1064 sges_in_segment--;
1065 }
1066
1067 chain_dma += ioc->request_sz;
1068 chain += ioc->request_sz;
1069 } while (1);
1070
1071
1072 fill_in_last_segment:
1073
1074 /* fill the last segment */
1075 while (sges_left) {
1076 if (sges_left == 1)
1077 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1078 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1079 else
1080 ioc->base_add_sg_single(sg_local, sgl_flags |
1081 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1082 sg_scmd = sg_next(sg_scmd);
1083 sg_local += ioc->sge_size;
1084 sges_left--;
1085 }
1086
1087 return 0;
1088}
1089
1090/**
Eric Moored5d135b2009-05-18 13:02:08 -06001091 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001092 * @sdev: scsi device struct
1093 * @qdepth: requested queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001094 * @reason: calling context
Eric Moore635374e2009-03-09 01:21:12 -06001095 *
1096 * Returns queue depth.
1097 */
1098static int
Mike Christiee881a172009-10-15 17:46:39 -07001099_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Eric Moore635374e2009-03-09 01:21:12 -06001100{
1101 struct Scsi_Host *shost = sdev->host;
1102 int max_depth;
1103 int tag_type;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301104 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1105 struct MPT2SAS_DEVICE *sas_device_priv_data;
1106 struct MPT2SAS_TARGET *sas_target_priv_data;
1107 struct _sas_device *sas_device;
1108 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001109
Mike Christiee881a172009-10-15 17:46:39 -07001110 if (reason != SCSI_QDEPTH_DEFAULT)
1111 return -EOPNOTSUPP;
1112
Eric Moore635374e2009-03-09 01:21:12 -06001113 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301114
1115 /* limit max device queue for SATA to 32 */
1116 sas_device_priv_data = sdev->hostdata;
1117 if (!sas_device_priv_data)
1118 goto not_sata;
1119 sas_target_priv_data = sas_device_priv_data->sas_target;
1120 if (!sas_target_priv_data)
1121 goto not_sata;
1122 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1123 goto not_sata;
1124 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1125 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1126 sas_device_priv_data->sas_target->sas_address);
1127 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1128 if (sas_device && sas_device->device_info &
1129 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1130 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1131
1132 not_sata:
1133
Eric Moore635374e2009-03-09 01:21:12 -06001134 if (!sdev->tagged_supported)
1135 max_depth = 1;
1136 if (qdepth > max_depth)
1137 qdepth = max_depth;
1138 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1139 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1140
1141 if (sdev->inquiry_len > 7)
1142 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1143 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1144 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1145 sdev->ordered_tags, sdev->scsi_level,
1146 (sdev->inquiry[7] & 2) >> 1);
1147
1148 return sdev->queue_depth;
1149}
1150
1151/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301152 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001153 * @sdev: scsi device struct
1154 * @tag_type: requested tag type
1155 *
1156 * Returns queue tag type.
1157 */
1158static int
Eric Moored5d135b2009-05-18 13:02:08 -06001159_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001160{
1161 if (sdev->tagged_supported) {
1162 scsi_set_tag_type(sdev, tag_type);
1163 if (tag_type)
1164 scsi_activate_tcq(sdev, sdev->queue_depth);
1165 else
1166 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1167 } else
1168 tag_type = 0;
1169
1170 return tag_type;
1171}
1172
1173/**
Eric Moored5d135b2009-05-18 13:02:08 -06001174 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001175 * @starget: scsi target struct
1176 *
1177 * Returns 0 if ok. Any other return is assumed to be an error and
1178 * the device is ignored.
1179 */
1180static int
Eric Moored5d135b2009-05-18 13:02:08 -06001181_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001182{
1183 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1184 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1185 struct MPT2SAS_TARGET *sas_target_priv_data;
1186 struct _sas_device *sas_device;
1187 struct _raid_device *raid_device;
1188 unsigned long flags;
1189 struct sas_rphy *rphy;
1190
1191 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1192 if (!sas_target_priv_data)
1193 return -ENOMEM;
1194
1195 starget->hostdata = sas_target_priv_data;
1196 sas_target_priv_data->starget = starget;
1197 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1198
1199 /* RAID volumes */
1200 if (starget->channel == RAID_CHANNEL) {
1201 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1202 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1203 starget->channel);
1204 if (raid_device) {
1205 sas_target_priv_data->handle = raid_device->handle;
1206 sas_target_priv_data->sas_address = raid_device->wwid;
1207 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1208 raid_device->starget = starget;
1209 }
1210 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1211 return 0;
1212 }
1213
1214 /* sas/sata devices */
1215 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1216 rphy = dev_to_rphy(starget->dev.parent);
1217 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1218 rphy->identify.sas_address);
1219
1220 if (sas_device) {
1221 sas_target_priv_data->handle = sas_device->handle;
1222 sas_target_priv_data->sas_address = sas_device->sas_address;
1223 sas_device->starget = starget;
1224 sas_device->id = starget->id;
1225 sas_device->channel = starget->channel;
1226 if (sas_device->hidden_raid_component)
1227 sas_target_priv_data->flags |=
1228 MPT_TARGET_FLAGS_RAID_COMPONENT;
1229 }
1230 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1231
1232 return 0;
1233}
1234
1235/**
Eric Moored5d135b2009-05-18 13:02:08 -06001236 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001237 * @starget: scsi target struct
1238 *
1239 * Returns nothing.
1240 */
1241static void
Eric Moored5d135b2009-05-18 13:02:08 -06001242_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001243{
1244 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1245 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1246 struct MPT2SAS_TARGET *sas_target_priv_data;
1247 struct _sas_device *sas_device;
1248 struct _raid_device *raid_device;
1249 unsigned long flags;
1250 struct sas_rphy *rphy;
1251
1252 sas_target_priv_data = starget->hostdata;
1253 if (!sas_target_priv_data)
1254 return;
1255
1256 if (starget->channel == RAID_CHANNEL) {
1257 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1258 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1259 starget->channel);
1260 if (raid_device) {
1261 raid_device->starget = NULL;
1262 raid_device->sdev = NULL;
1263 }
1264 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1265 goto out;
1266 }
1267
1268 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1269 rphy = dev_to_rphy(starget->dev.parent);
1270 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1271 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001272 if (sas_device && (sas_device->starget == starget) &&
1273 (sas_device->id == starget->id) &&
1274 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001275 sas_device->starget = NULL;
1276
1277 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1278
1279 out:
1280 kfree(sas_target_priv_data);
1281 starget->hostdata = NULL;
1282}
1283
1284/**
Eric Moored5d135b2009-05-18 13:02:08 -06001285 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001286 * @sdev: scsi device struct
1287 *
1288 * Returns 0 if ok. Any other return is assumed to be an error and
1289 * the device is ignored.
1290 */
1291static int
Eric Moored5d135b2009-05-18 13:02:08 -06001292_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001293{
1294 struct Scsi_Host *shost;
1295 struct MPT2SAS_ADAPTER *ioc;
1296 struct MPT2SAS_TARGET *sas_target_priv_data;
1297 struct MPT2SAS_DEVICE *sas_device_priv_data;
1298 struct scsi_target *starget;
1299 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001300 unsigned long flags;
1301
1302 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1303 if (!sas_device_priv_data)
1304 return -ENOMEM;
1305
1306 sas_device_priv_data->lun = sdev->lun;
1307 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1308
1309 starget = scsi_target(sdev);
1310 sas_target_priv_data = starget->hostdata;
1311 sas_target_priv_data->num_luns++;
1312 sas_device_priv_data->sas_target = sas_target_priv_data;
1313 sdev->hostdata = sas_device_priv_data;
1314 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1315 sdev->no_uld_attach = 1;
1316
1317 shost = dev_to_shost(&starget->dev);
1318 ioc = shost_priv(shost);
1319 if (starget->channel == RAID_CHANNEL) {
1320 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1321 raid_device = _scsih_raid_device_find_by_id(ioc,
1322 starget->id, starget->channel);
1323 if (raid_device)
1324 raid_device->sdev = sdev; /* raid is single lun */
1325 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001326 }
1327
Eric Moore635374e2009-03-09 01:21:12 -06001328 return 0;
1329}
1330
1331/**
Eric Moored5d135b2009-05-18 13:02:08 -06001332 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001333 * @sdev: scsi device struct
1334 *
1335 * Returns nothing.
1336 */
1337static void
Eric Moored5d135b2009-05-18 13:02:08 -06001338_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001339{
1340 struct MPT2SAS_TARGET *sas_target_priv_data;
1341 struct scsi_target *starget;
1342
1343 if (!sdev->hostdata)
1344 return;
1345
1346 starget = scsi_target(sdev);
1347 sas_target_priv_data = starget->hostdata;
1348 sas_target_priv_data->num_luns--;
1349 kfree(sdev->hostdata);
1350 sdev->hostdata = NULL;
1351}
1352
1353/**
Eric Moored5d135b2009-05-18 13:02:08 -06001354 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001355 * @ioc: per adapter object
1356 * @sas_device: the sas_device object
1357 * @sdev: scsi device struct
1358 */
1359static void
Eric Moored5d135b2009-05-18 13:02:08 -06001360_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001361 struct _sas_device *sas_device, struct scsi_device *sdev)
1362{
1363 Mpi2ConfigReply_t mpi_reply;
1364 Mpi2SasDevicePage0_t sas_device_pg0;
1365 u32 ioc_status;
1366 u16 flags;
1367 u32 device_info;
1368
1369 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1370 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1371 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1372 ioc->name, __FILE__, __LINE__, __func__);
1373 return;
1374 }
1375
1376 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1377 MPI2_IOCSTATUS_MASK;
1378 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1379 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1380 ioc->name, __FILE__, __LINE__, __func__);
1381 return;
1382 }
1383
1384 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301385 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001386
1387 sdev_printk(KERN_INFO, sdev,
1388 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1389 "sw_preserve(%s)\n",
1390 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1391 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1392 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1393 "n",
1394 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1395 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1396 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1397}
1398
1399/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301400 * _scsih_is_raid - return boolean indicating device is raid volume
1401 * @dev the device struct object
1402 */
1403static int
1404_scsih_is_raid(struct device *dev)
1405{
1406 struct scsi_device *sdev = to_scsi_device(dev);
1407
1408 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1409}
1410
1411/**
1412 * _scsih_get_resync - get raid volume resync percent complete
1413 * @dev the device struct object
1414 */
1415static void
1416_scsih_get_resync(struct device *dev)
1417{
1418 struct scsi_device *sdev = to_scsi_device(dev);
1419 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1420 static struct _raid_device *raid_device;
1421 unsigned long flags;
1422 Mpi2RaidVolPage0_t vol_pg0;
1423 Mpi2ConfigReply_t mpi_reply;
1424 u32 volume_status_flags;
1425 u8 percent_complete = 0;
1426
1427 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1428 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1429 sdev->channel);
1430 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1431
1432 if (!raid_device)
1433 goto out;
1434
1435 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1436 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1437 sizeof(Mpi2RaidVolPage0_t))) {
1438 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1439 ioc->name, __FILE__, __LINE__, __func__);
1440 goto out;
1441 }
1442
1443 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1444 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1445 percent_complete = raid_device->percent_complete;
1446 out:
1447 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1448}
1449
1450/**
1451 * _scsih_get_state - get raid volume level
1452 * @dev the device struct object
1453 */
1454static void
1455_scsih_get_state(struct device *dev)
1456{
1457 struct scsi_device *sdev = to_scsi_device(dev);
1458 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1459 static struct _raid_device *raid_device;
1460 unsigned long flags;
1461 Mpi2RaidVolPage0_t vol_pg0;
1462 Mpi2ConfigReply_t mpi_reply;
1463 u32 volstate;
1464 enum raid_state state = RAID_STATE_UNKNOWN;
1465
1466 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1467 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1468 sdev->channel);
1469 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1470
1471 if (!raid_device)
1472 goto out;
1473
1474 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1475 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1476 sizeof(Mpi2RaidVolPage0_t))) {
1477 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1478 ioc->name, __FILE__, __LINE__, __func__);
1479 goto out;
1480 }
1481
1482 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1483 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1484 state = RAID_STATE_RESYNCING;
1485 goto out;
1486 }
1487
1488 switch (vol_pg0.VolumeState) {
1489 case MPI2_RAID_VOL_STATE_OPTIMAL:
1490 case MPI2_RAID_VOL_STATE_ONLINE:
1491 state = RAID_STATE_ACTIVE;
1492 break;
1493 case MPI2_RAID_VOL_STATE_DEGRADED:
1494 state = RAID_STATE_DEGRADED;
1495 break;
1496 case MPI2_RAID_VOL_STATE_FAILED:
1497 case MPI2_RAID_VOL_STATE_MISSING:
1498 state = RAID_STATE_OFFLINE;
1499 break;
1500 }
1501 out:
1502 raid_set_state(mpt2sas_raid_template, dev, state);
1503}
1504
1505/**
1506 * _scsih_set_level - set raid level
1507 * @sdev: scsi device struct
1508 * @raid_device: raid_device object
1509 */
1510static void
1511_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1512{
1513 enum raid_level level = RAID_LEVEL_UNKNOWN;
1514
1515 switch (raid_device->volume_type) {
1516 case MPI2_RAID_VOL_TYPE_RAID0:
1517 level = RAID_LEVEL_0;
1518 break;
1519 case MPI2_RAID_VOL_TYPE_RAID10:
1520 level = RAID_LEVEL_10;
1521 break;
1522 case MPI2_RAID_VOL_TYPE_RAID1E:
1523 level = RAID_LEVEL_1E;
1524 break;
1525 case MPI2_RAID_VOL_TYPE_RAID1:
1526 level = RAID_LEVEL_1;
1527 break;
1528 }
1529
1530 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1531}
1532
1533/**
Eric Moore635374e2009-03-09 01:21:12 -06001534 * _scsih_get_volume_capabilities - volume capabilities
1535 * @ioc: per adapter object
1536 * @sas_device: the raid_device object
1537 */
1538static void
1539_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1540 struct _raid_device *raid_device)
1541{
1542 Mpi2RaidVolPage0_t *vol_pg0;
1543 Mpi2RaidPhysDiskPage0_t pd_pg0;
1544 Mpi2SasDevicePage0_t sas_device_pg0;
1545 Mpi2ConfigReply_t mpi_reply;
1546 u16 sz;
1547 u8 num_pds;
1548
1549 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1550 &num_pds)) || !num_pds) {
1551 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1552 ioc->name, __FILE__, __LINE__, __func__);
1553 return;
1554 }
1555
1556 raid_device->num_pds = num_pds;
1557 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1558 sizeof(Mpi2RaidVol0PhysDisk_t));
1559 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1560 if (!vol_pg0) {
1561 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1562 ioc->name, __FILE__, __LINE__, __func__);
1563 return;
1564 }
1565
1566 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1567 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1568 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1569 ioc->name, __FILE__, __LINE__, __func__);
1570 kfree(vol_pg0);
1571 return;
1572 }
1573
1574 raid_device->volume_type = vol_pg0->VolumeType;
1575
1576 /* figure out what the underlying devices are by
1577 * obtaining the device_info bits for the 1st device
1578 */
1579 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1580 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1581 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1582 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1583 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1584 le16_to_cpu(pd_pg0.DevHandle)))) {
1585 raid_device->device_info =
1586 le32_to_cpu(sas_device_pg0.DeviceInfo);
1587 }
1588 }
1589
1590 kfree(vol_pg0);
1591}
1592
1593/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301594 * _scsih_enable_tlr - setting TLR flags
1595 * @ioc: per adapter object
1596 * @sdev: scsi device struct
1597 *
1598 * Enabling Transaction Layer Retries for tape devices when
1599 * vpd page 0x90 is present
1600 *
1601 */
1602static void
1603_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1604{
1605 /* only for TAPE */
1606 if (sdev->type != TYPE_TAPE)
1607 return;
1608
1609 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1610 return;
1611
1612 sas_enable_tlr(sdev);
1613 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1614 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1615 return;
1616
1617}
1618
1619/**
Eric Moored5d135b2009-05-18 13:02:08 -06001620 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001621 * @sdev: scsi device struct
1622 *
1623 * Returns 0 if ok. Any other return is assumed to be an error and
1624 * the device is ignored.
1625 */
1626static int
Eric Moored5d135b2009-05-18 13:02:08 -06001627_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001628{
1629 struct Scsi_Host *shost = sdev->host;
1630 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1631 struct MPT2SAS_DEVICE *sas_device_priv_data;
1632 struct MPT2SAS_TARGET *sas_target_priv_data;
1633 struct _sas_device *sas_device;
1634 struct _raid_device *raid_device;
1635 unsigned long flags;
1636 int qdepth;
1637 u8 ssp_target = 0;
1638 char *ds = "";
1639 char *r_level = "";
1640
1641 qdepth = 1;
1642 sas_device_priv_data = sdev->hostdata;
1643 sas_device_priv_data->configured_lun = 1;
1644 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1645 sas_target_priv_data = sas_device_priv_data->sas_target;
1646
1647 /* raid volume handling */
1648 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1649
1650 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1651 raid_device = _scsih_raid_device_find_by_handle(ioc,
1652 sas_target_priv_data->handle);
1653 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1654 if (!raid_device) {
1655 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1656 ioc->name, __FILE__, __LINE__, __func__);
1657 return 0;
1658 }
1659
1660 _scsih_get_volume_capabilities(ioc, raid_device);
1661
1662 /* RAID Queue Depth Support
1663 * IS volume = underlying qdepth of drive type, either
1664 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1665 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1666 */
1667 if (raid_device->device_info &
1668 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1669 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1670 ds = "SSP";
1671 } else {
1672 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1673 if (raid_device->device_info &
1674 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1675 ds = "SATA";
1676 else
1677 ds = "STP";
1678 }
1679
1680 switch (raid_device->volume_type) {
1681 case MPI2_RAID_VOL_TYPE_RAID0:
1682 r_level = "RAID0";
1683 break;
1684 case MPI2_RAID_VOL_TYPE_RAID1E:
1685 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301686 if (ioc->manu_pg10.OEMIdentifier &&
1687 (ioc->manu_pg10.GenericFlags0 &
1688 MFG10_GF0_R10_DISPLAY) &&
1689 !(raid_device->num_pds % 2))
1690 r_level = "RAID10";
1691 else
1692 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001693 break;
1694 case MPI2_RAID_VOL_TYPE_RAID1:
1695 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1696 r_level = "RAID1";
1697 break;
1698 case MPI2_RAID_VOL_TYPE_RAID10:
1699 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1700 r_level = "RAID10";
1701 break;
1702 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1703 default:
1704 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1705 r_level = "RAIDX";
1706 break;
1707 }
1708
1709 sdev_printk(KERN_INFO, sdev, "%s: "
1710 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1711 r_level, raid_device->handle,
1712 (unsigned long long)raid_device->wwid,
1713 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001714 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301715 /* raid transport support */
1716 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001717 return 0;
1718 }
1719
1720 /* non-raid handling */
1721 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1722 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1723 sas_device_priv_data->sas_target->sas_address);
1724 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1725 if (sas_device) {
1726 if (sas_target_priv_data->flags &
1727 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1728 mpt2sas_config_get_volume_handle(ioc,
1729 sas_device->handle, &sas_device->volume_handle);
1730 mpt2sas_config_get_volume_wwid(ioc,
1731 sas_device->volume_handle,
1732 &sas_device->volume_wwid);
1733 }
1734 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1735 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1736 ssp_target = 1;
1737 ds = "SSP";
1738 } else {
1739 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1740 if (sas_device->device_info &
1741 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1742 ds = "STP";
1743 else if (sas_device->device_info &
1744 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1745 ds = "SATA";
1746 }
1747
1748 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1749 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1750 ds, sas_device->handle,
1751 (unsigned long long)sas_device->sas_address,
1752 (unsigned long long)sas_device->device_name);
1753 sdev_printk(KERN_INFO, sdev, "%s: "
1754 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1755 (unsigned long long) sas_device->enclosure_logical_id,
1756 sas_device->slot);
1757
1758 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001759 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001760 }
1761
Mike Christiee881a172009-10-15 17:46:39 -07001762 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001763
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301764 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001765 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301766 _scsih_enable_tlr(ioc, sdev);
1767 }
Eric Moore635374e2009-03-09 01:21:12 -06001768 return 0;
1769}
1770
1771/**
Eric Moored5d135b2009-05-18 13:02:08 -06001772 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001773 * @sdev: scsi device struct
1774 * @bdev: pointer to block device context
1775 * @capacity: device size (in 512 byte sectors)
1776 * @params: three element array to place output:
1777 * params[0] number of heads (max 255)
1778 * params[1] number of sectors (max 63)
1779 * params[2] number of cylinders
1780 *
1781 * Return nothing.
1782 */
1783static int
Eric Moored5d135b2009-05-18 13:02:08 -06001784_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001785 sector_t capacity, int params[])
1786{
1787 int heads;
1788 int sectors;
1789 sector_t cylinders;
1790 ulong dummy;
1791
1792 heads = 64;
1793 sectors = 32;
1794
1795 dummy = heads * sectors;
1796 cylinders = capacity;
1797 sector_div(cylinders, dummy);
1798
1799 /*
1800 * Handle extended translation size for logical drives
1801 * > 1Gb
1802 */
1803 if ((ulong)capacity >= 0x200000) {
1804 heads = 255;
1805 sectors = 63;
1806 dummy = heads * sectors;
1807 cylinders = capacity;
1808 sector_div(cylinders, dummy);
1809 }
1810
1811 /* return result */
1812 params[0] = heads;
1813 params[1] = sectors;
1814 params[2] = cylinders;
1815
1816 return 0;
1817}
1818
1819/**
1820 * _scsih_response_code - translation of device response code
1821 * @ioc: per adapter object
1822 * @response_code: response code returned by the device
1823 *
1824 * Return nothing.
1825 */
1826static void
1827_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1828{
1829 char *desc;
1830
1831 switch (response_code) {
1832 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1833 desc = "task management request completed";
1834 break;
1835 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1836 desc = "invalid frame";
1837 break;
1838 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1839 desc = "task management request not supported";
1840 break;
1841 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1842 desc = "task management request failed";
1843 break;
1844 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1845 desc = "task management request succeeded";
1846 break;
1847 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1848 desc = "invalid lun";
1849 break;
1850 case 0xA:
1851 desc = "overlapped tag attempted";
1852 break;
1853 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1854 desc = "task queued, however not sent to target";
1855 break;
1856 default:
1857 desc = "unknown";
1858 break;
1859 }
1860 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1861 ioc->name, response_code, desc);
1862}
1863
1864/**
Eric Moored5d135b2009-05-18 13:02:08 -06001865 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001866 * @ioc: per adapter object
1867 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301868 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001869 * @reply: reply message frame(lower 32bit addr)
1870 * Context: none.
1871 *
1872 * The callback handler when using scsih_issue_tm.
1873 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301874 * Return 1 meaning mf should be freed from _base_interrupt
1875 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001876 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301877static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301878_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001879{
1880 MPI2DefaultReply_t *mpi_reply;
1881
1882 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301883 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001884 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301885 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001886 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1887 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1888 if (mpi_reply) {
1889 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1890 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1891 }
1892 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1893 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301894 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001895}
1896
1897/**
1898 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1899 * @ioc: per adapter object
1900 * @handle: device handle
1901 *
1902 * During taskmangement request, we need to freeze the device queue.
1903 */
1904void
1905mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1906{
1907 struct MPT2SAS_DEVICE *sas_device_priv_data;
1908 struct scsi_device *sdev;
1909 u8 skip = 0;
1910
1911 shost_for_each_device(sdev, ioc->shost) {
1912 if (skip)
1913 continue;
1914 sas_device_priv_data = sdev->hostdata;
1915 if (!sas_device_priv_data)
1916 continue;
1917 if (sas_device_priv_data->sas_target->handle == handle) {
1918 sas_device_priv_data->sas_target->tm_busy = 1;
1919 skip = 1;
1920 ioc->ignore_loginfos = 1;
1921 }
1922 }
1923}
1924
1925/**
1926 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1927 * @ioc: per adapter object
1928 * @handle: device handle
1929 *
1930 * During taskmangement request, we need to freeze the device queue.
1931 */
1932void
1933mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1934{
1935 struct MPT2SAS_DEVICE *sas_device_priv_data;
1936 struct scsi_device *sdev;
1937 u8 skip = 0;
1938
1939 shost_for_each_device(sdev, ioc->shost) {
1940 if (skip)
1941 continue;
1942 sas_device_priv_data = sdev->hostdata;
1943 if (!sas_device_priv_data)
1944 continue;
1945 if (sas_device_priv_data->sas_target->handle == handle) {
1946 sas_device_priv_data->sas_target->tm_busy = 0;
1947 skip = 1;
1948 ioc->ignore_loginfos = 0;
1949 }
1950 }
1951}
1952
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301953
Eric Moore635374e2009-03-09 01:21:12 -06001954/**
1955 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1956 * @ioc: per adapter struct
1957 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301958 * @channel: the channel assigned by the OS
1959 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001960 * @lun: lun number
1961 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1962 * @smid_task: smid assigned to the task
1963 * @timeout: timeout in seconds
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301964 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06001965 *
1966 * A generic API for sending task management requests to firmware.
1967 *
Eric Moore635374e2009-03-09 01:21:12 -06001968 * The callback index is set inside `ioc->tm_cb_idx`.
1969 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301970 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06001971 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301972int
1973mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
1974 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
1975 struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001976{
1977 Mpi2SCSITaskManagementRequest_t *mpi_request;
1978 Mpi2SCSITaskManagementReply_t *mpi_reply;
1979 u16 smid = 0;
1980 u32 ioc_state;
1981 unsigned long timeleft;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301982 struct scsi_cmnd *scmd_lookup;
1983 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001984
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301985 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301986 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1987 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1988 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301989 rc = FAILED;
1990 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301991 }
1992
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05301993 if (ioc->shost_recovery || ioc->remove_host) {
Eric Moore635374e2009-03-09 01:21:12 -06001994 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1995 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301996 rc = FAILED;
1997 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06001998 }
Eric Moore635374e2009-03-09 01:21:12 -06001999
2000 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2001 if (ioc_state & MPI2_DOORBELL_USED) {
2002 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
2003 "active!\n", ioc->name));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302004 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2005 FORCE_BIG_HAMMER);
2006 rc = SUCCESS;
2007 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002008 }
2009
2010 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2011 mpt2sas_base_fault_info(ioc, ioc_state &
2012 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302013 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2014 FORCE_BIG_HAMMER);
2015 rc = SUCCESS;
2016 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002017 }
2018
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302019 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002020 if (!smid) {
2021 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2022 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302023 rc = FAILED;
2024 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002025 }
2026
2027 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302028 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2029 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002030 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2031 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2032 ioc->tm_cmds.smid = smid;
2033 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2034 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2035 mpi_request->DevHandle = cpu_to_le16(handle);
2036 mpi_request->TaskType = type;
2037 mpi_request->TaskMID = cpu_to_le16(smid_task);
2038 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2039 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302040 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302041 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002042 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002043 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2044 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2045 ioc->name, __func__);
2046 _debug_dump_mf(mpi_request,
2047 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302048 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2049 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2050 FORCE_BIG_HAMMER);
2051 rc = SUCCESS;
2052 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2053 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2054 goto err_out;
2055 }
Eric Moore635374e2009-03-09 01:21:12 -06002056 }
2057
2058 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2059 mpi_reply = ioc->tm_cmds.reply;
2060 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2061 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2062 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2063 le32_to_cpu(mpi_reply->IOCLogInfo),
2064 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302065 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002066 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302067 if (mpi_reply->IOCStatus)
2068 _debug_dump_mf(mpi_request,
2069 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2070 }
Eric Moore635374e2009-03-09 01:21:12 -06002071 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302072
2073 /* sanity check:
2074 * Check to see the commands were terminated.
2075 * This is only needed for eh callbacks, hence the scmd check.
2076 */
2077 rc = FAILED;
2078 if (scmd == NULL)
2079 goto bypass_sanity_checks;
2080 switch (type) {
2081 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2082 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2083 if (scmd_lookup && (scmd_lookup->serial_number ==
2084 scmd->serial_number))
2085 rc = FAILED;
2086 else
2087 rc = SUCCESS;
2088 break;
2089
2090 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2091 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2092 rc = FAILED;
2093 else
2094 rc = SUCCESS;
2095 break;
2096
2097 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2098 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2099 rc = FAILED;
2100 else
2101 rc = SUCCESS;
2102 break;
2103 }
2104
2105 bypass_sanity_checks:
2106
2107 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2108 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2109 mutex_unlock(&ioc->tm_cmds.mutex);
2110
2111 return rc;
2112
2113 err_out:
2114 mutex_unlock(&ioc->tm_cmds.mutex);
2115 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002116}
2117
2118/**
Eric Moored5d135b2009-05-18 13:02:08 -06002119 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06002120 * @sdev: scsi device struct
2121 *
2122 * Returns SUCCESS if command aborted else FAILED
2123 */
2124static int
Eric Moored5d135b2009-05-18 13:02:08 -06002125_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002126{
2127 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2128 struct MPT2SAS_DEVICE *sas_device_priv_data;
2129 u16 smid;
2130 u16 handle;
2131 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002132
2133 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
2134 ioc->name, scmd);
2135 scsi_print_command(scmd);
2136
2137 sas_device_priv_data = scmd->device->hostdata;
2138 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2139 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2140 ioc->name, scmd);
2141 scmd->result = DID_NO_CONNECT << 16;
2142 scmd->scsi_done(scmd);
2143 r = SUCCESS;
2144 goto out;
2145 }
2146
2147 /* search for the command */
2148 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2149 if (!smid) {
2150 scmd->result = DID_RESET << 16;
2151 r = SUCCESS;
2152 goto out;
2153 }
2154
2155 /* for hidden raid components and volumes this is not supported */
2156 if (sas_device_priv_data->sas_target->flags &
2157 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2158 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2159 scmd->result = DID_RESET << 16;
2160 r = FAILED;
2161 goto out;
2162 }
2163
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302164 mpt2sas_halt_firmware(ioc);
2165
Eric Moore635374e2009-03-09 01:21:12 -06002166 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302167 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2168 scmd->device->id, scmd->device->lun,
2169 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002170
2171 out:
2172 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
2173 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2174 return r;
2175}
2176
Eric Moore635374e2009-03-09 01:21:12 -06002177/**
Eric Moored5d135b2009-05-18 13:02:08 -06002178 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002179 * @sdev: scsi device struct
2180 *
2181 * Returns SUCCESS if command aborted else FAILED
2182 */
2183static int
Eric Moored5d135b2009-05-18 13:02:08 -06002184_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002185{
2186 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2187 struct MPT2SAS_DEVICE *sas_device_priv_data;
2188 struct _sas_device *sas_device;
2189 unsigned long flags;
2190 u16 handle;
2191 int r;
2192
Eric Moore993e0da2009-05-18 13:00:45 -06002193 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002194 ioc->name, scmd);
2195 scsi_print_command(scmd);
2196
2197 sas_device_priv_data = scmd->device->hostdata;
2198 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2199 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2200 ioc->name, scmd);
2201 scmd->result = DID_NO_CONNECT << 16;
2202 scmd->scsi_done(scmd);
2203 r = SUCCESS;
2204 goto out;
2205 }
2206
2207 /* for hidden raid components obtain the volume_handle */
2208 handle = 0;
2209 if (sas_device_priv_data->sas_target->flags &
2210 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2211 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2212 sas_device = _scsih_sas_device_find_by_handle(ioc,
2213 sas_device_priv_data->sas_target->handle);
2214 if (sas_device)
2215 handle = sas_device->volume_handle;
2216 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2217 } else
2218 handle = sas_device_priv_data->sas_target->handle;
2219
2220 if (!handle) {
2221 scmd->result = DID_RESET << 16;
2222 r = FAILED;
2223 goto out;
2224 }
2225
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302226 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2227 scmd->device->id, scmd->device->lun,
2228 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002229
2230 out:
2231 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2232 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2233 return r;
2234}
2235
2236/**
Eric Moored5d135b2009-05-18 13:02:08 -06002237 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002238 * @sdev: scsi device struct
2239 *
2240 * Returns SUCCESS if command aborted else FAILED
2241 */
2242static int
Eric Moored5d135b2009-05-18 13:02:08 -06002243_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002244{
2245 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2246 struct MPT2SAS_DEVICE *sas_device_priv_data;
2247 struct _sas_device *sas_device;
2248 unsigned long flags;
2249 u16 handle;
2250 int r;
2251
2252 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2253 ioc->name, scmd);
2254 scsi_print_command(scmd);
2255
2256 sas_device_priv_data = scmd->device->hostdata;
2257 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2258 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2259 ioc->name, scmd);
2260 scmd->result = DID_NO_CONNECT << 16;
2261 scmd->scsi_done(scmd);
2262 r = SUCCESS;
2263 goto out;
2264 }
2265
2266 /* for hidden raid components obtain the volume_handle */
2267 handle = 0;
2268 if (sas_device_priv_data->sas_target->flags &
2269 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2270 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2271 sas_device = _scsih_sas_device_find_by_handle(ioc,
2272 sas_device_priv_data->sas_target->handle);
2273 if (sas_device)
2274 handle = sas_device->volume_handle;
2275 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2276 } else
2277 handle = sas_device_priv_data->sas_target->handle;
2278
2279 if (!handle) {
2280 scmd->result = DID_RESET << 16;
2281 r = FAILED;
2282 goto out;
2283 }
2284
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302285 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2286 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2287 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002288
2289 out:
2290 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2291 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2292 return r;
2293}
2294
2295/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302296 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002297 * @sdev: scsi device struct
2298 *
2299 * Returns SUCCESS if command aborted else FAILED
2300 */
2301static int
Eric Moored5d135b2009-05-18 13:02:08 -06002302_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002303{
2304 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2305 int r, retval;
2306
2307 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2308 ioc->name, scmd);
2309 scsi_print_command(scmd);
2310
2311 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2312 FORCE_BIG_HAMMER);
2313 r = (retval < 0) ? FAILED : SUCCESS;
2314 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2315 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2316
2317 return r;
2318}
2319
2320/**
2321 * _scsih_fw_event_add - insert and queue up fw_event
2322 * @ioc: per adapter object
2323 * @fw_event: object describing the event
2324 * Context: This function will acquire ioc->fw_event_lock.
2325 *
2326 * This adds the firmware event object into link list, then queues it up to
2327 * be processed from user context.
2328 *
2329 * Return nothing.
2330 */
2331static void
2332_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2333{
2334 unsigned long flags;
2335
2336 if (ioc->firmware_event_thread == NULL)
2337 return;
2338
2339 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2340 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302341 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2342 queue_delayed_work(ioc->firmware_event_thread,
2343 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002344 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2345}
2346
2347/**
2348 * _scsih_fw_event_free - delete fw_event
2349 * @ioc: per adapter object
2350 * @fw_event: object describing the event
2351 * Context: This function will acquire ioc->fw_event_lock.
2352 *
2353 * This removes firmware event object from link list, frees associated memory.
2354 *
2355 * Return nothing.
2356 */
2357static void
2358_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2359 *fw_event)
2360{
2361 unsigned long flags;
2362
2363 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2364 list_del(&fw_event->list);
2365 kfree(fw_event->event_data);
2366 kfree(fw_event);
2367 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2368}
2369
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302370
Eric Moore635374e2009-03-09 01:21:12 -06002371/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302372 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002373 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002374 *
2375 * Return nothing.
2376 */
2377static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302378_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002379{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302380 struct fw_event_work *fw_event;
2381
2382 if (ioc->wait_for_port_enable_to_complete)
2383 return;
2384 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2385 if (!fw_event)
2386 return;
2387 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2388 fw_event->ioc = ioc;
2389 _scsih_fw_event_add(ioc, fw_event);
2390}
2391
2392/**
2393 * _scsih_fw_event_cleanup_queue - cleanup event queue
2394 * @ioc: per adapter object
2395 *
2396 * Walk the firmware event queue, either killing timers, or waiting
2397 * for outstanding events to complete
2398 *
2399 * Return nothing.
2400 */
2401static void
2402_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2403{
2404 struct fw_event_work *fw_event, *next;
2405
2406 if (list_empty(&ioc->fw_event_list) ||
2407 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002408 return;
2409
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302410 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2411 if (cancel_delayed_work(&fw_event->delayed_work)) {
2412 _scsih_fw_event_free(ioc, fw_event);
2413 continue;
2414 }
2415 fw_event->cancel_pending_work = 1;
2416 }
Eric Moore635374e2009-03-09 01:21:12 -06002417}
2418
Eric Moore635374e2009-03-09 01:21:12 -06002419/**
2420 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2421 * @ioc: per adapter object
2422 * @handle: device handle
2423 *
2424 * During device pull we need to appropiately set the sdev state.
2425 */
2426static void
2427_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2428{
2429 struct MPT2SAS_DEVICE *sas_device_priv_data;
2430 struct scsi_device *sdev;
2431
2432 shost_for_each_device(sdev, ioc->shost) {
2433 sas_device_priv_data = sdev->hostdata;
2434 if (!sas_device_priv_data)
2435 continue;
2436 if (!sas_device_priv_data->block)
2437 continue;
2438 if (sas_device_priv_data->sas_target->handle == handle) {
2439 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2440 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2441 "handle(0x%04x)\n", ioc->name, handle));
2442 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302443 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002444 }
2445 }
2446}
2447
2448/**
2449 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2450 * @ioc: per adapter object
2451 * @handle: device handle
2452 *
2453 * During device pull we need to appropiately set the sdev state.
2454 */
2455static void
2456_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2457{
2458 struct MPT2SAS_DEVICE *sas_device_priv_data;
2459 struct scsi_device *sdev;
2460
2461 shost_for_each_device(sdev, ioc->shost) {
2462 sas_device_priv_data = sdev->hostdata;
2463 if (!sas_device_priv_data)
2464 continue;
2465 if (sas_device_priv_data->block)
2466 continue;
2467 if (sas_device_priv_data->sas_target->handle == handle) {
2468 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2469 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2470 "handle(0x%04x)\n", ioc->name, handle));
2471 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302472 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002473 }
2474 }
2475}
2476
2477/**
2478 * _scsih_block_io_to_children_attached_to_ex
2479 * @ioc: per adapter object
2480 * @sas_expander: the sas_device object
2481 *
2482 * This routine set sdev state to SDEV_BLOCK for all devices
2483 * attached to this expander. This function called when expander is
2484 * pulled.
2485 */
2486static void
2487_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2488 struct _sas_node *sas_expander)
2489{
2490 struct _sas_port *mpt2sas_port;
2491 struct _sas_device *sas_device;
2492 struct _sas_node *expander_sibling;
2493 unsigned long flags;
2494
2495 if (!sas_expander)
2496 return;
2497
2498 list_for_each_entry(mpt2sas_port,
2499 &sas_expander->sas_port_list, port_list) {
2500 if (mpt2sas_port->remote_identify.device_type ==
2501 SAS_END_DEVICE) {
2502 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2503 sas_device =
2504 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2505 mpt2sas_port->remote_identify.sas_address);
2506 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2507 if (!sas_device)
2508 continue;
2509 _scsih_block_io_device(ioc, sas_device->handle);
2510 }
2511 }
2512
2513 list_for_each_entry(mpt2sas_port,
2514 &sas_expander->sas_port_list, port_list) {
2515
2516 if (mpt2sas_port->remote_identify.device_type ==
2517 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2518 mpt2sas_port->remote_identify.device_type ==
2519 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2520
2521 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2522 expander_sibling =
2523 mpt2sas_scsih_expander_find_by_sas_address(
2524 ioc, mpt2sas_port->remote_identify.sas_address);
2525 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2526 _scsih_block_io_to_children_attached_to_ex(ioc,
2527 expander_sibling);
2528 }
2529 }
2530}
2531
2532/**
2533 * _scsih_block_io_to_children_attached_directly
2534 * @ioc: per adapter object
2535 * @event_data: topology change event data
2536 *
2537 * This routine set sdev state to SDEV_BLOCK for all devices
2538 * direct attached during device pull.
2539 */
2540static void
2541_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2542 Mpi2EventDataSasTopologyChangeList_t *event_data)
2543{
2544 int i;
2545 u16 handle;
2546 u16 reason_code;
2547 u8 phy_number;
2548
2549 for (i = 0; i < event_data->NumEntries; i++) {
2550 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2551 if (!handle)
2552 continue;
2553 phy_number = event_data->StartPhyNum + i;
2554 reason_code = event_data->PHY[i].PhyStatus &
2555 MPI2_EVENT_SAS_TOPO_RC_MASK;
2556 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2557 _scsih_block_io_device(ioc, handle);
2558 }
2559}
2560
2561/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302562 * _scsih_tm_tr_send - send task management request
2563 * @ioc: per adapter object
2564 * @handle: device handle
2565 * Context: interrupt time.
2566 *
2567 * This code is to initiate the device removal handshake protocal
2568 * with controller firmware. This function will issue target reset
2569 * using high priority request queue. It will send a sas iounit
2570 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2571 *
2572 * This is designed to send muliple task management request at the same
2573 * time to the fifo. If the fifo is full, we will append the request,
2574 * and process it in a future completion.
2575 */
2576static void
2577_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2578{
2579 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302580 u16 smid;
2581 struct _sas_device *sas_device;
2582 unsigned long flags;
2583 struct _tr_list *delayed_tr;
2584
Kashyap, Desai1278b112010-03-09 17:34:13 +05302585 if (ioc->shost_recovery || ioc->remove_host) {
2586 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2587 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302588 return;
2589 }
2590
2591 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2592 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302593 if (sas_device && sas_device->hidden_raid_component) {
2594 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302595 return;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302596 }
2597 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302598
2599 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2600 if (!smid) {
2601 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2602 if (!delayed_tr)
2603 return;
2604 INIT_LIST_HEAD(&delayed_tr->list);
2605 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302606 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2607 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2608 "DELAYED:tr:handle(0x%04x), (open)\n",
2609 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302610 return;
2611 }
2612
Kashyap, Desai1278b112010-03-09 17:34:13 +05302613 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2614 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2615 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302616 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2617 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2618 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2619 mpi_request->DevHandle = cpu_to_le16(handle);
2620 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302621 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2622}
2623
2624
2625
2626/**
2627 * _scsih_sas_control_complete - completion routine
2628 * @ioc: per adapter object
2629 * @smid: system request message index
2630 * @msix_index: MSIX table index supplied by the OS
2631 * @reply: reply message frame(lower 32bit addr)
2632 * Context: interrupt time.
2633 *
2634 * This is the sas iounit controll completion routine.
2635 * This code is part of the code to initiate the device removal
2636 * handshake protocal with controller firmware.
2637 *
2638 * Return 1 meaning mf should be freed from _base_interrupt
2639 * 0 means the mf is freed from this function.
2640 */
2641static u8
2642_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2643 u8 msix_index, u32 reply)
2644{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302645 Mpi2SasIoUnitControlReply_t *mpi_reply =
2646 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2647
Kashyap, Desai1278b112010-03-09 17:34:13 +05302648 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2649 "sc_complete:handle(0x%04x), (open) "
2650 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2651 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2652 le16_to_cpu(mpi_reply->IOCStatus),
2653 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302654 return 1;
2655}
2656
2657/**
2658 * _scsih_tm_tr_complete -
2659 * @ioc: per adapter object
2660 * @smid: system request message index
2661 * @msix_index: MSIX table index supplied by the OS
2662 * @reply: reply message frame(lower 32bit addr)
2663 * Context: interrupt time.
2664 *
2665 * This is the target reset completion routine.
2666 * This code is part of the code to initiate the device removal
2667 * handshake protocal with controller firmware.
2668 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2669 *
2670 * Return 1 meaning mf should be freed from _base_interrupt
2671 * 0 means the mf is freed from this function.
2672 */
2673static u8
2674_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2675 u32 reply)
2676{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302677 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302678 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302679 Mpi2SCSITaskManagementReply_t *mpi_reply =
2680 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2681 Mpi2SasIoUnitControlRequest_t *mpi_request;
2682 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302683 struct _tr_list *delayed_tr;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302684
Kashyap, Desai1278b112010-03-09 17:34:13 +05302685 if (ioc->shost_recovery || ioc->remove_host) {
2686 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2687 "progress!\n", __func__, ioc->name));
2688 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302689 }
2690
Kashyap, Desai1278b112010-03-09 17:34:13 +05302691 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2692 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2693 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2694 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2695 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2696 le16_to_cpu(mpi_reply->DevHandle), smid));
2697 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302698 }
2699
Kashyap, Desai1278b112010-03-09 17:34:13 +05302700 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2701 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2702 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2703 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2704 le32_to_cpu(mpi_reply->IOCLogInfo),
2705 le32_to_cpu(mpi_reply->TerminationCount)));
2706
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302707 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2708 if (!smid_sas_ctrl) {
2709 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2710 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302711 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302712 }
2713
Kashyap, Desai1278b112010-03-09 17:34:13 +05302714 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2715 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2716 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302717 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2718 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2719 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2720 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302721 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302722 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302723
2724 if (!list_empty(&ioc->delayed_tr_list)) {
2725 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2726 struct _tr_list, list);
2727 mpt2sas_base_free_smid(ioc, smid);
2728 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2729 list_del(&delayed_tr->list);
2730 kfree(delayed_tr);
2731 return 0; /* tells base_interrupt not to free mf */
2732 }
2733 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302734}
2735
2736/**
Eric Moore635374e2009-03-09 01:21:12 -06002737 * _scsih_check_topo_delete_events - sanity check on topo events
2738 * @ioc: per adapter object
2739 * @event_data: the event data payload
2740 *
2741 * This routine added to better handle cable breaker.
2742 *
2743 * This handles the case where driver recieves multiple expander
2744 * add and delete events in a single shot. When there is a delete event
2745 * the routine will void any pending add events waiting in the event queue.
2746 *
2747 * Return nothing.
2748 */
2749static void
2750_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2751 Mpi2EventDataSasTopologyChangeList_t *event_data)
2752{
2753 struct fw_event_work *fw_event;
2754 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2755 u16 expander_handle;
2756 struct _sas_node *sas_expander;
2757 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302758 int i, reason_code;
2759 u16 handle;
2760
2761 for (i = 0 ; i < event_data->NumEntries; i++) {
2762 if (event_data->PHY[i].PhyStatus &
2763 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2764 continue;
2765 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2766 if (!handle)
2767 continue;
2768 reason_code = event_data->PHY[i].PhyStatus &
2769 MPI2_EVENT_SAS_TOPO_RC_MASK;
2770 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2771 _scsih_tm_tr_send(ioc, handle);
2772 }
Eric Moore635374e2009-03-09 01:21:12 -06002773
2774 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2775 if (expander_handle < ioc->sas_hba.num_phys) {
2776 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2777 return;
2778 }
2779
2780 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2781 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2782 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2783 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2784 expander_handle);
2785 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2786 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2787 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2788 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2789
2790 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2791 return;
2792
2793 /* mark ignore flag for pending events */
2794 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2795 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2796 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2797 fw_event->ignore)
2798 continue;
2799 local_event_data = fw_event->event_data;
2800 if (local_event_data->ExpStatus ==
2801 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2802 local_event_data->ExpStatus ==
2803 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2804 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2805 expander_handle) {
2806 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2807 "setting ignoring flag\n", ioc->name));
2808 fw_event->ignore = 1;
2809 }
2810 }
2811 }
2812 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2813}
2814
2815/**
Eric Moore635374e2009-03-09 01:21:12 -06002816 * _scsih_flush_running_cmds - completing outstanding commands.
2817 * @ioc: per adapter object
2818 *
2819 * The flushing out of all pending scmd commands following host reset,
2820 * where all IO is dropped to the floor.
2821 *
2822 * Return nothing.
2823 */
2824static void
2825_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2826{
2827 struct scsi_cmnd *scmd;
2828 u16 smid;
2829 u16 count = 0;
2830
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302831 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2832 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002833 if (!scmd)
2834 continue;
2835 count++;
2836 mpt2sas_base_free_smid(ioc, smid);
2837 scsi_dma_unmap(scmd);
2838 scmd->result = DID_RESET << 16;
2839 scmd->scsi_done(scmd);
2840 }
2841 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2842 ioc->name, count));
2843}
2844
2845/**
Eric Moore3c621b32009-05-18 12:59:41 -06002846 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2847 * @scmd: pointer to scsi command object
2848 * @mpi_request: pointer to the SCSI_IO reqest message frame
2849 *
2850 * Supporting protection 1 and 3.
2851 *
2852 * Returns nothing
2853 */
2854static void
2855_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2856{
2857 u16 eedp_flags;
2858 unsigned char prot_op = scsi_get_prot_op(scmd);
2859 unsigned char prot_type = scsi_get_prot_type(scmd);
2860
Eric Moored334aa72010-04-22 10:47:40 -06002861 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06002862 return;
2863
2864 if (prot_op == SCSI_PROT_READ_STRIP)
2865 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2866 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2867 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2868 else
2869 return;
2870
Eric Moore3c621b32009-05-18 12:59:41 -06002871 switch (prot_type) {
2872 case SCSI_PROT_DIF_TYPE1:
2873
2874 /*
2875 * enable ref/guard checking
2876 * auto increment ref tag
2877 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302878 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06002879 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2880 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2881 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2882 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06002883 break;
Eric Moore3c621b32009-05-18 12:59:41 -06002884
Eric Moored334aa72010-04-22 10:47:40 -06002885 case SCSI_PROT_DIF_TYPE2:
2886
2887 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2888 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2889 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06002890 break;
2891
2892 case SCSI_PROT_DIF_TYPE3:
2893
2894 /*
2895 * enable guard checking
2896 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302897 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06002898 break;
2899 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05302900 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
2901 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06002902}
2903
2904/**
2905 * _scsih_eedp_error_handling - return sense code for EEDP errors
2906 * @scmd: pointer to scsi command object
2907 * @ioc_status: ioc status
2908 *
2909 * Returns nothing
2910 */
2911static void
2912_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2913{
2914 u8 ascq;
2915 u8 sk;
2916 u8 host_byte;
2917
2918 switch (ioc_status) {
2919 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2920 ascq = 0x01;
2921 break;
2922 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2923 ascq = 0x02;
2924 break;
2925 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2926 ascq = 0x03;
2927 break;
2928 default:
2929 ascq = 0x00;
2930 break;
2931 }
2932
2933 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2934 sk = ILLEGAL_REQUEST;
2935 host_byte = DID_ABORT;
2936 } else {
2937 sk = ABORTED_COMMAND;
2938 host_byte = DID_OK;
2939 }
2940
2941 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2942 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2943 SAM_STAT_CHECK_CONDITION;
2944}
2945
2946/**
Eric Moored5d135b2009-05-18 13:02:08 -06002947 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002948 * @scmd: pointer to scsi command object
2949 * @done: function pointer to be invoked on completion
2950 *
2951 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2952 *
2953 * Returns 0 on success. If there's a failure, return either:
2954 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2955 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2956 */
2957static int
Eric Moored5d135b2009-05-18 13:02:08 -06002958_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002959{
2960 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2961 struct MPT2SAS_DEVICE *sas_device_priv_data;
2962 struct MPT2SAS_TARGET *sas_target_priv_data;
2963 Mpi2SCSIIORequest_t *mpi_request;
2964 u32 mpi_control;
2965 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002966
2967 scmd->scsi_done = done;
2968 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05302969 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002970 scmd->result = DID_NO_CONNECT << 16;
2971 scmd->scsi_done(scmd);
2972 return 0;
2973 }
2974
2975 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05302976 /* invalid device handle */
2977 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06002978 scmd->result = DID_NO_CONNECT << 16;
2979 scmd->scsi_done(scmd);
2980 return 0;
2981 }
2982
Kashyap, Desai130b9582010-04-08 17:54:32 +05302983 /* host recovery or link resets sent via IOCTLs */
2984 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002985 return SCSI_MLQUEUE_HOST_BUSY;
Kashyap, Desai130b9582010-04-08 17:54:32 +05302986 /* device busy with task managment */
2987 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
2988 return SCSI_MLQUEUE_DEVICE_BUSY;
2989 /* device has been deleted */
2990 else if (sas_target_priv_data->deleted) {
2991 scmd->result = DID_NO_CONNECT << 16;
2992 scmd->scsi_done(scmd);
2993 return 0;
2994 }
Eric Moore635374e2009-03-09 01:21:12 -06002995
2996 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2997 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2998 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2999 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3000 else
3001 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3002
3003 /* set tags */
3004 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3005 if (scmd->device->tagged_supported) {
3006 if (scmd->device->ordered_tags)
3007 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3008 else
3009 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3010 } else
3011/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3012/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3013 */
3014 mpi_control |= (0x500);
3015
3016 } else
3017 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303018 /* Make sure Device is not raid volume */
3019 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003020 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003021 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3022
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303023 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003024 if (!smid) {
3025 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3026 ioc->name, __func__);
3027 goto out;
3028 }
3029 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3030 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003031 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003032 if (scmd->cmd_len == 32)
3033 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003034 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3035 if (sas_device_priv_data->sas_target->flags &
3036 MPT_TARGET_FLAGS_RAID_COMPONENT)
3037 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3038 else
3039 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3040 mpi_request->DevHandle =
3041 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3042 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3043 mpi_request->Control = cpu_to_le32(mpi_control);
3044 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3045 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3046 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3047 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303048 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003049 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3050 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3051 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303052 mpi_request->VF_ID = 0; /* TODO */
3053 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003054 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3055 mpi_request->LUN);
3056 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3057
3058 if (!mpi_request->DataLength) {
3059 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3060 } else {
3061 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3062 mpt2sas_base_free_smid(ioc, smid);
3063 goto out;
3064 }
3065 }
3066
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303067 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3068 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3069 sas_device_priv_data->sas_target->handle);
3070 else
3071 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003072 return 0;
3073
3074 out:
3075 return SCSI_MLQUEUE_HOST_BUSY;
3076}
3077
3078/**
3079 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3080 * @sense_buffer: sense data returned by target
3081 * @data: normalized skey/asc/ascq
3082 *
3083 * Return nothing.
3084 */
3085static void
3086_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3087{
3088 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3089 /* descriptor format */
3090 data->skey = sense_buffer[1] & 0x0F;
3091 data->asc = sense_buffer[2];
3092 data->ascq = sense_buffer[3];
3093 } else {
3094 /* fixed format */
3095 data->skey = sense_buffer[2] & 0x0F;
3096 data->asc = sense_buffer[12];
3097 data->ascq = sense_buffer[13];
3098 }
3099}
3100
3101#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3102/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003103 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003104 * @ioc: per adapter object
3105 * @scmd: pointer to scsi command object
3106 * @mpi_reply: reply mf payload returned from firmware
3107 *
3108 * scsi_status - SCSI Status code returned from target device
3109 * scsi_state - state info associated with SCSI_IO determined by ioc
3110 * ioc_status - ioc supplied status info
3111 *
3112 * Return nothing.
3113 */
3114static void
3115_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3116 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3117{
3118 u32 response_info;
3119 u8 *response_bytes;
3120 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3121 MPI2_IOCSTATUS_MASK;
3122 u8 scsi_state = mpi_reply->SCSIState;
3123 u8 scsi_status = mpi_reply->SCSIStatus;
3124 char *desc_ioc_state = NULL;
3125 char *desc_scsi_status = NULL;
3126 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303127 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3128
3129 if (log_info == 0x31170000)
3130 return;
Eric Moore635374e2009-03-09 01:21:12 -06003131
3132 switch (ioc_status) {
3133 case MPI2_IOCSTATUS_SUCCESS:
3134 desc_ioc_state = "success";
3135 break;
3136 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3137 desc_ioc_state = "invalid function";
3138 break;
3139 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3140 desc_ioc_state = "scsi recovered error";
3141 break;
3142 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3143 desc_ioc_state = "scsi invalid dev handle";
3144 break;
3145 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3146 desc_ioc_state = "scsi device not there";
3147 break;
3148 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3149 desc_ioc_state = "scsi data overrun";
3150 break;
3151 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3152 desc_ioc_state = "scsi data underrun";
3153 break;
3154 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3155 desc_ioc_state = "scsi io data error";
3156 break;
3157 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3158 desc_ioc_state = "scsi protocol error";
3159 break;
3160 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3161 desc_ioc_state = "scsi task terminated";
3162 break;
3163 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3164 desc_ioc_state = "scsi residual mismatch";
3165 break;
3166 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3167 desc_ioc_state = "scsi task mgmt failed";
3168 break;
3169 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3170 desc_ioc_state = "scsi ioc terminated";
3171 break;
3172 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3173 desc_ioc_state = "scsi ext terminated";
3174 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003175 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3176 desc_ioc_state = "eedp guard error";
3177 break;
3178 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3179 desc_ioc_state = "eedp ref tag error";
3180 break;
3181 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3182 desc_ioc_state = "eedp app tag error";
3183 break;
Eric Moore635374e2009-03-09 01:21:12 -06003184 default:
3185 desc_ioc_state = "unknown";
3186 break;
3187 }
3188
3189 switch (scsi_status) {
3190 case MPI2_SCSI_STATUS_GOOD:
3191 desc_scsi_status = "good";
3192 break;
3193 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3194 desc_scsi_status = "check condition";
3195 break;
3196 case MPI2_SCSI_STATUS_CONDITION_MET:
3197 desc_scsi_status = "condition met";
3198 break;
3199 case MPI2_SCSI_STATUS_BUSY:
3200 desc_scsi_status = "busy";
3201 break;
3202 case MPI2_SCSI_STATUS_INTERMEDIATE:
3203 desc_scsi_status = "intermediate";
3204 break;
3205 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3206 desc_scsi_status = "intermediate condmet";
3207 break;
3208 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3209 desc_scsi_status = "reservation conflict";
3210 break;
3211 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3212 desc_scsi_status = "command terminated";
3213 break;
3214 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3215 desc_scsi_status = "task set full";
3216 break;
3217 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3218 desc_scsi_status = "aca active";
3219 break;
3220 case MPI2_SCSI_STATUS_TASK_ABORTED:
3221 desc_scsi_status = "task aborted";
3222 break;
3223 default:
3224 desc_scsi_status = "unknown";
3225 break;
3226 }
3227
3228 desc_scsi_state[0] = '\0';
3229 if (!scsi_state)
3230 desc_scsi_state = " ";
3231 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3232 strcat(desc_scsi_state, "response info ");
3233 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3234 strcat(desc_scsi_state, "state terminated ");
3235 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3236 strcat(desc_scsi_state, "no status ");
3237 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3238 strcat(desc_scsi_state, "autosense failed ");
3239 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3240 strcat(desc_scsi_state, "autosense valid ");
3241
3242 scsi_print_command(scmd);
3243 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3244 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3245 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3246 ioc_status, smid);
3247 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3248 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3249 scsi_get_resid(scmd));
3250 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3251 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3252 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3253 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3254 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3255 scsi_status, desc_scsi_state, scsi_state);
3256
3257 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3258 struct sense_info data;
3259 _scsih_normalize_sense(scmd->sense_buffer, &data);
3260 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303261 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3262 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003263 }
3264
3265 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3266 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3267 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303268 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003269 }
3270}
3271#endif
3272
3273/**
3274 * _scsih_smart_predicted_fault - illuminate Fault LED
3275 * @ioc: per adapter object
3276 * @handle: device handle
3277 *
3278 * Return nothing.
3279 */
3280static void
3281_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3282{
3283 Mpi2SepReply_t mpi_reply;
3284 Mpi2SepRequest_t mpi_request;
3285 struct scsi_target *starget;
3286 struct MPT2SAS_TARGET *sas_target_priv_data;
3287 Mpi2EventNotificationReply_t *event_reply;
3288 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3289 struct _sas_device *sas_device;
3290 ssize_t sz;
3291 unsigned long flags;
3292
3293 /* only handle non-raid devices */
3294 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3295 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3296 if (!sas_device) {
3297 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3298 return;
3299 }
3300 starget = sas_device->starget;
3301 sas_target_priv_data = starget->hostdata;
3302
3303 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3304 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3305 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3306 return;
3307 }
3308 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3309 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3310
3311 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3312 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3313 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3314 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3315 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303316 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003317 mpi_request.DevHandle = cpu_to_le16(handle);
3318 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3319 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3320 &mpi_request)) != 0) {
3321 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3322 ioc->name, __FILE__, __LINE__, __func__);
3323 return;
3324 }
3325
3326 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3327 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3328 "enclosure_processor: ioc_status (0x%04x), "
3329 "loginfo(0x%08x)\n", ioc->name,
3330 le16_to_cpu(mpi_reply.IOCStatus),
3331 le32_to_cpu(mpi_reply.IOCLogInfo)));
3332 return;
3333 }
3334 }
3335
3336 /* insert into event log */
3337 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3338 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3339 event_reply = kzalloc(sz, GFP_KERNEL);
3340 if (!event_reply) {
3341 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3342 ioc->name, __FILE__, __LINE__, __func__);
3343 return;
3344 }
3345
3346 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3347 event_reply->Event =
3348 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3349 event_reply->MsgLength = sz/4;
3350 event_reply->EventDataLength =
3351 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3352 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3353 event_reply->EventData;
3354 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3355 event_data->ASC = 0x5D;
3356 event_data->DevHandle = cpu_to_le16(handle);
3357 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3358 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3359 kfree(event_reply);
3360}
3361
3362/**
Eric Moored5d135b2009-05-18 13:02:08 -06003363 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003364 * @ioc: per adapter object
3365 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303366 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003367 * @reply: reply message frame(lower 32bit addr)
3368 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303369 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003370 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303371 * Return 1 meaning mf should be freed from _base_interrupt
3372 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003373 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303374static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303375_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003376{
3377 Mpi2SCSIIORequest_t *mpi_request;
3378 Mpi2SCSIIOReply_t *mpi_reply;
3379 struct scsi_cmnd *scmd;
3380 u16 ioc_status;
3381 u32 xfer_cnt;
3382 u8 scsi_state;
3383 u8 scsi_status;
3384 u32 log_info;
3385 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303386 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003387
3388 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303389 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003390 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303391 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003392
3393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3394
3395 if (mpi_reply == NULL) {
3396 scmd->result = DID_OK << 16;
3397 goto out;
3398 }
3399
3400 sas_device_priv_data = scmd->device->hostdata;
3401 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3402 sas_device_priv_data->sas_target->deleted) {
3403 scmd->result = DID_NO_CONNECT << 16;
3404 goto out;
3405 }
3406
3407 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303408 scsi_state = mpi_reply->SCSIState;
3409 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3410 response_code =
3411 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003412 if (!sas_device_priv_data->tlr_snoop_check) {
3413 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303414 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3415 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303416 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3417 sas_disable_tlr(scmd->device);
3418 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3419 }
Eric Moore635374e2009-03-09 01:21:12 -06003420 }
3421
3422 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3423 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3424 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3425 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3426 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3427 else
3428 log_info = 0;
3429 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003430 scsi_status = mpi_reply->SCSIStatus;
3431
3432 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3433 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3434 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3435 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3436 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3437 }
3438
3439 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3440 struct sense_info data;
3441 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3442 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003443 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003444 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003445 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003446 _scsih_normalize_sense(scmd->sense_buffer, &data);
3447 /* failure prediction threshold exceeded */
3448 if (data.asc == 0x5D)
3449 _scsih_smart_predicted_fault(ioc,
3450 le16_to_cpu(mpi_reply->DevHandle));
3451 }
3452
3453 switch (ioc_status) {
3454 case MPI2_IOCSTATUS_BUSY:
3455 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3456 scmd->result = SAM_STAT_BUSY;
3457 break;
3458
3459 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3460 scmd->result = DID_NO_CONNECT << 16;
3461 break;
3462
3463 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3464 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303465 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3466 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003467 }
Eric Moore635374e2009-03-09 01:21:12 -06003468 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3469 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3470 scmd->result = DID_RESET << 16;
3471 break;
3472
3473 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3474 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3475 scmd->result = DID_SOFT_ERROR << 16;
3476 else
3477 scmd->result = (DID_OK << 16) | scsi_status;
3478 break;
3479
3480 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3481 scmd->result = (DID_OK << 16) | scsi_status;
3482
3483 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3484 break;
3485
3486 if (xfer_cnt < scmd->underflow) {
3487 if (scsi_status == SAM_STAT_BUSY)
3488 scmd->result = SAM_STAT_BUSY;
3489 else
3490 scmd->result = DID_SOFT_ERROR << 16;
3491 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3492 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3493 scmd->result = DID_SOFT_ERROR << 16;
3494 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3495 scmd->result = DID_RESET << 16;
3496 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3497 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3498 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3499 scmd->result = (DRIVER_SENSE << 24) |
3500 SAM_STAT_CHECK_CONDITION;
3501 scmd->sense_buffer[0] = 0x70;
3502 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3503 scmd->sense_buffer[12] = 0x20;
3504 scmd->sense_buffer[13] = 0;
3505 }
3506 break;
3507
3508 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3509 scsi_set_resid(scmd, 0);
3510 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3511 case MPI2_IOCSTATUS_SUCCESS:
3512 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303513 if (response_code ==
3514 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3515 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3516 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003517 scmd->result = DID_SOFT_ERROR << 16;
3518 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3519 scmd->result = DID_RESET << 16;
3520 break;
3521
Eric Moore3c621b32009-05-18 12:59:41 -06003522 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3523 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3524 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3525 _scsih_eedp_error_handling(scmd, ioc_status);
3526 break;
Eric Moore635374e2009-03-09 01:21:12 -06003527 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3528 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3529 case MPI2_IOCSTATUS_INVALID_SGL:
3530 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3531 case MPI2_IOCSTATUS_INVALID_FIELD:
3532 case MPI2_IOCSTATUS_INVALID_STATE:
3533 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3534 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3535 default:
3536 scmd->result = DID_SOFT_ERROR << 16;
3537 break;
3538
3539 }
3540
3541#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3542 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3543 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3544#endif
3545
3546 out:
3547 scsi_dma_unmap(scmd);
3548 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303549 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003550}
3551
3552/**
Eric Moore635374e2009-03-09 01:21:12 -06003553 * _scsih_sas_host_refresh - refreshing sas host object contents
3554 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003555 * Context: user
3556 *
3557 * During port enable, fw will send topology events for every device. Its
3558 * possible that the handles may change from the previous setting, so this
3559 * code keeping handles updating if changed.
3560 *
3561 * Return nothing.
3562 */
3563static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303564_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003565{
3566 u16 sz;
3567 u16 ioc_status;
3568 int i;
3569 Mpi2ConfigReply_t mpi_reply;
3570 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303571 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003572
3573 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3574 "updating handles for sas_host(0x%016llx)\n",
3575 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3576
3577 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3578 * sizeof(Mpi2SasIOUnit0PhyData_t));
3579 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3580 if (!sas_iounit_pg0) {
3581 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3582 ioc->name, __FILE__, __LINE__, __func__);
3583 return;
3584 }
Eric Moore635374e2009-03-09 01:21:12 -06003585
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303586 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3587 sas_iounit_pg0, sz)) != 0)
3588 goto out;
3589 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3590 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3591 goto out;
3592 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3593 if (i == 0)
3594 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3595 PhyData[0].ControllerDevHandle);
3596 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3597 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3598 AttachedDevHandle);
3599 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3600 attached_handle, i, sas_iounit_pg0->PhyData[i].
3601 NegotiatedLinkRate >> 4);
3602 }
Eric Moore635374e2009-03-09 01:21:12 -06003603 out:
3604 kfree(sas_iounit_pg0);
3605}
3606
3607/**
3608 * _scsih_sas_host_add - create sas host object
3609 * @ioc: per adapter object
3610 *
3611 * Creating host side data object, stored in ioc->sas_hba
3612 *
3613 * Return nothing.
3614 */
3615static void
3616_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3617{
3618 int i;
3619 Mpi2ConfigReply_t mpi_reply;
3620 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3621 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3622 Mpi2SasPhyPage0_t phy_pg0;
3623 Mpi2SasDevicePage0_t sas_device_pg0;
3624 Mpi2SasEnclosurePage0_t enclosure_pg0;
3625 u16 ioc_status;
3626 u16 sz;
3627 u16 device_missing_delay;
3628
3629 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3630 if (!ioc->sas_hba.num_phys) {
3631 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3632 ioc->name, __FILE__, __LINE__, __func__);
3633 return;
3634 }
3635
3636 /* sas_iounit page 0 */
3637 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3638 sizeof(Mpi2SasIOUnit0PhyData_t));
3639 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3640 if (!sas_iounit_pg0) {
3641 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3642 ioc->name, __FILE__, __LINE__, __func__);
3643 return;
3644 }
3645 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3646 sas_iounit_pg0, sz))) {
3647 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3648 ioc->name, __FILE__, __LINE__, __func__);
3649 goto out;
3650 }
3651 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3652 MPI2_IOCSTATUS_MASK;
3653 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3654 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3655 ioc->name, __FILE__, __LINE__, __func__);
3656 goto out;
3657 }
3658
3659 /* sas_iounit page 1 */
3660 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3661 sizeof(Mpi2SasIOUnit1PhyData_t));
3662 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3663 if (!sas_iounit_pg1) {
3664 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3665 ioc->name, __FILE__, __LINE__, __func__);
3666 goto out;
3667 }
3668 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3669 sas_iounit_pg1, sz))) {
3670 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3671 ioc->name, __FILE__, __LINE__, __func__);
3672 goto out;
3673 }
3674 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3675 MPI2_IOCSTATUS_MASK;
3676 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3678 ioc->name, __FILE__, __LINE__, __func__);
3679 goto out;
3680 }
3681
3682 ioc->io_missing_delay =
3683 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3684 device_missing_delay =
3685 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3686 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3687 ioc->device_missing_delay = (device_missing_delay &
3688 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3689 else
3690 ioc->device_missing_delay = device_missing_delay &
3691 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3692
3693 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3694 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3695 sizeof(struct _sas_phy), GFP_KERNEL);
3696 if (!ioc->sas_hba.phy) {
3697 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3698 ioc->name, __FILE__, __LINE__, __func__);
3699 goto out;
3700 }
3701 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3702 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3703 i))) {
3704 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3705 ioc->name, __FILE__, __LINE__, __func__);
3706 goto out;
3707 }
3708 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3709 MPI2_IOCSTATUS_MASK;
3710 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3711 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3712 ioc->name, __FILE__, __LINE__, __func__);
3713 goto out;
3714 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303715
3716 if (i == 0)
3717 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3718 PhyData[0].ControllerDevHandle);
3719 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06003720 ioc->sas_hba.phy[i].phy_id = i;
3721 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3722 phy_pg0, ioc->sas_hba.parent_dev);
3723 }
3724 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303725 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06003726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3727 ioc->name, __FILE__, __LINE__, __func__);
3728 goto out;
3729 }
Eric Moore635374e2009-03-09 01:21:12 -06003730 ioc->sas_hba.enclosure_handle =
3731 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3732 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3733 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3734 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3735 (unsigned long long) ioc->sas_hba.sas_address,
3736 ioc->sas_hba.num_phys) ;
3737
3738 if (ioc->sas_hba.enclosure_handle) {
3739 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3740 &enclosure_pg0,
3741 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3742 ioc->sas_hba.enclosure_handle))) {
3743 ioc->sas_hba.enclosure_logical_id =
3744 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3745 }
3746 }
3747
3748 out:
3749 kfree(sas_iounit_pg1);
3750 kfree(sas_iounit_pg0);
3751}
3752
3753/**
3754 * _scsih_expander_add - creating expander object
3755 * @ioc: per adapter object
3756 * @handle: expander handle
3757 *
3758 * Creating expander object, stored in ioc->sas_expander_list.
3759 *
3760 * Return 0 for success, else error.
3761 */
3762static int
3763_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3764{
3765 struct _sas_node *sas_expander;
3766 Mpi2ConfigReply_t mpi_reply;
3767 Mpi2ExpanderPage0_t expander_pg0;
3768 Mpi2ExpanderPage1_t expander_pg1;
3769 Mpi2SasEnclosurePage0_t enclosure_pg0;
3770 u32 ioc_status;
3771 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303772 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003773 int i;
3774 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303775 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003776 int rc = 0;
3777
3778 if (!handle)
3779 return -1;
3780
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303781 if (ioc->shost_recovery)
3782 return -1;
3783
Eric Moore635374e2009-03-09 01:21:12 -06003784 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3785 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3786 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3787 ioc->name, __FILE__, __LINE__, __func__);
3788 return -1;
3789 }
3790
3791 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3792 MPI2_IOCSTATUS_MASK;
3793 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3794 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3795 ioc->name, __FILE__, __LINE__, __func__);
3796 return -1;
3797 }
3798
3799 /* handle out of order topology events */
3800 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303801 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3802 != 0) {
3803 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3804 ioc->name, __FILE__, __LINE__, __func__);
3805 return -1;
3806 }
3807 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06003808 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303809 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3810 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003811 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3812 if (!sas_expander) {
3813 rc = _scsih_expander_add(ioc, parent_handle);
3814 if (rc != 0)
3815 return rc;
3816 }
3817 }
3818
Eric Moore635374e2009-03-09 01:21:12 -06003819 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303820 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06003821 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3822 sas_address);
3823 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3824
3825 if (sas_expander)
3826 return 0;
3827
3828 sas_expander = kzalloc(sizeof(struct _sas_node),
3829 GFP_KERNEL);
3830 if (!sas_expander) {
3831 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3832 ioc->name, __FILE__, __LINE__, __func__);
3833 return -1;
3834 }
3835
3836 sas_expander->handle = handle;
3837 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303838 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06003839 sas_expander->sas_address = sas_address;
3840
3841 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3842 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303843 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06003844 sas_expander->sas_address, sas_expander->num_phys);
3845
3846 if (!sas_expander->num_phys)
3847 goto out_fail;
3848 sas_expander->phy = kcalloc(sas_expander->num_phys,
3849 sizeof(struct _sas_phy), GFP_KERNEL);
3850 if (!sas_expander->phy) {
3851 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3852 ioc->name, __FILE__, __LINE__, __func__);
3853 rc = -1;
3854 goto out_fail;
3855 }
3856
3857 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3858 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303859 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003860 if (!mpt2sas_port) {
3861 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3862 ioc->name, __FILE__, __LINE__, __func__);
3863 rc = -1;
3864 goto out_fail;
3865 }
3866 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3867
3868 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3869 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3870 &expander_pg1, i, handle))) {
3871 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3872 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303873 rc = -1;
3874 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003875 }
3876 sas_expander->phy[i].handle = handle;
3877 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303878
3879 if ((mpt2sas_transport_add_expander_phy(ioc,
3880 &sas_expander->phy[i], expander_pg1,
3881 sas_expander->parent_dev))) {
3882 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3883 ioc->name, __FILE__, __LINE__, __func__);
3884 rc = -1;
3885 goto out_fail;
3886 }
Eric Moore635374e2009-03-09 01:21:12 -06003887 }
3888
3889 if (sas_expander->enclosure_handle) {
3890 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3891 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3892 sas_expander->enclosure_handle))) {
3893 sas_expander->enclosure_logical_id =
3894 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3895 }
3896 }
3897
3898 _scsih_expander_node_add(ioc, sas_expander);
3899 return 0;
3900
3901 out_fail:
3902
Kashyap, Desai20f58952009-08-07 19:34:26 +05303903 if (mpt2sas_port)
3904 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303905 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003906 kfree(sas_expander);
3907 return rc;
3908}
3909
3910/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05303911 * _scsih_done - scsih callback handler.
3912 * @ioc: per adapter object
3913 * @smid: system request message index
3914 * @msix_index: MSIX table index supplied by the OS
3915 * @reply: reply message frame(lower 32bit addr)
3916 *
3917 * Callback handler when sending internal generated message frames.
3918 * The callback index passed is `ioc->scsih_cb_idx`
3919 *
3920 * Return 1 meaning mf should be freed from _base_interrupt
3921 * 0 means the mf is freed from this function.
3922 */
3923static u8
3924_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3925{
3926 MPI2DefaultReply_t *mpi_reply;
3927
3928 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3929 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
3930 return 1;
3931 if (ioc->scsih_cmds.smid != smid)
3932 return 1;
3933 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
3934 if (mpi_reply) {
3935 memcpy(ioc->scsih_cmds.reply, mpi_reply,
3936 mpi_reply->MsgLength*4);
3937 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
3938 }
3939 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
3940 complete(&ioc->scsih_cmds.done);
3941 return 1;
3942}
3943
3944/**
Eric Moore635374e2009-03-09 01:21:12 -06003945 * _scsih_expander_remove - removing expander object
3946 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303947 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06003948 *
3949 * Return nothing.
3950 */
3951static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303952_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06003953{
3954 struct _sas_node *sas_expander;
3955 unsigned long flags;
3956
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303957 if (ioc->shost_recovery)
3958 return;
3959
Eric Moore635374e2009-03-09 01:21:12 -06003960 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303961 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3962 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003963 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3964 _scsih_expander_node_remove(ioc, sas_expander);
3965}
3966
3967/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05303968 * _scsih_check_access_status - check access flags
3969 * @ioc: per adapter object
3970 * @sas_address: sas address
3971 * @handle: sas device handle
3972 * @access_flags: errors returned during discovery of the device
3973 *
3974 * Return 0 for success, else failure
3975 */
3976static u8
3977_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
3978 u16 handle, u8 access_status)
3979{
3980 u8 rc = 1;
3981 char *desc = NULL;
3982
3983 switch (access_status) {
3984 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
3985 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
3986 rc = 0;
3987 break;
3988 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
3989 desc = "sata capability failed";
3990 break;
3991 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
3992 desc = "sata affiliation conflict";
3993 break;
3994 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
3995 desc = "route not addressable";
3996 break;
3997 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
3998 desc = "smp error not addressable";
3999 break;
4000 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4001 desc = "device blocked";
4002 break;
4003 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4004 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4005 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4006 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4007 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4008 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4009 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4010 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4011 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4012 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4013 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4014 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4015 desc = "sata initialization failed";
4016 break;
4017 default:
4018 desc = "unknown";
4019 break;
4020 }
4021
4022 if (!rc)
4023 return 0;
4024
4025 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4026 "handle(0x%04x)\n", ioc->name, desc,
4027 (unsigned long long)sas_address, handle);
4028 return rc;
4029}
4030
4031static void
4032_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4033{
4034 Mpi2ConfigReply_t mpi_reply;
4035 Mpi2SasDevicePage0_t sas_device_pg0;
4036 struct _sas_device *sas_device;
4037 u32 ioc_status;
4038 unsigned long flags;
4039 u64 sas_address;
4040 struct scsi_target *starget;
4041 struct MPT2SAS_TARGET *sas_target_priv_data;
4042 u32 device_info;
4043
4044 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4045 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4046 return;
4047
4048 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4049 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4050 return;
4051
4052 /* check if this is end device */
4053 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4054 if (!(_scsih_is_end_device(device_info)))
4055 return;
4056
4057 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4058 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4059 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4060 sas_address);
4061
4062 if (!sas_device) {
4063 printk(MPT2SAS_ERR_FMT "device is not present "
4064 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4065 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4066 return;
4067 }
4068
4069 if (unlikely(sas_device->handle != handle)) {
4070 starget = sas_device->starget;
4071 sas_target_priv_data = starget->hostdata;
4072 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4073 " to (0x%04x)!!!\n", sas_device->handle, handle);
4074 sas_target_priv_data->handle = handle;
4075 sas_device->handle = handle;
4076 }
4077 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4078
4079 /* check if device is present */
4080 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4081 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4082 printk(MPT2SAS_ERR_FMT "device is not present "
4083 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4084 return;
4085 }
4086
4087 /* check if there were any issues with discovery */
4088 if (_scsih_check_access_status(ioc, sas_address, handle,
4089 sas_device_pg0.AccessStatus))
4090 return;
4091 _scsih_ublock_io_device(ioc, handle);
4092
4093}
4094
4095/**
Eric Moore635374e2009-03-09 01:21:12 -06004096 * _scsih_add_device - creating sas device object
4097 * @ioc: per adapter object
4098 * @handle: sas device handle
4099 * @phy_num: phy number end device attached to
4100 * @is_pd: is this hidden raid component
4101 *
4102 * Creating end device object, stored in ioc->sas_device_list.
4103 *
4104 * Returns 0 for success, non-zero for failure.
4105 */
4106static int
4107_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4108{
4109 Mpi2ConfigReply_t mpi_reply;
4110 Mpi2SasDevicePage0_t sas_device_pg0;
4111 Mpi2SasEnclosurePage0_t enclosure_pg0;
4112 struct _sas_device *sas_device;
4113 u32 ioc_status;
4114 __le64 sas_address;
4115 u32 device_info;
4116 unsigned long flags;
4117
4118 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4119 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4120 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4121 ioc->name, __FILE__, __LINE__, __func__);
4122 return -1;
4123 }
4124
4125 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4126 MPI2_IOCSTATUS_MASK;
4127 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4128 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4129 ioc->name, __FILE__, __LINE__, __func__);
4130 return -1;
4131 }
4132
Kashyap, Desaib4344272010-03-17 16:24:14 +05304133 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4134
Eric Moore635374e2009-03-09 01:21:12 -06004135 /* check if device is present */
4136 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4137 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4138 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4139 ioc->name, __FILE__, __LINE__, __func__);
4140 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4141 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4142 return -1;
4143 }
4144
Kashyap, Desaib4344272010-03-17 16:24:14 +05304145 /* check if there were any issues with discovery */
4146 if (_scsih_check_access_status(ioc, sas_address, handle,
4147 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004148 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004149
4150 /* check if this is end device */
4151 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4152 if (!(_scsih_is_end_device(device_info))) {
4153 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4154 ioc->name, __FILE__, __LINE__, __func__);
4155 return -1;
4156 }
4157
Eric Moore635374e2009-03-09 01:21:12 -06004158
4159 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4160 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4161 sas_address);
4162 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4163
Kashyap, Desaib4344272010-03-17 16:24:14 +05304164 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004165 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004166
4167 sas_device = kzalloc(sizeof(struct _sas_device),
4168 GFP_KERNEL);
4169 if (!sas_device) {
4170 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4171 ioc->name, __FILE__, __LINE__, __func__);
4172 return -1;
4173 }
4174
4175 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304176 if (_scsih_get_sas_address(ioc, le16_to_cpu
4177 (sas_device_pg0.ParentDevHandle),
4178 &sas_device->sas_address_parent) != 0)
4179 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4180 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004181 sas_device->enclosure_handle =
4182 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4183 sas_device->slot =
4184 le16_to_cpu(sas_device_pg0.Slot);
4185 sas_device->device_info = device_info;
4186 sas_device->sas_address = sas_address;
4187 sas_device->hidden_raid_component = is_pd;
4188
4189 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304190 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4191 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4192 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004193 sas_device->enclosure_logical_id =
4194 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004195
4196 /* get device name */
4197 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4198
4199 if (ioc->wait_for_port_enable_to_complete)
4200 _scsih_sas_device_init_add(ioc, sas_device);
4201 else
4202 _scsih_sas_device_add(ioc, sas_device);
4203
4204 return 0;
4205}
4206
4207/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304208 * _scsih_remove_pd_device - removing sas device pd object
Eric Moore635374e2009-03-09 01:21:12 -06004209 * @ioc: per adapter object
Kashyap, Desai1278b112010-03-09 17:34:13 +05304210 * @sas_device_delete: the sas_device object
Eric Moore635374e2009-03-09 01:21:12 -06004211 *
Kashyap, Desai1278b112010-03-09 17:34:13 +05304212 * For hidden raid components, we do driver-fw handshake from
4213 * hotplug work threads.
Eric Moore635374e2009-03-09 01:21:12 -06004214 * Return nothing.
4215 */
4216static void
Kashyap, Desai1278b112010-03-09 17:34:13 +05304217_scsih_remove_pd_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
4218 sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004219{
Eric Moore635374e2009-03-09 01:21:12 -06004220 Mpi2SasIoUnitControlReply_t mpi_reply;
4221 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desai1278b112010-03-09 17:34:13 +05304222 u16 vol_handle, handle;
Eric Moore635374e2009-03-09 01:21:12 -06004223
Kashyap, Desai1278b112010-03-09 17:34:13 +05304224 handle = sas_device.handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304225 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
4226 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
Kashyap, Desai1278b112010-03-09 17:34:13 +05304227 (unsigned long long) sas_device.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004228
Kashyap, Desai1278b112010-03-09 17:34:13 +05304229 vol_handle = sas_device.volume_handle;
4230 if (!vol_handle)
4231 return;
4232 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
4233 "handle(0x%04x)\n", ioc->name, vol_handle));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05304234 mpt2sas_scsih_issue_tm(ioc, vol_handle, 0, 0, 0,
4235 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30, NULL);
Kashyap, Desai1278b112010-03-09 17:34:13 +05304236 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
4237 "done: handle(0x%04x)\n", ioc->name, vol_handle));
4238 if (ioc->shost_recovery)
4239 return;
Eric Moore635374e2009-03-09 01:21:12 -06004240
4241 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
4242 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
4243 "(0x%04x)\n", ioc->name, handle));
4244 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4245 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4246 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05304247 mpi_request.DevHandle = cpu_to_le16(handle);
Eric Moore635374e2009-03-09 01:21:12 -06004248 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
Kashyap, Desai1278b112010-03-09 17:34:13 +05304249 &mpi_request)) != 0)
Eric Moore635374e2009-03-09 01:21:12 -06004250 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4251 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004252
4253 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
4254 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
4255 le16_to_cpu(mpi_reply.IOCStatus),
4256 le32_to_cpu(mpi_reply.IOCLogInfo)));
4257
Kashyap, Desai1278b112010-03-09 17:34:13 +05304258 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle(0x%04x),"
4259 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
4260 (unsigned long long) sas_device.sas_address));
4261}
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304262
Kashyap, Desai1278b112010-03-09 17:34:13 +05304263/**
4264 * _scsih_remove_device - removing sas device object
4265 * @ioc: per adapter object
4266 * @sas_device_delete: the sas_device object
4267 *
4268 * Return nothing.
4269 */
4270static void
4271_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4272 struct _sas_device *sas_device)
4273{
4274 struct _sas_device sas_device_backup;
4275 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304276
Kashyap, Desai1278b112010-03-09 17:34:13 +05304277 if (!sas_device)
4278 return;
Eric Moore635374e2009-03-09 01:21:12 -06004279
Kashyap, Desai1278b112010-03-09 17:34:13 +05304280 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004281 _scsih_sas_device_remove(ioc, sas_device);
4282
Kashyap, Desai1278b112010-03-09 17:34:13 +05304283 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4284 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4285 sas_device_backup.handle, (unsigned long long)
4286 sas_device_backup.sas_address));
4287
4288 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4289 sas_target_priv_data = sas_device_backup.starget->hostdata;
4290 sas_target_priv_data->deleted = 1;
4291 }
4292
4293 if (sas_device_backup.hidden_raid_component)
4294 _scsih_remove_pd_device(ioc, sas_device_backup);
4295
4296 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4297
4298 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4299 sas_device_backup.sas_address_parent);
4300
4301 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4302 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4303 (unsigned long long) sas_device_backup.sas_address);
4304
4305 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4306 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4307 sas_device_backup.handle, (unsigned long long)
4308 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004309}
4310
4311#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4312/**
4313 * _scsih_sas_topology_change_event_debug - debug for topology event
4314 * @ioc: per adapter object
4315 * @event_data: event data payload
4316 * Context: user.
4317 */
4318static void
4319_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4320 Mpi2EventDataSasTopologyChangeList_t *event_data)
4321{
4322 int i;
4323 u16 handle;
4324 u16 reason_code;
4325 u8 phy_number;
4326 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304327 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004328
4329 switch (event_data->ExpStatus) {
4330 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4331 status_str = "add";
4332 break;
4333 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4334 status_str = "remove";
4335 break;
4336 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304337 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004338 status_str = "responding";
4339 break;
4340 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4341 status_str = "remove delay";
4342 break;
4343 default:
4344 status_str = "unknown status";
4345 break;
4346 }
4347 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4348 ioc->name, status_str);
4349 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4350 "start_phy(%02d), count(%d)\n",
4351 le16_to_cpu(event_data->ExpanderDevHandle),
4352 le16_to_cpu(event_data->EnclosureHandle),
4353 event_data->StartPhyNum, event_data->NumEntries);
4354 for (i = 0; i < event_data->NumEntries; i++) {
4355 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4356 if (!handle)
4357 continue;
4358 phy_number = event_data->StartPhyNum + i;
4359 reason_code = event_data->PHY[i].PhyStatus &
4360 MPI2_EVENT_SAS_TOPO_RC_MASK;
4361 switch (reason_code) {
4362 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304363 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004364 break;
4365 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304366 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004367 break;
4368 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304369 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004370 break;
4371 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304372 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004373 break;
4374 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304375 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004376 break;
4377 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304378 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004379 break;
4380 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304381 link_rate = event_data->PHY[i].LinkRate >> 4;
4382 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4383 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
4384 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4385 handle, status_str, link_rate, prev_link_rate);
4386
Eric Moore635374e2009-03-09 01:21:12 -06004387 }
4388}
4389#endif
4390
4391/**
4392 * _scsih_sas_topology_change_event - handle topology changes
4393 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304394 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004395 * Context: user.
4396 *
4397 */
4398static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304399_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004400 struct fw_event_work *fw_event)
4401{
4402 int i;
4403 u16 parent_handle, handle;
4404 u16 reason_code;
4405 u8 phy_number;
4406 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304407 struct _sas_device *sas_device;
4408 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004409 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304410 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304411 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004412
4413#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4414 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4415 _scsih_sas_topology_change_event_debug(ioc, event_data);
4416#endif
4417
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304418 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304419 return;
4420
Eric Moore635374e2009-03-09 01:21:12 -06004421 if (!ioc->sas_hba.num_phys)
4422 _scsih_sas_host_add(ioc);
4423 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304424 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004425
4426 if (fw_event->ignore) {
4427 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4428 "event\n", ioc->name));
4429 return;
4430 }
4431
4432 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4433
4434 /* handle expander add */
4435 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4436 if (_scsih_expander_add(ioc, parent_handle) != 0)
4437 return;
4438
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304439 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4440 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4441 parent_handle);
4442 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4443 if (sas_expander)
4444 sas_address = sas_expander->sas_address;
4445 else if (parent_handle < ioc->sas_hba.num_phys)
4446 sas_address = ioc->sas_hba.sas_address;
4447 else
4448 return;
4449
Eric Moore635374e2009-03-09 01:21:12 -06004450 /* handle siblings events */
4451 for (i = 0; i < event_data->NumEntries; i++) {
4452 if (fw_event->ignore) {
4453 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4454 "expander event\n", ioc->name));
4455 return;
4456 }
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304457 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304458 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304459 phy_number = event_data->StartPhyNum + i;
4460 reason_code = event_data->PHY[i].PhyStatus &
4461 MPI2_EVENT_SAS_TOPO_RC_MASK;
4462 if ((event_data->PHY[i].PhyStatus &
4463 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4464 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004465 continue;
4466 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4467 if (!handle)
4468 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304469 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304470 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004471 switch (reason_code) {
4472 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304473
4474 if (link_rate == prev_link_rate)
4475 break;
4476
4477 mpt2sas_transport_update_links(ioc, sas_address,
4478 handle, phy_number, link_rate);
4479
Kashyap, Desaib4344272010-03-17 16:24:14 +05304480 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4481 break;
4482
4483 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304484 break;
Eric Moore635374e2009-03-09 01:21:12 -06004485 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304486
4487 mpt2sas_transport_update_links(ioc, sas_address,
4488 handle, phy_number, link_rate);
4489
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304490 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004491 break;
4492 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304493
4494 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4495 sas_device = _scsih_sas_device_find_by_handle(ioc,
4496 handle);
4497 if (!sas_device) {
4498 spin_unlock_irqrestore(&ioc->sas_device_lock,
4499 flags);
4500 break;
4501 }
4502 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4503 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004504 break;
4505 }
4506 }
4507
4508 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304509 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4510 sas_expander)
4511 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004512
4513}
4514
4515#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4516/**
4517 * _scsih_sas_device_status_change_event_debug - debug for device event
4518 * @event_data: event data payload
4519 * Context: user.
4520 *
4521 * Return nothing.
4522 */
4523static void
4524_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4525 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4526{
4527 char *reason_str = NULL;
4528
4529 switch (event_data->ReasonCode) {
4530 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4531 reason_str = "smart data";
4532 break;
4533 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4534 reason_str = "unsupported device discovered";
4535 break;
4536 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4537 reason_str = "internal device reset";
4538 break;
4539 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4540 reason_str = "internal task abort";
4541 break;
4542 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4543 reason_str = "internal task abort set";
4544 break;
4545 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4546 reason_str = "internal clear task set";
4547 break;
4548 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4549 reason_str = "internal query task";
4550 break;
4551 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4552 reason_str = "sata init failure";
4553 break;
4554 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4555 reason_str = "internal device reset complete";
4556 break;
4557 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4558 reason_str = "internal task abort complete";
4559 break;
4560 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4561 reason_str = "internal async notification";
4562 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304563 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4564 reason_str = "expander reduced functionality";
4565 break;
4566 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4567 reason_str = "expander reduced functionality complete";
4568 break;
Eric Moore635374e2009-03-09 01:21:12 -06004569 default:
4570 reason_str = "unknown reason";
4571 break;
4572 }
4573 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4574 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4575 reason_str, le16_to_cpu(event_data->DevHandle),
4576 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4577 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4578 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4579 event_data->ASC, event_data->ASCQ);
4580 printk(KERN_INFO "\n");
4581}
4582#endif
4583
4584/**
4585 * _scsih_sas_device_status_change_event - handle device status change
4586 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304587 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004588 * Context: user.
4589 *
4590 * Return nothing.
4591 */
4592static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304593_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4594 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004595{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304596 struct MPT2SAS_TARGET *target_priv_data;
4597 struct _sas_device *sas_device;
4598 __le64 sas_address;
4599 unsigned long flags;
4600 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4601 fw_event->event_data;
4602
Eric Moore635374e2009-03-09 01:21:12 -06004603#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4604 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304605 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304606 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004607#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304608
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304609 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304610 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304611 event_data->ReasonCode !=
4612 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304613 return;
4614
4615 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4616 sas_address = le64_to_cpu(event_data->SASAddress);
4617 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4618 sas_address);
4619 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4620
4621 if (!sas_device || !sas_device->starget)
4622 return;
4623
4624 target_priv_data = sas_device->starget->hostdata;
4625 if (!target_priv_data)
4626 return;
4627
4628 if (event_data->ReasonCode ==
4629 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4630 target_priv_data->tm_busy = 1;
4631 else
4632 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004633}
4634
4635#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4636/**
4637 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4638 * @ioc: per adapter object
4639 * @event_data: event data payload
4640 * Context: user.
4641 *
4642 * Return nothing.
4643 */
4644static void
4645_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4646 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4647{
4648 char *reason_str = NULL;
4649
4650 switch (event_data->ReasonCode) {
4651 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4652 reason_str = "enclosure add";
4653 break;
4654 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4655 reason_str = "enclosure remove";
4656 break;
4657 default:
4658 reason_str = "unknown reason";
4659 break;
4660 }
4661
4662 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4663 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4664 " number slots(%d)\n", ioc->name, reason_str,
4665 le16_to_cpu(event_data->EnclosureHandle),
4666 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4667 le16_to_cpu(event_data->StartSlot));
4668}
4669#endif
4670
4671/**
4672 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4673 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304674 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004675 * Context: user.
4676 *
4677 * Return nothing.
4678 */
4679static void
4680_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304681 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004682{
4683#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4684 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4685 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304686 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004687#endif
4688}
4689
4690/**
4691 * _scsih_sas_broadcast_primative_event - handle broadcast events
4692 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304693 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004694 * Context: user.
4695 *
4696 * Return nothing.
4697 */
4698static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304699_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4700 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004701{
4702 struct scsi_cmnd *scmd;
4703 u16 smid, handle;
4704 u32 lun;
4705 struct MPT2SAS_DEVICE *sas_device_priv_data;
4706 u32 termination_count;
4707 u32 query_count;
4708 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304709#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4710 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4711#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05304712 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004713 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4714 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4715 event_data->PortWidth));
Eric Moore635374e2009-03-09 01:21:12 -06004716 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4717 __func__));
4718
Eric Moore635374e2009-03-09 01:21:12 -06004719 termination_count = 0;
4720 query_count = 0;
4721 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304722 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004723 scmd = _scsih_scsi_lookup_get(ioc, smid);
4724 if (!scmd)
4725 continue;
4726 sas_device_priv_data = scmd->device->hostdata;
4727 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4728 continue;
4729 /* skip hidden raid components */
4730 if (sas_device_priv_data->sas_target->flags &
4731 MPT_TARGET_FLAGS_RAID_COMPONENT)
4732 continue;
4733 /* skip volumes */
4734 if (sas_device_priv_data->sas_target->flags &
4735 MPT_TARGET_FLAGS_VOLUME)
4736 continue;
4737
4738 handle = sas_device_priv_data->sas_target->handle;
4739 lun = sas_device_priv_data->lun;
4740 query_count++;
4741
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05304742 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
4743 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06004744 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05304745 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
4746 & MPI2_IOCSTATUS_MASK;
4747 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06004748 (mpi_reply->ResponseCode ==
4749 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4750 mpi_reply->ResponseCode ==
4751 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4752 continue;
4753
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05304754 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
4755 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06004756 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4757 }
Eric Moore635374e2009-03-09 01:21:12 -06004758 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004759
4760 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4761 "%s - exit, query_count = %d termination_count = %d\n",
4762 ioc->name, __func__, query_count, termination_count));
4763}
4764
4765/**
4766 * _scsih_sas_discovery_event - handle discovery events
4767 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304768 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004769 * Context: user.
4770 *
4771 * Return nothing.
4772 */
4773static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304774_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4775 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004776{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304777 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4778
Eric Moore635374e2009-03-09 01:21:12 -06004779#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4780 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4781 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4782 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4783 "start" : "stop");
4784 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304785 printk("discovery_status(0x%08x)",
4786 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06004787 printk("\n");
4788 }
4789#endif
4790
4791 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4792 !ioc->sas_hba.num_phys)
4793 _scsih_sas_host_add(ioc);
4794}
4795
4796/**
4797 * _scsih_reprobe_lun - reprobing lun
4798 * @sdev: scsi device struct
4799 * @no_uld_attach: sdev->no_uld_attach flag setting
4800 *
4801 **/
4802static void
4803_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4804{
4805 int rc;
4806
4807 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4808 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4809 sdev->no_uld_attach ? "hidding" : "exposing");
4810 rc = scsi_device_reprobe(sdev);
4811}
4812
4813/**
4814 * _scsih_reprobe_target - reprobing target
4815 * @starget: scsi target struct
4816 * @no_uld_attach: sdev->no_uld_attach flag setting
4817 *
4818 * Note: no_uld_attach flag determines whether the disk device is attached
4819 * to block layer. A value of `1` means to not attach.
4820 **/
4821static void
4822_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4823{
4824 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4825
4826 if (no_uld_attach)
4827 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4828 else
4829 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4830
4831 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4832 _scsih_reprobe_lun);
4833}
4834/**
4835 * _scsih_sas_volume_add - add new volume
4836 * @ioc: per adapter object
4837 * @element: IR config element data
4838 * Context: user.
4839 *
4840 * Return nothing.
4841 */
4842static void
4843_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4844 Mpi2EventIrConfigElement_t *element)
4845{
4846 struct _raid_device *raid_device;
4847 unsigned long flags;
4848 u64 wwid;
4849 u16 handle = le16_to_cpu(element->VolDevHandle);
4850 int rc;
4851
Eric Moore635374e2009-03-09 01:21:12 -06004852 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4853 if (!wwid) {
4854 printk(MPT2SAS_ERR_FMT
4855 "failure at %s:%d/%s()!\n", ioc->name,
4856 __FILE__, __LINE__, __func__);
4857 return;
4858 }
4859
4860 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4861 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4862 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4863
4864 if (raid_device)
4865 return;
4866
4867 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4868 if (!raid_device) {
4869 printk(MPT2SAS_ERR_FMT
4870 "failure at %s:%d/%s()!\n", ioc->name,
4871 __FILE__, __LINE__, __func__);
4872 return;
4873 }
4874
4875 raid_device->id = ioc->sas_id++;
4876 raid_device->channel = RAID_CHANNEL;
4877 raid_device->handle = handle;
4878 raid_device->wwid = wwid;
4879 _scsih_raid_device_add(ioc, raid_device);
4880 if (!ioc->wait_for_port_enable_to_complete) {
4881 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4882 raid_device->id, 0);
4883 if (rc)
4884 _scsih_raid_device_remove(ioc, raid_device);
4885 } else
4886 _scsih_determine_boot_device(ioc, raid_device, 1);
4887}
4888
4889/**
4890 * _scsih_sas_volume_delete - delete volume
4891 * @ioc: per adapter object
4892 * @element: IR config element data
4893 * Context: user.
4894 *
4895 * Return nothing.
4896 */
4897static void
4898_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4899 Mpi2EventIrConfigElement_t *element)
4900{
4901 struct _raid_device *raid_device;
4902 u16 handle = le16_to_cpu(element->VolDevHandle);
4903 unsigned long flags;
4904 struct MPT2SAS_TARGET *sas_target_priv_data;
4905
Eric Moore635374e2009-03-09 01:21:12 -06004906 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4907 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4908 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4909 if (!raid_device)
4910 return;
4911 if (raid_device->starget) {
4912 sas_target_priv_data = raid_device->starget->hostdata;
4913 sas_target_priv_data->deleted = 1;
4914 scsi_remove_target(&raid_device->starget->dev);
4915 }
4916 _scsih_raid_device_remove(ioc, raid_device);
4917}
4918
4919/**
4920 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4921 * @ioc: per adapter object
4922 * @element: IR config element data
4923 * Context: user.
4924 *
4925 * Return nothing.
4926 */
4927static void
4928_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4929 Mpi2EventIrConfigElement_t *element)
4930{
4931 struct _sas_device *sas_device;
4932 unsigned long flags;
4933 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4934
4935 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4936 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4937 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4938 if (!sas_device)
4939 return;
4940
4941 /* exposing raid component */
4942 sas_device->volume_handle = 0;
4943 sas_device->volume_wwid = 0;
4944 sas_device->hidden_raid_component = 0;
4945 _scsih_reprobe_target(sas_device->starget, 0);
4946}
4947
4948/**
4949 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4950 * @ioc: per adapter object
4951 * @element: IR config element data
4952 * Context: user.
4953 *
4954 * Return nothing.
4955 */
4956static void
4957_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4958 Mpi2EventIrConfigElement_t *element)
4959{
4960 struct _sas_device *sas_device;
4961 unsigned long flags;
4962 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4963
4964 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4965 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4966 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4967 if (!sas_device)
4968 return;
4969
4970 /* hiding raid component */
4971 mpt2sas_config_get_volume_handle(ioc, handle,
4972 &sas_device->volume_handle);
4973 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4974 &sas_device->volume_wwid);
4975 sas_device->hidden_raid_component = 1;
4976 _scsih_reprobe_target(sas_device->starget, 1);
4977}
4978
4979/**
4980 * _scsih_sas_pd_delete - delete pd component
4981 * @ioc: per adapter object
4982 * @element: IR config element data
4983 * Context: user.
4984 *
4985 * Return nothing.
4986 */
4987static void
4988_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4989 Mpi2EventIrConfigElement_t *element)
4990{
4991 struct _sas_device *sas_device;
4992 unsigned long flags;
4993 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4994
4995 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4996 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4997 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4998 if (!sas_device)
4999 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305000 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005001}
5002
5003/**
5004 * _scsih_sas_pd_add - remove pd component
5005 * @ioc: per adapter object
5006 * @element: IR config element data
5007 * Context: user.
5008 *
5009 * Return nothing.
5010 */
5011static void
5012_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5013 Mpi2EventIrConfigElement_t *element)
5014{
5015 struct _sas_device *sas_device;
5016 unsigned long flags;
5017 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305018 Mpi2ConfigReply_t mpi_reply;
5019 Mpi2SasDevicePage0_t sas_device_pg0;
5020 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305021 u64 sas_address;
5022 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005023
5024 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5025 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5026 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305027 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06005028 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305029 return;
5030 }
5031
5032 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5033 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5034 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5035 ioc->name, __FILE__, __LINE__, __func__);
5036 return;
5037 }
5038
5039 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5040 MPI2_IOCSTATUS_MASK;
5041 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5042 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5043 ioc->name, __FILE__, __LINE__, __func__);
5044 return;
5045 }
5046
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305047 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5048 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5049 mpt2sas_transport_update_links(ioc, sas_address, handle,
5050 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305051
5052 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005053}
5054
5055#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5056/**
5057 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5058 * @ioc: per adapter object
5059 * @event_data: event data payload
5060 * Context: user.
5061 *
5062 * Return nothing.
5063 */
5064static void
5065_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5066 Mpi2EventDataIrConfigChangeList_t *event_data)
5067{
5068 Mpi2EventIrConfigElement_t *element;
5069 u8 element_type;
5070 int i;
5071 char *reason_str = NULL, *element_str = NULL;
5072
5073 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5074
5075 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
5076 ioc->name, (le32_to_cpu(event_data->Flags) &
5077 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5078 "foreign" : "native", event_data->NumElements);
5079 for (i = 0; i < event_data->NumElements; i++, element++) {
5080 switch (element->ReasonCode) {
5081 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5082 reason_str = "add";
5083 break;
5084 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5085 reason_str = "remove";
5086 break;
5087 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5088 reason_str = "no change";
5089 break;
5090 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5091 reason_str = "hide";
5092 break;
5093 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5094 reason_str = "unhide";
5095 break;
5096 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5097 reason_str = "volume_created";
5098 break;
5099 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5100 reason_str = "volume_deleted";
5101 break;
5102 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5103 reason_str = "pd_created";
5104 break;
5105 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5106 reason_str = "pd_deleted";
5107 break;
5108 default:
5109 reason_str = "unknown reason";
5110 break;
5111 }
5112 element_type = le16_to_cpu(element->ElementFlags) &
5113 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5114 switch (element_type) {
5115 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5116 element_str = "volume";
5117 break;
5118 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5119 element_str = "phys disk";
5120 break;
5121 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5122 element_str = "hot spare";
5123 break;
5124 default:
5125 element_str = "unknown element";
5126 break;
5127 }
5128 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
5129 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5130 reason_str, le16_to_cpu(element->VolDevHandle),
5131 le16_to_cpu(element->PhysDiskDevHandle),
5132 element->PhysDiskNum);
5133 }
5134}
5135#endif
5136
5137/**
5138 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5139 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305140 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005141 * Context: user.
5142 *
5143 * Return nothing.
5144 */
5145static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305146_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5147 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005148{
5149 Mpi2EventIrConfigElement_t *element;
5150 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305151 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305152 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005153
5154#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5155 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5156 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5157
5158#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305159 foreign_config = (le32_to_cpu(event_data->Flags) &
5160 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005161
5162 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5163 for (i = 0; i < event_data->NumElements; i++, element++) {
5164
5165 switch (element->ReasonCode) {
5166 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5167 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305168 if (!foreign_config)
5169 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005170 break;
5171 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5172 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305173 if (!foreign_config)
5174 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005175 break;
5176 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5177 _scsih_sas_pd_hide(ioc, element);
5178 break;
5179 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5180 _scsih_sas_pd_expose(ioc, element);
5181 break;
5182 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5183 _scsih_sas_pd_add(ioc, element);
5184 break;
5185 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5186 _scsih_sas_pd_delete(ioc, element);
5187 break;
5188 }
5189 }
5190}
5191
5192/**
5193 * _scsih_sas_ir_volume_event - IR volume event
5194 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305195 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005196 * Context: user.
5197 *
5198 * Return nothing.
5199 */
5200static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305201_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5202 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005203{
5204 u64 wwid;
5205 unsigned long flags;
5206 struct _raid_device *raid_device;
5207 u16 handle;
5208 u32 state;
5209 int rc;
5210 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305211 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005212
5213 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5214 return;
5215
5216 handle = le16_to_cpu(event_data->VolDevHandle);
5217 state = le32_to_cpu(event_data->NewValue);
5218 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5219 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5220 le32_to_cpu(event_data->PreviousValue), state));
5221
5222 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5223 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5224 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5225
5226 switch (state) {
5227 case MPI2_RAID_VOL_STATE_MISSING:
5228 case MPI2_RAID_VOL_STATE_FAILED:
5229 if (!raid_device)
5230 break;
5231 if (raid_device->starget) {
5232 sas_target_priv_data = raid_device->starget->hostdata;
5233 sas_target_priv_data->deleted = 1;
5234 scsi_remove_target(&raid_device->starget->dev);
5235 }
5236 _scsih_raid_device_remove(ioc, raid_device);
5237 break;
5238
5239 case MPI2_RAID_VOL_STATE_ONLINE:
5240 case MPI2_RAID_VOL_STATE_DEGRADED:
5241 case MPI2_RAID_VOL_STATE_OPTIMAL:
5242 if (raid_device)
5243 break;
5244
5245 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5246 if (!wwid) {
5247 printk(MPT2SAS_ERR_FMT
5248 "failure at %s:%d/%s()!\n", ioc->name,
5249 __FILE__, __LINE__, __func__);
5250 break;
5251 }
5252
5253 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5254 if (!raid_device) {
5255 printk(MPT2SAS_ERR_FMT
5256 "failure at %s:%d/%s()!\n", ioc->name,
5257 __FILE__, __LINE__, __func__);
5258 break;
5259 }
5260
5261 raid_device->id = ioc->sas_id++;
5262 raid_device->channel = RAID_CHANNEL;
5263 raid_device->handle = handle;
5264 raid_device->wwid = wwid;
5265 _scsih_raid_device_add(ioc, raid_device);
5266 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5267 raid_device->id, 0);
5268 if (rc)
5269 _scsih_raid_device_remove(ioc, raid_device);
5270 break;
5271
5272 case MPI2_RAID_VOL_STATE_INITIALIZING:
5273 default:
5274 break;
5275 }
5276}
5277
5278/**
5279 * _scsih_sas_ir_physical_disk_event - PD event
5280 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305281 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005282 * Context: user.
5283 *
5284 * Return nothing.
5285 */
5286static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305287_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5288 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005289{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305290 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005291 u32 state;
5292 struct _sas_device *sas_device;
5293 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305294 Mpi2ConfigReply_t mpi_reply;
5295 Mpi2SasDevicePage0_t sas_device_pg0;
5296 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305297 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305298 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005299
5300 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5301 return;
5302
5303 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5304 state = le32_to_cpu(event_data->NewValue);
5305
5306 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5307 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5308 le32_to_cpu(event_data->PreviousValue), state));
5309
5310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5311 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5312 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5313
5314 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005315 case MPI2_RAID_PD_STATE_ONLINE:
5316 case MPI2_RAID_PD_STATE_DEGRADED:
5317 case MPI2_RAID_PD_STATE_REBUILDING:
5318 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305319 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06005320 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305321 return;
5322 }
5323
5324 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5325 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5326 handle))) {
5327 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5328 ioc->name, __FILE__, __LINE__, __func__);
5329 return;
5330 }
5331
5332 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5333 MPI2_IOCSTATUS_MASK;
5334 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5335 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5336 ioc->name, __FILE__, __LINE__, __func__);
5337 return;
5338 }
5339
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305340 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5341 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5342 mpt2sas_transport_update_links(ioc, sas_address, handle,
5343 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305344
5345 _scsih_add_device(ioc, handle, 0, 1);
5346
Eric Moore635374e2009-03-09 01:21:12 -06005347 break;
5348
Kashyap, Desai62727a72009-08-07 19:35:18 +05305349 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005350 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5351 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5352 case MPI2_RAID_PD_STATE_HOT_SPARE:
5353 default:
5354 break;
5355 }
5356}
5357
5358#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5359/**
5360 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5361 * @ioc: per adapter object
5362 * @event_data: event data payload
5363 * Context: user.
5364 *
5365 * Return nothing.
5366 */
5367static void
5368_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5369 Mpi2EventDataIrOperationStatus_t *event_data)
5370{
5371 char *reason_str = NULL;
5372
5373 switch (event_data->RAIDOperation) {
5374 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5375 reason_str = "resync";
5376 break;
5377 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5378 reason_str = "online capacity expansion";
5379 break;
5380 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5381 reason_str = "consistency check";
5382 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305383 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5384 reason_str = "background init";
5385 break;
5386 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5387 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005388 break;
5389 }
5390
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305391 if (!reason_str)
5392 return;
5393
Eric Moore635374e2009-03-09 01:21:12 -06005394 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5395 "\thandle(0x%04x), percent complete(%d)\n",
5396 ioc->name, reason_str,
5397 le16_to_cpu(event_data->VolDevHandle),
5398 event_data->PercentComplete);
5399}
5400#endif
5401
5402/**
5403 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5404 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305405 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005406 * Context: user.
5407 *
5408 * Return nothing.
5409 */
5410static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305411_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5412 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005413{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305414 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5415 static struct _raid_device *raid_device;
5416 unsigned long flags;
5417 u16 handle;
5418
Eric Moore635374e2009-03-09 01:21:12 -06005419#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5420 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305421 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305422 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005423#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305424
5425 /* code added for raid transport support */
5426 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5427
5428 handle = le16_to_cpu(event_data->VolDevHandle);
5429
5430 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5431 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5432 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5433
5434 if (!raid_device)
5435 return;
5436
5437 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5438 raid_device->percent_complete =
5439 event_data->PercentComplete;
5440 }
Eric Moore635374e2009-03-09 01:21:12 -06005441}
5442
5443/**
5444 * _scsih_task_set_full - handle task set full
5445 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305446 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005447 * Context: user.
5448 *
5449 * Throttle back qdepth.
5450 */
5451static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305452_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5453 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005454{
5455 unsigned long flags;
5456 struct _sas_device *sas_device;
5457 static struct _raid_device *raid_device;
5458 struct scsi_device *sdev;
5459 int depth;
5460 u16 current_depth;
5461 u16 handle;
5462 int id, channel;
5463 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305464 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005465
5466 current_depth = le16_to_cpu(event_data->CurrentDepth);
5467 handle = le16_to_cpu(event_data->DevHandle);
5468 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5469 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5470 if (!sas_device) {
5471 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5472 return;
5473 }
5474 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5475 id = sas_device->id;
5476 channel = sas_device->channel;
5477 sas_address = sas_device->sas_address;
5478
5479 /* if hidden raid component, then change to volume characteristics */
5480 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5481 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5482 raid_device = _scsih_raid_device_find_by_handle(
5483 ioc, sas_device->volume_handle);
5484 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5485 if (raid_device) {
5486 id = raid_device->id;
5487 channel = raid_device->channel;
5488 handle = raid_device->handle;
5489 sas_address = raid_device->wwid;
5490 }
5491 }
5492
5493 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5494 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5495 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5496 handle, (unsigned long long)sas_address, current_depth);
5497
5498 shost_for_each_device(sdev, ioc->shost) {
5499 if (sdev->id == id && sdev->channel == channel) {
5500 if (current_depth > sdev->queue_depth) {
5501 if (ioc->logging_level &
5502 MPT_DEBUG_TASK_SET_FULL)
5503 sdev_printk(KERN_INFO, sdev, "strange "
5504 "observation, the queue depth is"
5505 " (%d) meanwhile fw queue depth "
5506 "is (%d)\n", sdev->queue_depth,
5507 current_depth);
5508 continue;
5509 }
5510 depth = scsi_track_queue_full(sdev,
5511 current_depth - 1);
5512 if (depth > 0)
5513 sdev_printk(KERN_INFO, sdev, "Queue depth "
5514 "reduced to (%d)\n", depth);
5515 else if (depth < 0)
5516 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5517 "Queueing is being disabled\n");
5518 else if (depth == 0)
5519 if (ioc->logging_level &
5520 MPT_DEBUG_TASK_SET_FULL)
5521 sdev_printk(KERN_INFO, sdev,
5522 "Queue depth not changed yet\n");
5523 }
5524 }
5525}
5526
5527/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305528 * _scsih_prep_device_scan - initialize parameters prior to device scan
5529 * @ioc: per adapter object
5530 *
5531 * Set the deleted flag prior to device scan. If the device is found during
5532 * the scan, then we clear the deleted flag.
5533 */
5534static void
5535_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5536{
5537 struct MPT2SAS_DEVICE *sas_device_priv_data;
5538 struct scsi_device *sdev;
5539
5540 shost_for_each_device(sdev, ioc->shost) {
5541 sas_device_priv_data = sdev->hostdata;
5542 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5543 sas_device_priv_data->sas_target->deleted = 1;
5544 }
5545}
5546
5547/**
Eric Moore635374e2009-03-09 01:21:12 -06005548 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5549 * @ioc: per adapter object
5550 * @sas_address: sas address
5551 * @slot: enclosure slot id
5552 * @handle: device handle
5553 *
5554 * After host reset, find out whether devices are still responding.
5555 * Used in _scsi_remove_unresponsive_sas_devices.
5556 *
5557 * Return nothing.
5558 */
5559static void
5560_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5561 u16 slot, u16 handle)
5562{
5563 struct MPT2SAS_TARGET *sas_target_priv_data;
5564 struct scsi_target *starget;
5565 struct _sas_device *sas_device;
5566 unsigned long flags;
5567
5568 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5569 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5570 if (sas_device->sas_address == sas_address &&
5571 sas_device->slot == slot && sas_device->starget) {
5572 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305573 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305574 if (starget && starget->hostdata) {
5575 sas_target_priv_data = starget->hostdata;
5576 sas_target_priv_data->tm_busy = 0;
5577 sas_target_priv_data->deleted = 0;
5578 } else
5579 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005580 starget_printk(KERN_INFO, sas_device->starget,
5581 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5582 "logical id(0x%016llx), slot(%d)\n", handle,
5583 (unsigned long long)sas_device->sas_address,
5584 (unsigned long long)
5585 sas_device->enclosure_logical_id,
5586 sas_device->slot);
5587 if (sas_device->handle == handle)
5588 goto out;
5589 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5590 sas_device->handle);
5591 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305592 if (sas_target_priv_data)
5593 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005594 goto out;
5595 }
5596 }
5597 out:
5598 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5599}
5600
5601/**
5602 * _scsih_search_responding_sas_devices -
5603 * @ioc: per adapter object
5604 *
5605 * After host reset, find out whether devices are still responding.
5606 * If not remove.
5607 *
5608 * Return nothing.
5609 */
5610static void
5611_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5612{
5613 Mpi2SasDevicePage0_t sas_device_pg0;
5614 Mpi2ConfigReply_t mpi_reply;
5615 u16 ioc_status;
5616 __le64 sas_address;
5617 u16 handle;
5618 u32 device_info;
5619 u16 slot;
5620
5621 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5622
5623 if (list_empty(&ioc->sas_device_list))
5624 return;
5625
5626 handle = 0xFFFF;
5627 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5628 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5629 handle))) {
5630 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5631 MPI2_IOCSTATUS_MASK;
5632 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5633 break;
5634 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5635 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5636 if (!(_scsih_is_end_device(device_info)))
5637 continue;
5638 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5639 slot = le16_to_cpu(sas_device_pg0.Slot);
5640 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5641 handle);
5642 }
5643}
5644
5645/**
5646 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5647 * @ioc: per adapter object
5648 * @wwid: world wide identifier for raid volume
5649 * @handle: device handle
5650 *
5651 * After host reset, find out whether devices are still responding.
5652 * Used in _scsi_remove_unresponsive_raid_devices.
5653 *
5654 * Return nothing.
5655 */
5656static void
5657_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5658 u16 handle)
5659{
5660 struct MPT2SAS_TARGET *sas_target_priv_data;
5661 struct scsi_target *starget;
5662 struct _raid_device *raid_device;
5663 unsigned long flags;
5664
5665 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5666 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5667 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05305668 starget = raid_device->starget;
5669 if (starget && starget->hostdata) {
5670 sas_target_priv_data = starget->hostdata;
5671 sas_target_priv_data->deleted = 0;
5672 } else
5673 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005674 raid_device->responding = 1;
5675 starget_printk(KERN_INFO, raid_device->starget,
5676 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5677 (unsigned long long)raid_device->wwid);
5678 if (raid_device->handle == handle)
5679 goto out;
5680 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5681 raid_device->handle);
5682 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305683 if (sas_target_priv_data)
5684 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005685 goto out;
5686 }
5687 }
5688 out:
5689 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5690}
5691
5692/**
5693 * _scsih_search_responding_raid_devices -
5694 * @ioc: per adapter object
5695 *
5696 * After host reset, find out whether devices are still responding.
5697 * If not remove.
5698 *
5699 * Return nothing.
5700 */
5701static void
5702_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5703{
5704 Mpi2RaidVolPage1_t volume_pg1;
5705 Mpi2ConfigReply_t mpi_reply;
5706 u16 ioc_status;
5707 u16 handle;
5708
5709 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5710
5711 if (list_empty(&ioc->raid_device_list))
5712 return;
5713
5714 handle = 0xFFFF;
5715 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5716 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5717 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5718 MPI2_IOCSTATUS_MASK;
5719 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5720 break;
5721 handle = le16_to_cpu(volume_pg1.DevHandle);
5722 _scsih_mark_responding_raid_device(ioc,
5723 le64_to_cpu(volume_pg1.WWID), handle);
5724 }
5725}
5726
5727/**
5728 * _scsih_mark_responding_expander - mark a expander as responding
5729 * @ioc: per adapter object
5730 * @sas_address: sas address
5731 * @handle:
5732 *
5733 * After host reset, find out whether devices are still responding.
5734 * Used in _scsi_remove_unresponsive_expanders.
5735 *
5736 * Return nothing.
5737 */
5738static void
5739_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5740 u16 handle)
5741{
5742 struct _sas_node *sas_expander;
5743 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305744 int i;
Eric Moore635374e2009-03-09 01:21:12 -06005745
5746 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5747 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305748 if (sas_expander->sas_address != sas_address)
5749 continue;
5750 sas_expander->responding = 1;
5751 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06005752 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305753 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5754 " from(0x%04x) to (0x%04x)!!!\n",
5755 (unsigned long long)sas_expander->sas_address,
5756 sas_expander->handle, handle);
5757 sas_expander->handle = handle;
5758 for (i = 0 ; i < sas_expander->num_phys ; i++)
5759 sas_expander->phy[i].handle = handle;
5760 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005761 }
5762 out:
5763 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5764}
5765
5766/**
5767 * _scsih_search_responding_expanders -
5768 * @ioc: per adapter object
5769 *
5770 * After host reset, find out whether devices are still responding.
5771 * If not remove.
5772 *
5773 * Return nothing.
5774 */
5775static void
5776_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5777{
5778 Mpi2ExpanderPage0_t expander_pg0;
5779 Mpi2ConfigReply_t mpi_reply;
5780 u16 ioc_status;
5781 __le64 sas_address;
5782 u16 handle;
5783
5784 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5785
5786 if (list_empty(&ioc->sas_expander_list))
5787 return;
5788
5789 handle = 0xFFFF;
5790 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5791 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5792
5793 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5794 MPI2_IOCSTATUS_MASK;
5795 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5796 break;
5797
5798 handle = le16_to_cpu(expander_pg0.DevHandle);
5799 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5800 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5801 "sas_addr(0x%016llx)\n", handle,
5802 (unsigned long long)sas_address);
5803 _scsih_mark_responding_expander(ioc, sas_address, handle);
5804 }
5805
5806}
5807
5808/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305809 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06005810 * @ioc: per adapter object
5811 *
5812 * Return nothing.
5813 */
5814static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305815_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06005816{
5817 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305818 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005819 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005820
Eric Moore635374e2009-03-09 01:21:12 -06005821
5822 list_for_each_entry_safe(sas_device, sas_device_next,
5823 &ioc->sas_device_list, list) {
5824 if (sas_device->responding) {
5825 sas_device->responding = 0;
5826 continue;
5827 }
5828 if (sas_device->starget)
5829 starget_printk(KERN_INFO, sas_device->starget,
5830 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5831 "enclosure logical id(0x%016llx), slot(%d)\n",
5832 sas_device->handle,
5833 (unsigned long long)sas_device->sas_address,
5834 (unsigned long long)
5835 sas_device->enclosure_logical_id,
5836 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305837 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005838 }
5839
5840 list_for_each_entry_safe(raid_device, raid_device_next,
5841 &ioc->raid_device_list, list) {
5842 if (raid_device->responding) {
5843 raid_device->responding = 0;
5844 continue;
5845 }
5846 if (raid_device->starget) {
5847 starget_printk(KERN_INFO, raid_device->starget,
5848 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5849 raid_device->handle,
5850 (unsigned long long)raid_device->wwid);
5851 scsi_remove_target(&raid_device->starget->dev);
5852 }
5853 _scsih_raid_device_remove(ioc, raid_device);
5854 }
5855
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305856 retry_expander_search:
5857 sas_expander = NULL;
5858 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005859 if (sas_expander->responding) {
5860 sas_expander->responding = 0;
5861 continue;
5862 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305863 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305864 goto retry_expander_search;
5865 }
5866}
5867
5868/**
5869 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5870 * @ioc: per adapter object
5871 * @reset_phase: phase
5872 *
5873 * The handler for doing any required cleanup or initialization.
5874 *
5875 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5876 * MPT2_IOC_DONE_RESET
5877 *
5878 * Return nothing.
5879 */
5880void
5881mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5882{
5883 switch (reset_phase) {
5884 case MPT2_IOC_PRE_RESET:
5885 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5886 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305887 break;
5888 case MPT2_IOC_AFTER_RESET:
5889 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5890 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305891 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
5892 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
5893 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
5894 complete(&ioc->scsih_cmds.done);
5895 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305896 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5897 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5898 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5899 complete(&ioc->tm_cmds.done);
5900 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305901 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305902 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305903 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305904 break;
5905 case MPT2_IOC_DONE_RESET:
5906 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5907 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305908 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05305909 _scsih_prep_device_scan(ioc);
5910 _scsih_search_responding_sas_devices(ioc);
5911 _scsih_search_responding_raid_devices(ioc);
5912 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305913 break;
Eric Moore635374e2009-03-09 01:21:12 -06005914 }
5915}
5916
5917/**
5918 * _firmware_event_work - delayed task for processing firmware events
5919 * @ioc: per adapter object
5920 * @work: equal to the fw_event_work object
5921 * Context: user.
5922 *
5923 * Return nothing.
5924 */
5925static void
5926_firmware_event_work(struct work_struct *work)
5927{
5928 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305929 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06005930 unsigned long flags;
5931 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5932
Eric Moore635374e2009-03-09 01:21:12 -06005933 /* the queue is being flushed so ignore this event */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305934 if (ioc->remove_host || fw_event->cancel_pending_work) {
Eric Moore635374e2009-03-09 01:21:12 -06005935 _scsih_fw_event_free(ioc, fw_event);
5936 return;
5937 }
Eric Moore635374e2009-03-09 01:21:12 -06005938
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305939 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
5940 _scsih_fw_event_free(ioc, fw_event);
5941 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5942 if (ioc->shost_recovery) {
5943 init_completion(&ioc->shost_recovery_done);
5944 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5945 flags);
5946 wait_for_completion(&ioc->shost_recovery_done);
5947 } else
5948 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
5949 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05305950 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005951 return;
5952 }
Eric Moore635374e2009-03-09 01:21:12 -06005953
5954 switch (fw_event->event) {
5955 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305956 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005957 break;
5958 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305959 _scsih_sas_device_status_change_event(ioc,
5960 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005961 break;
5962 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305963 _scsih_sas_discovery_event(ioc,
5964 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005965 break;
5966 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305967 _scsih_sas_broadcast_primative_event(ioc,
5968 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005969 break;
5970 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5971 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305972 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005973 break;
5974 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305975 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005976 break;
5977 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305978 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005979 break;
5980 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305981 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005982 break;
5983 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305984 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005985 break;
5986 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305987 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005988 break;
5989 }
5990 _scsih_fw_event_free(ioc, fw_event);
5991}
5992
5993/**
5994 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5995 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305996 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06005997 * @reply: reply message frame(lower 32bit addr)
5998 * Context: interrupt.
5999 *
6000 * This function merely adds a new work task into ioc->firmware_event_thread.
6001 * The tasks are worked from _firmware_event_work in user context.
6002 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306003 * Return 1 meaning mf should be freed from _base_interrupt
6004 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006005 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306006u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306007mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6008 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006009{
6010 struct fw_event_work *fw_event;
6011 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006012 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306013 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006014
6015 /* events turned off due to host reset or driver unloading */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306016 if (ioc->remove_host)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306017 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006018
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306019 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006020 event = le16_to_cpu(mpi_reply->Event);
6021
6022 switch (event) {
6023 /* handle these */
6024 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6025 {
6026 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6027 (Mpi2EventDataSasBroadcastPrimitive_t *)
6028 mpi_reply->EventData;
6029
6030 if (baen_data->Primitive !=
6031 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6032 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306033 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006034 ioc->broadcast_aen_busy = 1;
6035 break;
6036 }
6037
6038 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6039 _scsih_check_topo_delete_events(ioc,
6040 (Mpi2EventDataSasTopologyChangeList_t *)
6041 mpi_reply->EventData);
6042 break;
6043
6044 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6045 case MPI2_EVENT_IR_OPERATION_STATUS:
6046 case MPI2_EVENT_SAS_DISCOVERY:
6047 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6048 case MPI2_EVENT_IR_VOLUME:
6049 case MPI2_EVENT_IR_PHYSICAL_DISK:
6050 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6051 case MPI2_EVENT_TASK_SET_FULL:
6052 break;
6053
6054 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306055 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006056 }
6057
6058 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6059 if (!fw_event) {
6060 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6061 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306062 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006063 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306064 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6065 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006066 if (!fw_event->event_data) {
6067 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6068 ioc->name, __FILE__, __LINE__, __func__);
6069 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306070 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006071 }
6072
6073 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306074 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006075 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306076 fw_event->VF_ID = mpi_reply->VF_ID;
6077 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006078 fw_event->event = event;
6079 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306080 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006081}
6082
6083/* shost template */
6084static struct scsi_host_template scsih_driver_template = {
6085 .module = THIS_MODULE,
6086 .name = "Fusion MPT SAS Host",
6087 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006088 .queuecommand = _scsih_qcmd,
6089 .target_alloc = _scsih_target_alloc,
6090 .slave_alloc = _scsih_slave_alloc,
6091 .slave_configure = _scsih_slave_configure,
6092 .target_destroy = _scsih_target_destroy,
6093 .slave_destroy = _scsih_slave_destroy,
6094 .change_queue_depth = _scsih_change_queue_depth,
6095 .change_queue_type = _scsih_change_queue_type,
6096 .eh_abort_handler = _scsih_abort,
6097 .eh_device_reset_handler = _scsih_dev_reset,
6098 .eh_target_reset_handler = _scsih_target_reset,
6099 .eh_host_reset_handler = _scsih_host_reset,
6100 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006101 .can_queue = 1,
6102 .this_id = -1,
6103 .sg_tablesize = MPT2SAS_SG_DEPTH,
6104 .max_sectors = 8192,
6105 .cmd_per_lun = 7,
6106 .use_clustering = ENABLE_CLUSTERING,
6107 .shost_attrs = mpt2sas_host_attrs,
6108 .sdev_attrs = mpt2sas_dev_attrs,
6109};
6110
6111/**
6112 * _scsih_expander_node_remove - removing expander device from list.
6113 * @ioc: per adapter object
6114 * @sas_expander: the sas_device object
6115 * Context: Calling function should acquire ioc->sas_node_lock.
6116 *
6117 * Removing object and freeing associated memory from the
6118 * ioc->sas_expander_list.
6119 *
6120 * Return nothing.
6121 */
6122static void
6123_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6124 struct _sas_node *sas_expander)
6125{
6126 struct _sas_port *mpt2sas_port;
6127 struct _sas_device *sas_device;
6128 struct _sas_node *expander_sibling;
6129 unsigned long flags;
6130
6131 if (!sas_expander)
6132 return;
6133
6134 /* remove sibling ports attached to this expander */
6135 retry_device_search:
6136 list_for_each_entry(mpt2sas_port,
6137 &sas_expander->sas_port_list, port_list) {
6138 if (mpt2sas_port->remote_identify.device_type ==
6139 SAS_END_DEVICE) {
6140 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6141 sas_device =
6142 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6143 mpt2sas_port->remote_identify.sas_address);
6144 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6145 if (!sas_device)
6146 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306147 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306148 if (ioc->shost_recovery)
6149 return;
Eric Moore635374e2009-03-09 01:21:12 -06006150 goto retry_device_search;
6151 }
6152 }
6153
6154 retry_expander_search:
6155 list_for_each_entry(mpt2sas_port,
6156 &sas_expander->sas_port_list, port_list) {
6157
6158 if (mpt2sas_port->remote_identify.device_type ==
6159 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6160 mpt2sas_port->remote_identify.device_type ==
6161 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6162
6163 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6164 expander_sibling =
6165 mpt2sas_scsih_expander_find_by_sas_address(
6166 ioc, mpt2sas_port->remote_identify.sas_address);
6167 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6168 if (!expander_sibling)
6169 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306170 _scsih_expander_remove(ioc,
6171 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306172 if (ioc->shost_recovery)
6173 return;
Eric Moore635374e2009-03-09 01:21:12 -06006174 goto retry_expander_search;
6175 }
6176 }
6177
6178 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306179 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006180
6181 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6182 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6183 sas_expander->handle, (unsigned long long)
6184 sas_expander->sas_address);
6185
6186 list_del(&sas_expander->list);
6187 kfree(sas_expander->phy);
6188 kfree(sas_expander);
6189}
6190
6191/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306192 * _scsih_ir_shutdown - IR shutdown notification
6193 * @ioc: per adapter object
6194 *
6195 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6196 * the host system is shutting down.
6197 *
6198 * Return nothing.
6199 */
6200static void
6201_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6202{
6203 Mpi2RaidActionRequest_t *mpi_request;
6204 Mpi2RaidActionReply_t *mpi_reply;
6205 u16 smid;
6206
6207 /* is IR firmware build loaded ? */
6208 if (!ioc->ir_firmware)
6209 return;
6210
6211 /* are there any volumes ? */
6212 if (list_empty(&ioc->raid_device_list))
6213 return;
6214
6215 mutex_lock(&ioc->scsih_cmds.mutex);
6216
6217 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6218 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6219 ioc->name, __func__);
6220 goto out;
6221 }
6222 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6223
6224 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6225 if (!smid) {
6226 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6227 ioc->name, __func__);
6228 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6229 goto out;
6230 }
6231
6232 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6233 ioc->scsih_cmds.smid = smid;
6234 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6235
6236 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6237 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6238
6239 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6240 init_completion(&ioc->scsih_cmds.done);
6241 mpt2sas_base_put_smid_default(ioc, smid);
6242 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6243
6244 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6245 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6246 ioc->name, __func__);
6247 goto out;
6248 }
6249
6250 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6251 mpi_reply = ioc->scsih_cmds.reply;
6252
6253 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6254 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6255 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6256 le32_to_cpu(mpi_reply->IOCLogInfo));
6257 }
6258
6259 out:
6260 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6261 mutex_unlock(&ioc->scsih_cmds.mutex);
6262}
6263
6264/**
6265 * _scsih_shutdown - routine call during system shutdown
6266 * @pdev: PCI device struct
6267 *
6268 * Return nothing.
6269 */
6270static void
6271_scsih_shutdown(struct pci_dev *pdev)
6272{
6273 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6274 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306275 struct workqueue_struct *wq;
6276 unsigned long flags;
6277
6278 ioc->remove_host = 1;
6279 _scsih_fw_event_cleanup_queue(ioc);
6280
6281 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6282 wq = ioc->firmware_event_thread;
6283 ioc->firmware_event_thread = NULL;
6284 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6285 if (wq)
6286 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306287
6288 _scsih_ir_shutdown(ioc);
6289 mpt2sas_base_detach(ioc);
6290}
6291
6292/**
Eric Moored5d135b2009-05-18 13:02:08 -06006293 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006294 * @pdev: PCI device struct
6295 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306296 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006297 * Return nothing.
6298 */
6299static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006300_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006301{
6302 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6303 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6304 struct _sas_port *mpt2sas_port;
6305 struct _sas_device *sas_device;
6306 struct _sas_node *expander_sibling;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306307 struct _raid_device *raid_device, *next;
6308 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006309 struct workqueue_struct *wq;
6310 unsigned long flags;
6311
6312 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306313 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006314
6315 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6316 wq = ioc->firmware_event_thread;
6317 ioc->firmware_event_thread = NULL;
6318 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6319 if (wq)
6320 destroy_workqueue(wq);
6321
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306322 /* release all the volumes */
6323 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6324 list) {
6325 if (raid_device->starget) {
6326 sas_target_priv_data =
6327 raid_device->starget->hostdata;
6328 sas_target_priv_data->deleted = 1;
6329 scsi_remove_target(&raid_device->starget->dev);
6330 }
6331 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6332 "(0x%016llx)\n", ioc->name, raid_device->handle,
6333 (unsigned long long) raid_device->wwid);
6334 _scsih_raid_device_remove(ioc, raid_device);
6335 }
6336
Eric Moore635374e2009-03-09 01:21:12 -06006337 /* free ports attached to the sas_host */
6338 retry_again:
6339 list_for_each_entry(mpt2sas_port,
6340 &ioc->sas_hba.sas_port_list, port_list) {
6341 if (mpt2sas_port->remote_identify.device_type ==
6342 SAS_END_DEVICE) {
6343 sas_device =
6344 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6345 mpt2sas_port->remote_identify.sas_address);
6346 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306347 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006348 goto retry_again;
6349 }
6350 } else {
6351 expander_sibling =
6352 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6353 mpt2sas_port->remote_identify.sas_address);
6354 if (expander_sibling) {
6355 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306356 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006357 goto retry_again;
6358 }
6359 }
6360 }
6361
6362 /* free phys attached to the sas_host */
6363 if (ioc->sas_hba.num_phys) {
6364 kfree(ioc->sas_hba.phy);
6365 ioc->sas_hba.phy = NULL;
6366 ioc->sas_hba.num_phys = 0;
6367 }
6368
6369 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306370 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006371 list_del(&ioc->list);
6372 scsi_remove_host(shost);
6373 scsi_host_put(shost);
6374}
6375
6376/**
6377 * _scsih_probe_boot_devices - reports 1st device
6378 * @ioc: per adapter object
6379 *
6380 * If specified in bios page 2, this routine reports the 1st
6381 * device scsi-ml or sas transport for persistent boot device
6382 * purposes. Please refer to function _scsih_determine_boot_device()
6383 */
6384static void
6385_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6386{
6387 u8 is_raid;
6388 void *device;
6389 struct _sas_device *sas_device;
6390 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306391 u16 handle;
6392 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006393 u64 sas_address;
6394 unsigned long flags;
6395 int rc;
6396
6397 device = NULL;
6398 if (ioc->req_boot_device.device) {
6399 device = ioc->req_boot_device.device;
6400 is_raid = ioc->req_boot_device.is_raid;
6401 } else if (ioc->req_alt_boot_device.device) {
6402 device = ioc->req_alt_boot_device.device;
6403 is_raid = ioc->req_alt_boot_device.is_raid;
6404 } else if (ioc->current_boot_device.device) {
6405 device = ioc->current_boot_device.device;
6406 is_raid = ioc->current_boot_device.is_raid;
6407 }
6408
6409 if (!device)
6410 return;
6411
6412 if (is_raid) {
6413 raid_device = device;
6414 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6415 raid_device->id, 0);
6416 if (rc)
6417 _scsih_raid_device_remove(ioc, raid_device);
6418 } else {
6419 sas_device = device;
6420 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306421 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006422 sas_address = sas_device->sas_address;
6423 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6424 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6425 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6426 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306427 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006428 _scsih_sas_device_remove(ioc, sas_device);
6429 } else if (!sas_device->starget) {
6430 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306431 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006432 _scsih_sas_device_remove(ioc, sas_device);
6433 }
6434 }
6435}
6436
6437/**
6438 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6439 * @ioc: per adapter object
6440 *
6441 * Called during initial loading of the driver.
6442 */
6443static void
6444_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6445{
6446 struct _raid_device *raid_device, *raid_next;
6447 int rc;
6448
6449 list_for_each_entry_safe(raid_device, raid_next,
6450 &ioc->raid_device_list, list) {
6451 if (raid_device->starget)
6452 continue;
6453 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6454 raid_device->id, 0);
6455 if (rc)
6456 _scsih_raid_device_remove(ioc, raid_device);
6457 }
6458}
6459
6460/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306461 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006462 * @ioc: per adapter object
6463 *
6464 * Called during initial loading of the driver.
6465 */
6466static void
6467_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6468{
6469 struct _sas_device *sas_device, *next;
6470 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006471
6472 /* SAS Device List */
6473 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6474 list) {
6475 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6476 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6477 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6478
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306479 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6480 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006481 _scsih_sas_device_remove(ioc, sas_device);
6482 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306483 mpt2sas_transport_port_remove(ioc,
6484 sas_device->sas_address,
6485 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006486 _scsih_sas_device_remove(ioc, sas_device);
6487 }
6488 }
6489}
6490
6491/**
6492 * _scsih_probe_devices - probing for devices
6493 * @ioc: per adapter object
6494 *
6495 * Called during initial loading of the driver.
6496 */
6497static void
6498_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6499{
6500 u16 volume_mapping_flags =
6501 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6502 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6503
6504 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6505 return; /* return when IOC doesn't support initiator mode */
6506
6507 _scsih_probe_boot_devices(ioc);
6508
6509 if (ioc->ir_firmware) {
6510 if ((volume_mapping_flags &
6511 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6512 _scsih_probe_sas(ioc);
6513 _scsih_probe_raid(ioc);
6514 } else {
6515 _scsih_probe_raid(ioc);
6516 _scsih_probe_sas(ioc);
6517 }
6518 } else
6519 _scsih_probe_sas(ioc);
6520}
6521
6522/**
Eric Moored5d135b2009-05-18 13:02:08 -06006523 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006524 * @pdev: PCI device struct
6525 * @id: pci device id
6526 *
6527 * Returns 0 success, anything else error.
6528 */
6529static int
Eric Moored5d135b2009-05-18 13:02:08 -06006530_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006531{
6532 struct MPT2SAS_ADAPTER *ioc;
6533 struct Scsi_Host *shost;
6534
6535 shost = scsi_host_alloc(&scsih_driver_template,
6536 sizeof(struct MPT2SAS_ADAPTER));
6537 if (!shost)
6538 return -ENODEV;
6539
6540 /* init local params */
6541 ioc = shost_priv(shost);
6542 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6543 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006544 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006545 ioc->shost = shost;
6546 ioc->id = mpt_ids++;
6547 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6548 ioc->pdev = pdev;
6549 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6550 ioc->tm_cb_idx = tm_cb_idx;
6551 ioc->ctl_cb_idx = ctl_cb_idx;
6552 ioc->base_cb_idx = base_cb_idx;
6553 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306554 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006555 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306556 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6557 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006558 ioc->logging_level = logging_level;
6559 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05306560 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006561 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6562 spin_lock_init(&ioc->scsi_lookup_lock);
6563 spin_lock_init(&ioc->sas_device_lock);
6564 spin_lock_init(&ioc->sas_node_lock);
6565 spin_lock_init(&ioc->fw_event_lock);
6566 spin_lock_init(&ioc->raid_device_lock);
6567
6568 INIT_LIST_HEAD(&ioc->sas_device_list);
6569 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6570 INIT_LIST_HEAD(&ioc->sas_expander_list);
6571 INIT_LIST_HEAD(&ioc->fw_event_list);
6572 INIT_LIST_HEAD(&ioc->raid_device_list);
6573 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306574 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Eric Moore635374e2009-03-09 01:21:12 -06006575
6576 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06006577 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06006578 shost->max_lun = max_lun;
6579 shost->transportt = mpt2sas_transport_template;
6580 shost->unique_id = ioc->id;
6581
6582 if ((scsi_add_host(shost, &pdev->dev))) {
6583 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6584 ioc->name, __FILE__, __LINE__, __func__);
6585 list_del(&ioc->list);
6586 goto out_add_shost_fail;
6587 }
6588
Eric Moore3c621b32009-05-18 12:59:41 -06006589 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06006590 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306591 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006592
Eric Moore635374e2009-03-09 01:21:12 -06006593 /* event thread */
6594 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6595 "fw_event%d", ioc->id);
6596 ioc->firmware_event_thread = create_singlethread_workqueue(
6597 ioc->firmware_event_name);
6598 if (!ioc->firmware_event_thread) {
6599 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6600 ioc->name, __FILE__, __LINE__, __func__);
6601 goto out_thread_fail;
6602 }
6603
6604 ioc->wait_for_port_enable_to_complete = 1;
6605 if ((mpt2sas_base_attach(ioc))) {
6606 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6607 ioc->name, __FILE__, __LINE__, __func__);
6608 goto out_attach_fail;
6609 }
6610
6611 ioc->wait_for_port_enable_to_complete = 0;
6612 _scsih_probe_devices(ioc);
6613 return 0;
6614
6615 out_attach_fail:
6616 destroy_workqueue(ioc->firmware_event_thread);
6617 out_thread_fail:
6618 list_del(&ioc->list);
6619 scsi_remove_host(shost);
6620 out_add_shost_fail:
6621 return -ENODEV;
6622}
6623
6624#ifdef CONFIG_PM
6625/**
Eric Moored5d135b2009-05-18 13:02:08 -06006626 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006627 * @pdev: PCI device struct
6628 * @state: PM state change to (usually PCI_D3)
6629 *
6630 * Returns 0 success, anything else error.
6631 */
6632static int
Eric Moored5d135b2009-05-18 13:02:08 -06006633_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006634{
6635 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6636 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6637 u32 device_state;
6638
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306639 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006640 flush_scheduled_work();
6641 scsi_block_requests(shost);
6642 device_state = pci_choose_state(pdev, state);
6643 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6644 "operating state [D%d]\n", ioc->name, pdev,
6645 pci_name(pdev), device_state);
6646
6647 mpt2sas_base_free_resources(ioc);
6648 pci_save_state(pdev);
6649 pci_disable_device(pdev);
6650 pci_set_power_state(pdev, device_state);
6651 return 0;
6652}
6653
6654/**
Eric Moored5d135b2009-05-18 13:02:08 -06006655 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006656 * @pdev: PCI device struct
6657 *
6658 * Returns 0 success, anything else error.
6659 */
6660static int
Eric Moored5d135b2009-05-18 13:02:08 -06006661_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006662{
6663 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6664 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6665 u32 device_state = pdev->current_state;
6666 int r;
6667
6668 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6669 "operating state [D%d]\n", ioc->name, pdev,
6670 pci_name(pdev), device_state);
6671
6672 pci_set_power_state(pdev, PCI_D0);
6673 pci_enable_wake(pdev, PCI_D0, 0);
6674 pci_restore_state(pdev);
6675 ioc->pdev = pdev;
6676 r = mpt2sas_base_map_resources(ioc);
6677 if (r)
6678 return r;
6679
6680 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6681 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306682 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006683 return 0;
6684}
6685#endif /* CONFIG_PM */
6686
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05306687/**
6688 * _scsih_pci_error_detected - Called when a PCI error is detected.
6689 * @pdev: PCI device struct
6690 * @state: PCI channel state
6691 *
6692 * Description: Called when a PCI error is detected.
6693 *
6694 * Return value:
6695 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
6696 */
6697static pci_ers_result_t
6698_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6699{
6700 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6701 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6702
6703 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
6704 ioc->name, state);
6705
6706 switch (state) {
6707 case pci_channel_io_normal:
6708 return PCI_ERS_RESULT_CAN_RECOVER;
6709 case pci_channel_io_frozen:
6710 scsi_block_requests(ioc->shost);
6711 mpt2sas_base_stop_watchdog(ioc);
6712 mpt2sas_base_free_resources(ioc);
6713 return PCI_ERS_RESULT_NEED_RESET;
6714 case pci_channel_io_perm_failure:
6715 _scsih_remove(pdev);
6716 return PCI_ERS_RESULT_DISCONNECT;
6717 }
6718 return PCI_ERS_RESULT_NEED_RESET;
6719}
6720
6721/**
6722 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
6723 * @pdev: PCI device struct
6724 *
6725 * Description: This routine is called by the pci error recovery
6726 * code after the PCI slot has been reset, just before we
6727 * should resume normal operations.
6728 */
6729static pci_ers_result_t
6730_scsih_pci_slot_reset(struct pci_dev *pdev)
6731{
6732 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6733 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6734 int rc;
6735
6736 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
6737 ioc->name);
6738
6739 ioc->pdev = pdev;
6740 rc = mpt2sas_base_map_resources(ioc);
6741 if (rc)
6742 return PCI_ERS_RESULT_DISCONNECT;
6743
6744
6745 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6746 FORCE_BIG_HAMMER);
6747
6748 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
6749 (rc == 0) ? "success" : "failed");
6750
6751 if (!rc)
6752 return PCI_ERS_RESULT_RECOVERED;
6753 else
6754 return PCI_ERS_RESULT_DISCONNECT;
6755}
6756
6757/**
6758 * _scsih_pci_resume() - resume normal ops after PCI reset
6759 * @pdev: pointer to PCI device
6760 *
6761 * Called when the error recovery driver tells us that its
6762 * OK to resume normal operation. Use completion to allow
6763 * halted scsi ops to resume.
6764 */
6765static void
6766_scsih_pci_resume(struct pci_dev *pdev)
6767{
6768 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6769 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6770
6771 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
6772
6773 pci_cleanup_aer_uncorrect_error_status(pdev);
6774 mpt2sas_base_start_watchdog(ioc);
6775 scsi_unblock_requests(ioc->shost);
6776}
6777
6778/**
6779 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
6780 * @pdev: pointer to PCI device
6781 */
6782static pci_ers_result_t
6783_scsih_pci_mmio_enabled(struct pci_dev *pdev)
6784{
6785 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6786 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6787
6788 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
6789 ioc->name);
6790
6791 /* TODO - dump whatever for debugging purposes */
6792
6793 /* Request a slot reset. */
6794 return PCI_ERS_RESULT_NEED_RESET;
6795}
6796
6797static struct pci_error_handlers _scsih_err_handler = {
6798 .error_detected = _scsih_pci_error_detected,
6799 .mmio_enabled = _scsih_pci_mmio_enabled,
6800 .slot_reset = _scsih_pci_slot_reset,
6801 .resume = _scsih_pci_resume,
6802};
Eric Moore635374e2009-03-09 01:21:12 -06006803
6804static struct pci_driver scsih_driver = {
6805 .name = MPT2SAS_DRIVER_NAME,
6806 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06006807 .probe = _scsih_probe,
6808 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05306809 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05306810 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06006811#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06006812 .suspend = _scsih_suspend,
6813 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06006814#endif
6815};
6816
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306817/* raid transport support */
6818static struct raid_function_template mpt2sas_raid_functions = {
6819 .cookie = &scsih_driver_template,
6820 .is_raid = _scsih_is_raid,
6821 .get_resync = _scsih_get_resync,
6822 .get_state = _scsih_get_state,
6823};
Eric Moore635374e2009-03-09 01:21:12 -06006824
6825/**
Eric Moored5d135b2009-05-18 13:02:08 -06006826 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06006827 *
6828 * Returns 0 success, anything else error.
6829 */
6830static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06006831_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06006832{
6833 int error;
6834
6835 mpt_ids = 0;
6836 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6837 MPT2SAS_DRIVER_VERSION);
6838
6839 mpt2sas_transport_template =
6840 sas_attach_transport(&mpt2sas_transport_functions);
6841 if (!mpt2sas_transport_template)
6842 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306843 /* raid transport support */
6844 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
6845 if (!mpt2sas_raid_template) {
6846 sas_release_transport(mpt2sas_transport_template);
6847 return -ENODEV;
6848 }
Eric Moore635374e2009-03-09 01:21:12 -06006849
6850 mpt2sas_base_initialize_callback_handler();
6851
6852 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06006853 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06006854
6855 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06006856 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06006857
6858 /* base internal commands callback handler */
6859 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6860
6861 /* transport internal commands callback handler */
6862 transport_cb_idx = mpt2sas_base_register_callback_handler(
6863 mpt2sas_transport_done);
6864
Kashyap, Desai744090d2009-10-05 15:56:56 +05306865 /* scsih internal commands callback handler */
6866 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
6867
Eric Moore635374e2009-03-09 01:21:12 -06006868 /* configuration page API internal commands callback handler */
6869 config_cb_idx = mpt2sas_base_register_callback_handler(
6870 mpt2sas_config_done);
6871
6872 /* ctl module callback handler */
6873 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6874
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306875 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6876 _scsih_tm_tr_complete);
6877 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6878 _scsih_sas_control_complete);
6879
Eric Moore635374e2009-03-09 01:21:12 -06006880 mpt2sas_ctl_init();
6881
6882 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306883 if (error) {
6884 /* raid transport support */
6885 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06006886 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306887 }
Eric Moore635374e2009-03-09 01:21:12 -06006888
6889 return error;
6890}
6891
6892/**
Eric Moored5d135b2009-05-18 13:02:08 -06006893 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06006894 *
6895 * Returns 0 success, anything else error.
6896 */
6897static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06006898_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06006899{
6900 printk(KERN_INFO "mpt2sas version %s unloading\n",
6901 MPT2SAS_DRIVER_VERSION);
6902
6903 pci_unregister_driver(&scsih_driver);
6904
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306905 mpt2sas_ctl_exit();
6906
Eric Moore635374e2009-03-09 01:21:12 -06006907 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6908 mpt2sas_base_release_callback_handler(tm_cb_idx);
6909 mpt2sas_base_release_callback_handler(base_cb_idx);
6910 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306911 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06006912 mpt2sas_base_release_callback_handler(config_cb_idx);
6913 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6914
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306915 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6916 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6917
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306918 /* raid transport support */
6919 raid_class_release(mpt2sas_raid_template);
6920 sas_release_transport(mpt2sas_transport_template);
6921
Eric Moore635374e2009-03-09 01:21:12 -06006922}
6923
Eric Moored5d135b2009-05-18 13:02:08 -06006924module_init(_scsih_init);
6925module_exit(_scsih_exit);