blob: 8dc682f00fd26c005476845d140c87f52a1ce384 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai19d3ebe2009-09-14 11:01:36 +05305 * Copyright (C) 2007-2009 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060071LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060072
73/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060074static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053079static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060080static u8 config_cb_idx = -1;
81static int mpt_ids;
82
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053083static u8 tm_tr_cb_idx = -1 ;
84static u8 tm_sas_control_cb_idx = -1;
85
Eric Moore635374e2009-03-09 01:21:12 -060086/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060087static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060088MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
89 "(default=0)");
90
91/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
92#define MPT2SAS_MAX_LUN (16895)
93static int max_lun = MPT2SAS_MAX_LUN;
94module_param(max_lun, int, 0);
95MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
96
97/**
98 * struct sense_info - common structure for obtaining sense keys
99 * @skey: sense key
100 * @asc: additional sense code
101 * @ascq: additional sense code qualifier
102 */
103struct sense_info {
104 u8 skey;
105 u8 asc;
106 u8 ascq;
107};
108
109
Eric Moore635374e2009-03-09 01:21:12 -0600110/**
111 * struct fw_event_work - firmware event struct
112 * @list: link list framework
113 * @work: work object (ioc->fault_reset_work_q)
114 * @ioc: per adapter object
115 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530116 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600117 * @host_reset_handling: handling events during host reset
118 * @ignore: flag meaning this event has been marked to ignore
119 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
120 * @event_data: reply event data payload follows
121 *
122 * This object stored on ioc->fw_event_list.
123 */
124struct fw_event_work {
125 struct list_head list;
Eric Moore6f92a7a2009-04-21 15:43:33 -0600126 struct work_struct work;
Eric Moore635374e2009-03-09 01:21:12 -0600127 struct MPT2SAS_ADAPTER *ioc;
128 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530129 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600130 u8 host_reset_handling;
131 u8 ignore;
132 u16 event;
133 void *event_data;
134};
135
136/**
137 * struct _scsi_io_transfer - scsi io transfer
138 * @handle: sas device handle (assigned by firmware)
139 * @is_raid: flag set for hidden raid components
140 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
141 * @data_length: data transfer length
142 * @data_dma: dma pointer to data
143 * @sense: sense data
144 * @lun: lun number
145 * @cdb_length: cdb length
146 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530148 * @VF_ID: virtual function id
149 * @VP_ID: virtual port id
150 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600151 * @sense_length: sense length
152 * @ioc_status: ioc status
153 * @scsi_state: scsi state
154 * @scsi_status: scsi staus
155 * @log_info: log information
156 * @transfer_length: data length transfer when there is a reply message
157 *
158 * Used for sending internal scsi commands to devices within this module.
159 * Refer to _scsi_send_scsi_io().
160 */
161struct _scsi_io_transfer {
162 u16 handle;
163 u8 is_raid;
164 enum dma_data_direction dir;
165 u32 data_length;
166 dma_addr_t data_dma;
167 u8 sense[SCSI_SENSE_BUFFERSIZE];
168 u32 lun;
169 u8 cdb_length;
170 u8 cdb[32];
171 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530172 u8 VF_ID;
173 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600174 u8 valid_reply;
175 /* the following bits are only valid when 'valid_reply = 1' */
176 u32 sense_length;
177 u16 ioc_status;
178 u8 scsi_state;
179 u8 scsi_status;
180 u32 log_info;
181 u32 transfer_length;
182};
183
184/*
185 * The pci device ids are defined in mpi/mpi2_cnfg.h.
186 */
187static struct pci_device_id scsih_pci_table[] = {
188 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
189 PCI_ANY_ID, PCI_ANY_ID },
190 /* Falcon ~ 2008*/
191 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
192 PCI_ANY_ID, PCI_ANY_ID },
193 /* Liberator ~ 2108 */
194 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
195 PCI_ANY_ID, PCI_ANY_ID },
196 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
197 PCI_ANY_ID, PCI_ANY_ID },
198 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
199 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530200 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600201 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
202 PCI_ANY_ID, PCI_ANY_ID },
203 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
204 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530205 /* Thunderbolt ~ 2208 */
206 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
207 PCI_ANY_ID, PCI_ANY_ID },
208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
209 PCI_ANY_ID, PCI_ANY_ID },
210 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
211 PCI_ANY_ID, PCI_ANY_ID },
212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
213 PCI_ANY_ID, PCI_ANY_ID },
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
215 PCI_ANY_ID, PCI_ANY_ID },
216 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
217 PCI_ANY_ID, PCI_ANY_ID },
218 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
219 PCI_ANY_ID, PCI_ANY_ID },
220 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
221 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600222 {0} /* Terminating entry */
223};
224MODULE_DEVICE_TABLE(pci, scsih_pci_table);
225
226/**
Eric Moored5d135b2009-05-18 13:02:08 -0600227 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600228 *
229 * Note: The logging levels are defined in mpt2sas_debug.h.
230 */
231static int
Eric Moored5d135b2009-05-18 13:02:08 -0600232_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600233{
234 int ret = param_set_int(val, kp);
235 struct MPT2SAS_ADAPTER *ioc;
236
237 if (ret)
238 return ret;
239
240 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600241 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600242 ioc->logging_level = logging_level;
243 return 0;
244}
Eric Moored5d135b2009-05-18 13:02:08 -0600245module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600246 &logging_level, 0644);
247
248/**
249 * _scsih_srch_boot_sas_address - search based on sas_address
250 * @sas_address: sas address
251 * @boot_device: boot device object from bios page 2
252 *
253 * Returns 1 when there's a match, 0 means no match.
254 */
255static inline int
256_scsih_srch_boot_sas_address(u64 sas_address,
257 Mpi2BootDeviceSasWwid_t *boot_device)
258{
259 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
260}
261
262/**
263 * _scsih_srch_boot_device_name - search based on device name
264 * @device_name: device name specified in INDENTIFY fram
265 * @boot_device: boot device object from bios page 2
266 *
267 * Returns 1 when there's a match, 0 means no match.
268 */
269static inline int
270_scsih_srch_boot_device_name(u64 device_name,
271 Mpi2BootDeviceDeviceName_t *boot_device)
272{
273 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
274}
275
276/**
277 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
278 * @enclosure_logical_id: enclosure logical id
279 * @slot_number: slot number
280 * @boot_device: boot device object from bios page 2
281 *
282 * Returns 1 when there's a match, 0 means no match.
283 */
284static inline int
285_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
286 Mpi2BootDeviceEnclosureSlot_t *boot_device)
287{
288 return (enclosure_logical_id == le64_to_cpu(boot_device->
289 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
290 SlotNumber)) ? 1 : 0;
291}
292
293/**
294 * _scsih_is_boot_device - search for matching boot device.
295 * @sas_address: sas address
296 * @device_name: device name specified in INDENTIFY fram
297 * @enclosure_logical_id: enclosure logical id
298 * @slot_number: slot number
299 * @form: specifies boot device form
300 * @boot_device: boot device object from bios page 2
301 *
302 * Returns 1 when there's a match, 0 means no match.
303 */
304static int
305_scsih_is_boot_device(u64 sas_address, u64 device_name,
306 u64 enclosure_logical_id, u16 slot, u8 form,
307 Mpi2BiosPage2BootDevice_t *boot_device)
308{
309 int rc = 0;
310
311 switch (form) {
312 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
313 if (!sas_address)
314 break;
315 rc = _scsih_srch_boot_sas_address(
316 sas_address, &boot_device->SasWwid);
317 break;
318 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
319 if (!enclosure_logical_id)
320 break;
321 rc = _scsih_srch_boot_encl_slot(
322 enclosure_logical_id,
323 slot, &boot_device->EnclosureSlot);
324 break;
325 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
326 if (!device_name)
327 break;
328 rc = _scsih_srch_boot_device_name(
329 device_name, &boot_device->DeviceName);
330 break;
331 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
332 break;
333 }
334
335 return rc;
336}
337
338/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530339 * _scsih_get_sas_address - set the sas_address for given device handle
340 * @handle: device handle
341 * @sas_address: sas address
342 *
343 * Returns 0 success, non-zero when failure
344 */
345static int
346_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
347 u64 *sas_address)
348{
349 Mpi2SasDevicePage0_t sas_device_pg0;
350 Mpi2ConfigReply_t mpi_reply;
351 u32 ioc_status;
352
353 if (handle <= ioc->sas_hba.num_phys) {
354 *sas_address = ioc->sas_hba.sas_address;
355 return 0;
356 } else
357 *sas_address = 0;
358
359 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
360 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
361 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
362 ioc->name, __FILE__, __LINE__, __func__);
363 return -ENXIO;
364 }
365
366 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
367 MPI2_IOCSTATUS_MASK;
368 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
369 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
370 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
371 __FILE__, __LINE__, __func__);
372 return -EIO;
373 }
374
375 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
376 return 0;
377}
378
379/**
Eric Moore635374e2009-03-09 01:21:12 -0600380 * _scsih_determine_boot_device - determine boot device.
381 * @ioc: per adapter object
382 * @device: either sas_device or raid_device object
383 * @is_raid: [flag] 1 = raid object, 0 = sas object
384 *
385 * Determines whether this device should be first reported device to
386 * to scsi-ml or sas transport, this purpose is for persistant boot device.
387 * There are primary, alternate, and current entries in bios page 2. The order
388 * priority is primary, alternate, then current. This routine saves
389 * the corresponding device object and is_raid flag in the ioc object.
390 * The saved data to be used later in _scsih_probe_boot_devices().
391 */
392static void
393_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
394 void *device, u8 is_raid)
395{
396 struct _sas_device *sas_device;
397 struct _raid_device *raid_device;
398 u64 sas_address;
399 u64 device_name;
400 u64 enclosure_logical_id;
401 u16 slot;
402
403 /* only process this function when driver loads */
404 if (!ioc->wait_for_port_enable_to_complete)
405 return;
406
407 if (!is_raid) {
408 sas_device = device;
409 sas_address = sas_device->sas_address;
410 device_name = sas_device->device_name;
411 enclosure_logical_id = sas_device->enclosure_logical_id;
412 slot = sas_device->slot;
413 } else {
414 raid_device = device;
415 sas_address = raid_device->wwid;
416 device_name = 0;
417 enclosure_logical_id = 0;
418 slot = 0;
419 }
420
421 if (!ioc->req_boot_device.device) {
422 if (_scsih_is_boot_device(sas_address, device_name,
423 enclosure_logical_id, slot,
424 (ioc->bios_pg2.ReqBootDeviceForm &
425 MPI2_BIOSPAGE2_FORM_MASK),
426 &ioc->bios_pg2.RequestedBootDevice)) {
427 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
428 "%s: req_boot_device(0x%016llx)\n",
429 ioc->name, __func__,
430 (unsigned long long)sas_address));
431 ioc->req_boot_device.device = device;
432 ioc->req_boot_device.is_raid = is_raid;
433 }
434 }
435
436 if (!ioc->req_alt_boot_device.device) {
437 if (_scsih_is_boot_device(sas_address, device_name,
438 enclosure_logical_id, slot,
439 (ioc->bios_pg2.ReqAltBootDeviceForm &
440 MPI2_BIOSPAGE2_FORM_MASK),
441 &ioc->bios_pg2.RequestedAltBootDevice)) {
442 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
443 "%s: req_alt_boot_device(0x%016llx)\n",
444 ioc->name, __func__,
445 (unsigned long long)sas_address));
446 ioc->req_alt_boot_device.device = device;
447 ioc->req_alt_boot_device.is_raid = is_raid;
448 }
449 }
450
451 if (!ioc->current_boot_device.device) {
452 if (_scsih_is_boot_device(sas_address, device_name,
453 enclosure_logical_id, slot,
454 (ioc->bios_pg2.CurrentBootDeviceForm &
455 MPI2_BIOSPAGE2_FORM_MASK),
456 &ioc->bios_pg2.CurrentBootDevice)) {
457 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
458 "%s: current_boot_device(0x%016llx)\n",
459 ioc->name, __func__,
460 (unsigned long long)sas_address));
461 ioc->current_boot_device.device = device;
462 ioc->current_boot_device.is_raid = is_raid;
463 }
464 }
465}
466
467/**
468 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
469 * @ioc: per adapter object
470 * @sas_address: sas address
471 * Context: Calling function should acquire ioc->sas_device_lock
472 *
473 * This searches for sas_device based on sas_address, then return sas_device
474 * object.
475 */
476struct _sas_device *
477mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
478 u64 sas_address)
479{
480 struct _sas_device *sas_device, *r;
481
482 r = NULL;
483 /* check the sas_device_init_list */
484 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
485 list) {
486 if (sas_device->sas_address != sas_address)
487 continue;
488 r = sas_device;
489 goto out;
490 }
491
492 /* then check the sas_device_list */
493 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
494 if (sas_device->sas_address != sas_address)
495 continue;
496 r = sas_device;
497 goto out;
498 }
499 out:
500 return r;
501}
502
503/**
504 * _scsih_sas_device_find_by_handle - sas device search
505 * @ioc: per adapter object
506 * @handle: sas device handle (assigned by firmware)
507 * Context: Calling function should acquire ioc->sas_device_lock
508 *
509 * This searches for sas_device based on sas_address, then return sas_device
510 * object.
511 */
512static struct _sas_device *
513_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
514{
515 struct _sas_device *sas_device, *r;
516
517 r = NULL;
518 if (ioc->wait_for_port_enable_to_complete) {
519 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
520 list) {
521 if (sas_device->handle != handle)
522 continue;
523 r = sas_device;
524 goto out;
525 }
526 } else {
527 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
528 if (sas_device->handle != handle)
529 continue;
530 r = sas_device;
531 goto out;
532 }
533 }
534
535 out:
536 return r;
537}
538
539/**
540 * _scsih_sas_device_remove - remove sas_device from list.
541 * @ioc: per adapter object
542 * @sas_device: the sas_device object
543 * Context: This function will acquire ioc->sas_device_lock.
544 *
545 * Removing object and freeing associated memory from the ioc->sas_device_list.
546 */
547static void
548_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
549 struct _sas_device *sas_device)
550{
551 unsigned long flags;
552
553 spin_lock_irqsave(&ioc->sas_device_lock, flags);
554 list_del(&sas_device->list);
555 memset(sas_device, 0, sizeof(struct _sas_device));
556 kfree(sas_device);
557 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
558}
559
560/**
561 * _scsih_sas_device_add - insert sas_device to the list.
562 * @ioc: per adapter object
563 * @sas_device: the sas_device object
564 * Context: This function will acquire ioc->sas_device_lock.
565 *
566 * Adding new object to the ioc->sas_device_list.
567 */
568static void
569_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
570 struct _sas_device *sas_device)
571{
572 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600573
574 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
575 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
576 sas_device->handle, (unsigned long long)sas_device->sas_address));
577
578 spin_lock_irqsave(&ioc->sas_device_lock, flags);
579 list_add_tail(&sas_device->list, &ioc->sas_device_list);
580 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
581
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530582 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
583 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600584 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600585}
586
587/**
588 * _scsih_sas_device_init_add - insert sas_device to the list.
589 * @ioc: per adapter object
590 * @sas_device: the sas_device object
591 * Context: This function will acquire ioc->sas_device_lock.
592 *
593 * Adding new object at driver load time to the ioc->sas_device_init_list.
594 */
595static void
596_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
597 struct _sas_device *sas_device)
598{
599 unsigned long flags;
600
601 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
602 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
603 sas_device->handle, (unsigned long long)sas_device->sas_address));
604
605 spin_lock_irqsave(&ioc->sas_device_lock, flags);
606 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
607 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
608 _scsih_determine_boot_device(ioc, sas_device, 0);
609}
610
611/**
Eric Moore635374e2009-03-09 01:21:12 -0600612 * _scsih_raid_device_find_by_id - raid device search
613 * @ioc: per adapter object
614 * @id: sas device target id
615 * @channel: sas device channel
616 * Context: Calling function should acquire ioc->raid_device_lock
617 *
618 * This searches for raid_device based on target id, then return raid_device
619 * object.
620 */
621static struct _raid_device *
622_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
623{
624 struct _raid_device *raid_device, *r;
625
626 r = NULL;
627 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
628 if (raid_device->id == id && raid_device->channel == channel) {
629 r = raid_device;
630 goto out;
631 }
632 }
633
634 out:
635 return r;
636}
637
638/**
639 * _scsih_raid_device_find_by_handle - raid device search
640 * @ioc: per adapter object
641 * @handle: sas device handle (assigned by firmware)
642 * Context: Calling function should acquire ioc->raid_device_lock
643 *
644 * This searches for raid_device based on handle, then return raid_device
645 * object.
646 */
647static struct _raid_device *
648_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
649{
650 struct _raid_device *raid_device, *r;
651
652 r = NULL;
653 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
654 if (raid_device->handle != handle)
655 continue;
656 r = raid_device;
657 goto out;
658 }
659
660 out:
661 return r;
662}
663
664/**
665 * _scsih_raid_device_find_by_wwid - raid device search
666 * @ioc: per adapter object
667 * @handle: sas device handle (assigned by firmware)
668 * Context: Calling function should acquire ioc->raid_device_lock
669 *
670 * This searches for raid_device based on wwid, then return raid_device
671 * object.
672 */
673static struct _raid_device *
674_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
675{
676 struct _raid_device *raid_device, *r;
677
678 r = NULL;
679 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
680 if (raid_device->wwid != wwid)
681 continue;
682 r = raid_device;
683 goto out;
684 }
685
686 out:
687 return r;
688}
689
690/**
691 * _scsih_raid_device_add - add raid_device object
692 * @ioc: per adapter object
693 * @raid_device: raid_device object
694 *
695 * This is added to the raid_device_list link list.
696 */
697static void
698_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
699 struct _raid_device *raid_device)
700{
701 unsigned long flags;
702
703 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
704 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
705 raid_device->handle, (unsigned long long)raid_device->wwid));
706
707 spin_lock_irqsave(&ioc->raid_device_lock, flags);
708 list_add_tail(&raid_device->list, &ioc->raid_device_list);
709 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
710}
711
712/**
713 * _scsih_raid_device_remove - delete raid_device object
714 * @ioc: per adapter object
715 * @raid_device: raid_device object
716 *
717 * This is removed from the raid_device_list link list.
718 */
719static void
720_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
721 struct _raid_device *raid_device)
722{
723 unsigned long flags;
724
725 spin_lock_irqsave(&ioc->raid_device_lock, flags);
726 list_del(&raid_device->list);
727 memset(raid_device, 0, sizeof(struct _raid_device));
728 kfree(raid_device);
729 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
730}
731
732/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530733 * mpt2sas_scsih_expander_find_by_handle - expander device search
734 * @ioc: per adapter object
735 * @handle: expander handle (assigned by firmware)
736 * Context: Calling function should acquire ioc->sas_device_lock
737 *
738 * This searches for expander device based on handle, then returns the
739 * sas_node object.
740 */
741struct _sas_node *
742mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
743{
744 struct _sas_node *sas_expander, *r;
745
746 r = NULL;
747 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
748 if (sas_expander->handle != handle)
749 continue;
750 r = sas_expander;
751 goto out;
752 }
753 out:
754 return r;
755}
756
757/**
Eric Moore635374e2009-03-09 01:21:12 -0600758 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
759 * @ioc: per adapter object
760 * @sas_address: sas address
761 * Context: Calling function should acquire ioc->sas_node_lock.
762 *
763 * This searches for expander device based on sas_address, then returns the
764 * sas_node object.
765 */
766struct _sas_node *
767mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
768 u64 sas_address)
769{
770 struct _sas_node *sas_expander, *r;
771
772 r = NULL;
773 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
774 if (sas_expander->sas_address != sas_address)
775 continue;
776 r = sas_expander;
777 goto out;
778 }
779 out:
780 return r;
781}
782
783/**
784 * _scsih_expander_node_add - insert expander device to the list.
785 * @ioc: per adapter object
786 * @sas_expander: the sas_device object
787 * Context: This function will acquire ioc->sas_node_lock.
788 *
789 * Adding new object to the ioc->sas_expander_list.
790 *
791 * Return nothing.
792 */
793static void
794_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
795 struct _sas_node *sas_expander)
796{
797 unsigned long flags;
798
799 spin_lock_irqsave(&ioc->sas_node_lock, flags);
800 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
801 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
802}
803
804/**
805 * _scsih_is_end_device - determines if device is an end device
806 * @device_info: bitfield providing information about the device.
807 * Context: none
808 *
809 * Returns 1 if end device.
810 */
811static int
812_scsih_is_end_device(u32 device_info)
813{
814 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
815 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
816 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
817 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
818 return 1;
819 else
820 return 0;
821}
822
823/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530824 * mptscsih_get_scsi_lookup - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600825 * @ioc: per adapter object
826 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600827 *
828 * Returns the smid stored scmd pointer.
829 */
830static struct scsi_cmnd *
831_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
832{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530833 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600834}
835
836/**
837 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
838 * @ioc: per adapter object
839 * @smid: system request message index
840 * @scmd: pointer to scsi command object
841 * Context: This function will acquire ioc->scsi_lookup_lock.
842 *
843 * This will search for a scmd pointer in the scsi_lookup array,
844 * returning the revelent smid. A returned value of zero means invalid.
845 */
846static u16
847_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
848 *scmd)
849{
850 u16 smid;
851 unsigned long flags;
852 int i;
853
854 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
855 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530856 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600857 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530858 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600859 goto out;
860 }
861 }
862 out:
863 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
864 return smid;
865}
866
867/**
868 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
869 * @ioc: per adapter object
870 * @id: target id
871 * @channel: channel
872 * Context: This function will acquire ioc->scsi_lookup_lock.
873 *
874 * This will search for a matching channel:id in the scsi_lookup array,
875 * returning 1 if found.
876 */
877static u8
878_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
879 int channel)
880{
881 u8 found;
882 unsigned long flags;
883 int i;
884
885 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
886 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530887 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600888 if (ioc->scsi_lookup[i].scmd &&
889 (ioc->scsi_lookup[i].scmd->device->id == id &&
890 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
891 found = 1;
892 goto out;
893 }
894 }
895 out:
896 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
897 return found;
898}
899
900/**
Eric Moore993e0da2009-05-18 13:00:45 -0600901 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
902 * @ioc: per adapter object
903 * @id: target id
904 * @lun: lun number
905 * @channel: channel
906 * Context: This function will acquire ioc->scsi_lookup_lock.
907 *
908 * This will search for a matching channel:id:lun in the scsi_lookup array,
909 * returning 1 if found.
910 */
911static u8
912_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
913 unsigned int lun, int channel)
914{
915 u8 found;
916 unsigned long flags;
917 int i;
918
919 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
920 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530921 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600922 if (ioc->scsi_lookup[i].scmd &&
923 (ioc->scsi_lookup[i].scmd->device->id == id &&
924 ioc->scsi_lookup[i].scmd->device->channel == channel &&
925 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
926 found = 1;
927 goto out;
928 }
929 }
930 out:
931 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
932 return found;
933}
934
935/**
Eric Moore635374e2009-03-09 01:21:12 -0600936 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
937 * @ioc: per adapter object
938 * @smid: system request message index
939 *
940 * Returns phys pointer to chain buffer.
941 */
942static dma_addr_t
943_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
944{
945 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
946 ioc->chains_needed_per_io));
947}
948
949/**
950 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
951 * @ioc: per adapter object
952 * @smid: system request message index
953 *
954 * Returns virt pointer to chain buffer.
955 */
956static void *
957_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
958{
959 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
960 ioc->chains_needed_per_io)));
961}
962
963/**
964 * _scsih_build_scatter_gather - main sg creation routine
965 * @ioc: per adapter object
966 * @scmd: scsi command
967 * @smid: system request message index
968 * Context: none.
969 *
970 * The main routine that builds scatter gather table from a given
971 * scsi request sent via the .queuecommand main handler.
972 *
973 * Returns 0 success, anything else error
974 */
975static int
976_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
977 struct scsi_cmnd *scmd, u16 smid)
978{
979 Mpi2SCSIIORequest_t *mpi_request;
980 dma_addr_t chain_dma;
981 struct scatterlist *sg_scmd;
982 void *sg_local, *chain;
983 u32 chain_offset;
984 u32 chain_length;
985 u32 chain_flags;
986 u32 sges_left;
987 u32 sges_in_segment;
988 u32 sgl_flags;
989 u32 sgl_flags_last_element;
990 u32 sgl_flags_end_buffer;
991
992 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
993
994 /* init scatter gather flags */
995 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
996 if (scmd->sc_data_direction == DMA_TO_DEVICE)
997 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
998 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
999 << MPI2_SGE_FLAGS_SHIFT;
1000 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1001 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1002 << MPI2_SGE_FLAGS_SHIFT;
1003 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1004
1005 sg_scmd = scsi_sglist(scmd);
1006 sges_left = scsi_dma_map(scmd);
1007 if (!sges_left) {
1008 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1009 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1010 return -ENOMEM;
1011 }
1012
1013 sg_local = &mpi_request->SGL;
1014 sges_in_segment = ioc->max_sges_in_main_message;
1015 if (sges_left <= sges_in_segment)
1016 goto fill_in_last_segment;
1017
1018 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1019 (sges_in_segment * ioc->sge_size))/4;
1020
1021 /* fill in main message segment when there is a chain following */
1022 while (sges_in_segment) {
1023 if (sges_in_segment == 1)
1024 ioc->base_add_sg_single(sg_local,
1025 sgl_flags_last_element | sg_dma_len(sg_scmd),
1026 sg_dma_address(sg_scmd));
1027 else
1028 ioc->base_add_sg_single(sg_local, sgl_flags |
1029 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1030 sg_scmd = sg_next(sg_scmd);
1031 sg_local += ioc->sge_size;
1032 sges_left--;
1033 sges_in_segment--;
1034 }
1035
1036 /* initializing the chain flags and pointers */
1037 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1038 chain = _scsih_get_chain_buffer(ioc, smid);
1039 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1040 do {
1041 sges_in_segment = (sges_left <=
1042 ioc->max_sges_in_chain_message) ? sges_left :
1043 ioc->max_sges_in_chain_message;
1044 chain_offset = (sges_left == sges_in_segment) ?
1045 0 : (sges_in_segment * ioc->sge_size)/4;
1046 chain_length = sges_in_segment * ioc->sge_size;
1047 if (chain_offset) {
1048 chain_offset = chain_offset <<
1049 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1050 chain_length += ioc->sge_size;
1051 }
1052 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1053 chain_length, chain_dma);
1054 sg_local = chain;
1055 if (!chain_offset)
1056 goto fill_in_last_segment;
1057
1058 /* fill in chain segments */
1059 while (sges_in_segment) {
1060 if (sges_in_segment == 1)
1061 ioc->base_add_sg_single(sg_local,
1062 sgl_flags_last_element |
1063 sg_dma_len(sg_scmd),
1064 sg_dma_address(sg_scmd));
1065 else
1066 ioc->base_add_sg_single(sg_local, sgl_flags |
1067 sg_dma_len(sg_scmd),
1068 sg_dma_address(sg_scmd));
1069 sg_scmd = sg_next(sg_scmd);
1070 sg_local += ioc->sge_size;
1071 sges_left--;
1072 sges_in_segment--;
1073 }
1074
1075 chain_dma += ioc->request_sz;
1076 chain += ioc->request_sz;
1077 } while (1);
1078
1079
1080 fill_in_last_segment:
1081
1082 /* fill the last segment */
1083 while (sges_left) {
1084 if (sges_left == 1)
1085 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1086 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1087 else
1088 ioc->base_add_sg_single(sg_local, sgl_flags |
1089 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1090 sg_scmd = sg_next(sg_scmd);
1091 sg_local += ioc->sge_size;
1092 sges_left--;
1093 }
1094
1095 return 0;
1096}
1097
1098/**
Eric Moored5d135b2009-05-18 13:02:08 -06001099 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001100 * @sdev: scsi device struct
1101 * @qdepth: requested queue depth
1102 *
1103 * Returns queue depth.
1104 */
1105static int
Eric Moored5d135b2009-05-18 13:02:08 -06001106_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001107{
1108 struct Scsi_Host *shost = sdev->host;
1109 int max_depth;
1110 int tag_type;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301111 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1112 struct MPT2SAS_DEVICE *sas_device_priv_data;
1113 struct MPT2SAS_TARGET *sas_target_priv_data;
1114 struct _sas_device *sas_device;
1115 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001116
1117 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301118
1119 /* limit max device queue for SATA to 32 */
1120 sas_device_priv_data = sdev->hostdata;
1121 if (!sas_device_priv_data)
1122 goto not_sata;
1123 sas_target_priv_data = sas_device_priv_data->sas_target;
1124 if (!sas_target_priv_data)
1125 goto not_sata;
1126 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1127 goto not_sata;
1128 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1129 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1130 sas_device_priv_data->sas_target->sas_address);
1131 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1132 if (sas_device && sas_device->device_info &
1133 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1134 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1135
1136 not_sata:
1137
Eric Moore635374e2009-03-09 01:21:12 -06001138 if (!sdev->tagged_supported)
1139 max_depth = 1;
1140 if (qdepth > max_depth)
1141 qdepth = max_depth;
1142 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1143 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1144
1145 if (sdev->inquiry_len > 7)
1146 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1147 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1148 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1149 sdev->ordered_tags, sdev->scsi_level,
1150 (sdev->inquiry[7] & 2) >> 1);
1151
1152 return sdev->queue_depth;
1153}
1154
1155/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301156 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001157 * @sdev: scsi device struct
1158 * @tag_type: requested tag type
1159 *
1160 * Returns queue tag type.
1161 */
1162static int
Eric Moored5d135b2009-05-18 13:02:08 -06001163_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001164{
1165 if (sdev->tagged_supported) {
1166 scsi_set_tag_type(sdev, tag_type);
1167 if (tag_type)
1168 scsi_activate_tcq(sdev, sdev->queue_depth);
1169 else
1170 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1171 } else
1172 tag_type = 0;
1173
1174 return tag_type;
1175}
1176
1177/**
Eric Moored5d135b2009-05-18 13:02:08 -06001178 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001179 * @starget: scsi target struct
1180 *
1181 * Returns 0 if ok. Any other return is assumed to be an error and
1182 * the device is ignored.
1183 */
1184static int
Eric Moored5d135b2009-05-18 13:02:08 -06001185_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001186{
1187 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1188 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1189 struct MPT2SAS_TARGET *sas_target_priv_data;
1190 struct _sas_device *sas_device;
1191 struct _raid_device *raid_device;
1192 unsigned long flags;
1193 struct sas_rphy *rphy;
1194
1195 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1196 if (!sas_target_priv_data)
1197 return -ENOMEM;
1198
1199 starget->hostdata = sas_target_priv_data;
1200 sas_target_priv_data->starget = starget;
1201 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1202
1203 /* RAID volumes */
1204 if (starget->channel == RAID_CHANNEL) {
1205 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1206 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1207 starget->channel);
1208 if (raid_device) {
1209 sas_target_priv_data->handle = raid_device->handle;
1210 sas_target_priv_data->sas_address = raid_device->wwid;
1211 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1212 raid_device->starget = starget;
1213 }
1214 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1215 return 0;
1216 }
1217
1218 /* sas/sata devices */
1219 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1220 rphy = dev_to_rphy(starget->dev.parent);
1221 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1222 rphy->identify.sas_address);
1223
1224 if (sas_device) {
1225 sas_target_priv_data->handle = sas_device->handle;
1226 sas_target_priv_data->sas_address = sas_device->sas_address;
1227 sas_device->starget = starget;
1228 sas_device->id = starget->id;
1229 sas_device->channel = starget->channel;
1230 if (sas_device->hidden_raid_component)
1231 sas_target_priv_data->flags |=
1232 MPT_TARGET_FLAGS_RAID_COMPONENT;
1233 }
1234 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1235
1236 return 0;
1237}
1238
1239/**
Eric Moored5d135b2009-05-18 13:02:08 -06001240 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001241 * @starget: scsi target struct
1242 *
1243 * Returns nothing.
1244 */
1245static void
Eric Moored5d135b2009-05-18 13:02:08 -06001246_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001247{
1248 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1249 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1250 struct MPT2SAS_TARGET *sas_target_priv_data;
1251 struct _sas_device *sas_device;
1252 struct _raid_device *raid_device;
1253 unsigned long flags;
1254 struct sas_rphy *rphy;
1255
1256 sas_target_priv_data = starget->hostdata;
1257 if (!sas_target_priv_data)
1258 return;
1259
1260 if (starget->channel == RAID_CHANNEL) {
1261 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1262 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1263 starget->channel);
1264 if (raid_device) {
1265 raid_device->starget = NULL;
1266 raid_device->sdev = NULL;
1267 }
1268 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1269 goto out;
1270 }
1271
1272 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1273 rphy = dev_to_rphy(starget->dev.parent);
1274 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1275 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001276 if (sas_device && (sas_device->starget == starget) &&
1277 (sas_device->id == starget->id) &&
1278 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001279 sas_device->starget = NULL;
1280
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1282
1283 out:
1284 kfree(sas_target_priv_data);
1285 starget->hostdata = NULL;
1286}
1287
1288/**
Eric Moored5d135b2009-05-18 13:02:08 -06001289 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001290 * @sdev: scsi device struct
1291 *
1292 * Returns 0 if ok. Any other return is assumed to be an error and
1293 * the device is ignored.
1294 */
1295static int
Eric Moored5d135b2009-05-18 13:02:08 -06001296_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001297{
1298 struct Scsi_Host *shost;
1299 struct MPT2SAS_ADAPTER *ioc;
1300 struct MPT2SAS_TARGET *sas_target_priv_data;
1301 struct MPT2SAS_DEVICE *sas_device_priv_data;
1302 struct scsi_target *starget;
1303 struct _raid_device *raid_device;
1304 struct _sas_device *sas_device;
1305 unsigned long flags;
1306
1307 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1308 if (!sas_device_priv_data)
1309 return -ENOMEM;
1310
1311 sas_device_priv_data->lun = sdev->lun;
1312 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1313
1314 starget = scsi_target(sdev);
1315 sas_target_priv_data = starget->hostdata;
1316 sas_target_priv_data->num_luns++;
1317 sas_device_priv_data->sas_target = sas_target_priv_data;
1318 sdev->hostdata = sas_device_priv_data;
1319 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1320 sdev->no_uld_attach = 1;
1321
1322 shost = dev_to_shost(&starget->dev);
1323 ioc = shost_priv(shost);
1324 if (starget->channel == RAID_CHANNEL) {
1325 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1326 raid_device = _scsih_raid_device_find_by_id(ioc,
1327 starget->id, starget->channel);
1328 if (raid_device)
1329 raid_device->sdev = sdev; /* raid is single lun */
1330 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1331 } else {
1332 /* set TLR bit for SSP devices */
1333 if (!(ioc->facts.IOCCapabilities &
1334 MPI2_IOCFACTS_CAPABILITY_TLR))
1335 goto out;
1336 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1337 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1338 sas_device_priv_data->sas_target->sas_address);
1339 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1340 if (sas_device && sas_device->device_info &
1341 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1342 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1343 }
1344
1345 out:
1346 return 0;
1347}
1348
1349/**
Eric Moored5d135b2009-05-18 13:02:08 -06001350 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001351 * @sdev: scsi device struct
1352 *
1353 * Returns nothing.
1354 */
1355static void
Eric Moored5d135b2009-05-18 13:02:08 -06001356_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001357{
1358 struct MPT2SAS_TARGET *sas_target_priv_data;
1359 struct scsi_target *starget;
1360
1361 if (!sdev->hostdata)
1362 return;
1363
1364 starget = scsi_target(sdev);
1365 sas_target_priv_data = starget->hostdata;
1366 sas_target_priv_data->num_luns--;
1367 kfree(sdev->hostdata);
1368 sdev->hostdata = NULL;
1369}
1370
1371/**
Eric Moored5d135b2009-05-18 13:02:08 -06001372 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001373 * @ioc: per adapter object
1374 * @sas_device: the sas_device object
1375 * @sdev: scsi device struct
1376 */
1377static void
Eric Moored5d135b2009-05-18 13:02:08 -06001378_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001379 struct _sas_device *sas_device, struct scsi_device *sdev)
1380{
1381 Mpi2ConfigReply_t mpi_reply;
1382 Mpi2SasDevicePage0_t sas_device_pg0;
1383 u32 ioc_status;
1384 u16 flags;
1385 u32 device_info;
1386
1387 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1388 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1389 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1390 ioc->name, __FILE__, __LINE__, __func__);
1391 return;
1392 }
1393
1394 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1395 MPI2_IOCSTATUS_MASK;
1396 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1397 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1398 ioc->name, __FILE__, __LINE__, __func__);
1399 return;
1400 }
1401
1402 flags = le16_to_cpu(sas_device_pg0.Flags);
1403 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1404
1405 sdev_printk(KERN_INFO, sdev,
1406 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1407 "sw_preserve(%s)\n",
1408 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1409 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1410 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1411 "n",
1412 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1413 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1414 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1415}
1416
1417/**
1418 * _scsih_get_volume_capabilities - volume capabilities
1419 * @ioc: per adapter object
1420 * @sas_device: the raid_device object
1421 */
1422static void
1423_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1424 struct _raid_device *raid_device)
1425{
1426 Mpi2RaidVolPage0_t *vol_pg0;
1427 Mpi2RaidPhysDiskPage0_t pd_pg0;
1428 Mpi2SasDevicePage0_t sas_device_pg0;
1429 Mpi2ConfigReply_t mpi_reply;
1430 u16 sz;
1431 u8 num_pds;
1432
1433 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1434 &num_pds)) || !num_pds) {
1435 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1436 ioc->name, __FILE__, __LINE__, __func__);
1437 return;
1438 }
1439
1440 raid_device->num_pds = num_pds;
1441 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1442 sizeof(Mpi2RaidVol0PhysDisk_t));
1443 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1444 if (!vol_pg0) {
1445 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1446 ioc->name, __FILE__, __LINE__, __func__);
1447 return;
1448 }
1449
1450 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1451 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1452 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1453 ioc->name, __FILE__, __LINE__, __func__);
1454 kfree(vol_pg0);
1455 return;
1456 }
1457
1458 raid_device->volume_type = vol_pg0->VolumeType;
1459
1460 /* figure out what the underlying devices are by
1461 * obtaining the device_info bits for the 1st device
1462 */
1463 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1464 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1465 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1466 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1467 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1468 le16_to_cpu(pd_pg0.DevHandle)))) {
1469 raid_device->device_info =
1470 le32_to_cpu(sas_device_pg0.DeviceInfo);
1471 }
1472 }
1473
1474 kfree(vol_pg0);
1475}
1476
1477/**
Eric Moored5d135b2009-05-18 13:02:08 -06001478 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001479 * @sdev: scsi device struct
1480 *
1481 * Returns 0 if ok. Any other return is assumed to be an error and
1482 * the device is ignored.
1483 */
1484static int
Eric Moored5d135b2009-05-18 13:02:08 -06001485_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001486{
1487 struct Scsi_Host *shost = sdev->host;
1488 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1489 struct MPT2SAS_DEVICE *sas_device_priv_data;
1490 struct MPT2SAS_TARGET *sas_target_priv_data;
1491 struct _sas_device *sas_device;
1492 struct _raid_device *raid_device;
1493 unsigned long flags;
1494 int qdepth;
1495 u8 ssp_target = 0;
1496 char *ds = "";
1497 char *r_level = "";
1498
1499 qdepth = 1;
1500 sas_device_priv_data = sdev->hostdata;
1501 sas_device_priv_data->configured_lun = 1;
1502 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1503 sas_target_priv_data = sas_device_priv_data->sas_target;
1504
1505 /* raid volume handling */
1506 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1507
1508 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1509 raid_device = _scsih_raid_device_find_by_handle(ioc,
1510 sas_target_priv_data->handle);
1511 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1512 if (!raid_device) {
1513 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1514 ioc->name, __FILE__, __LINE__, __func__);
1515 return 0;
1516 }
1517
1518 _scsih_get_volume_capabilities(ioc, raid_device);
1519
1520 /* RAID Queue Depth Support
1521 * IS volume = underlying qdepth of drive type, either
1522 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1523 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1524 */
1525 if (raid_device->device_info &
1526 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1527 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1528 ds = "SSP";
1529 } else {
1530 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1531 if (raid_device->device_info &
1532 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1533 ds = "SATA";
1534 else
1535 ds = "STP";
1536 }
1537
1538 switch (raid_device->volume_type) {
1539 case MPI2_RAID_VOL_TYPE_RAID0:
1540 r_level = "RAID0";
1541 break;
1542 case MPI2_RAID_VOL_TYPE_RAID1E:
1543 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301544 if (ioc->manu_pg10.OEMIdentifier &&
1545 (ioc->manu_pg10.GenericFlags0 &
1546 MFG10_GF0_R10_DISPLAY) &&
1547 !(raid_device->num_pds % 2))
1548 r_level = "RAID10";
1549 else
1550 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001551 break;
1552 case MPI2_RAID_VOL_TYPE_RAID1:
1553 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1554 r_level = "RAID1";
1555 break;
1556 case MPI2_RAID_VOL_TYPE_RAID10:
1557 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1558 r_level = "RAID10";
1559 break;
1560 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1561 default:
1562 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1563 r_level = "RAIDX";
1564 break;
1565 }
1566
1567 sdev_printk(KERN_INFO, sdev, "%s: "
1568 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1569 r_level, raid_device->handle,
1570 (unsigned long long)raid_device->wwid,
1571 raid_device->num_pds, ds);
Eric Moored5d135b2009-05-18 13:02:08 -06001572 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001573 return 0;
1574 }
1575
1576 /* non-raid handling */
1577 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1578 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1579 sas_device_priv_data->sas_target->sas_address);
1580 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1581 if (sas_device) {
1582 if (sas_target_priv_data->flags &
1583 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1584 mpt2sas_config_get_volume_handle(ioc,
1585 sas_device->handle, &sas_device->volume_handle);
1586 mpt2sas_config_get_volume_wwid(ioc,
1587 sas_device->volume_handle,
1588 &sas_device->volume_wwid);
1589 }
1590 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1591 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1592 ssp_target = 1;
1593 ds = "SSP";
1594 } else {
1595 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1596 if (sas_device->device_info &
1597 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1598 ds = "STP";
1599 else if (sas_device->device_info &
1600 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1601 ds = "SATA";
1602 }
1603
1604 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1605 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1606 ds, sas_device->handle,
1607 (unsigned long long)sas_device->sas_address,
1608 (unsigned long long)sas_device->device_name);
1609 sdev_printk(KERN_INFO, sdev, "%s: "
1610 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1611 (unsigned long long) sas_device->enclosure_logical_id,
1612 sas_device->slot);
1613
1614 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001615 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001616 }
1617
Eric Moored5d135b2009-05-18 13:02:08 -06001618 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001619
1620 if (ssp_target)
1621 sas_read_port_mode_page(sdev);
1622 return 0;
1623}
1624
1625/**
Eric Moored5d135b2009-05-18 13:02:08 -06001626 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001627 * @sdev: scsi device struct
1628 * @bdev: pointer to block device context
1629 * @capacity: device size (in 512 byte sectors)
1630 * @params: three element array to place output:
1631 * params[0] number of heads (max 255)
1632 * params[1] number of sectors (max 63)
1633 * params[2] number of cylinders
1634 *
1635 * Return nothing.
1636 */
1637static int
Eric Moored5d135b2009-05-18 13:02:08 -06001638_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001639 sector_t capacity, int params[])
1640{
1641 int heads;
1642 int sectors;
1643 sector_t cylinders;
1644 ulong dummy;
1645
1646 heads = 64;
1647 sectors = 32;
1648
1649 dummy = heads * sectors;
1650 cylinders = capacity;
1651 sector_div(cylinders, dummy);
1652
1653 /*
1654 * Handle extended translation size for logical drives
1655 * > 1Gb
1656 */
1657 if ((ulong)capacity >= 0x200000) {
1658 heads = 255;
1659 sectors = 63;
1660 dummy = heads * sectors;
1661 cylinders = capacity;
1662 sector_div(cylinders, dummy);
1663 }
1664
1665 /* return result */
1666 params[0] = heads;
1667 params[1] = sectors;
1668 params[2] = cylinders;
1669
1670 return 0;
1671}
1672
1673/**
1674 * _scsih_response_code - translation of device response code
1675 * @ioc: per adapter object
1676 * @response_code: response code returned by the device
1677 *
1678 * Return nothing.
1679 */
1680static void
1681_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1682{
1683 char *desc;
1684
1685 switch (response_code) {
1686 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1687 desc = "task management request completed";
1688 break;
1689 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1690 desc = "invalid frame";
1691 break;
1692 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1693 desc = "task management request not supported";
1694 break;
1695 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1696 desc = "task management request failed";
1697 break;
1698 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1699 desc = "task management request succeeded";
1700 break;
1701 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1702 desc = "invalid lun";
1703 break;
1704 case 0xA:
1705 desc = "overlapped tag attempted";
1706 break;
1707 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1708 desc = "task queued, however not sent to target";
1709 break;
1710 default:
1711 desc = "unknown";
1712 break;
1713 }
1714 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1715 ioc->name, response_code, desc);
1716}
1717
1718/**
Eric Moored5d135b2009-05-18 13:02:08 -06001719 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001720 * @ioc: per adapter object
1721 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301722 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001723 * @reply: reply message frame(lower 32bit addr)
1724 * Context: none.
1725 *
1726 * The callback handler when using scsih_issue_tm.
1727 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301728 * Return 1 meaning mf should be freed from _base_interrupt
1729 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001730 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301731static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301732_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001733{
1734 MPI2DefaultReply_t *mpi_reply;
1735
1736 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301737 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001738 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301739 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001740 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1741 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1742 if (mpi_reply) {
1743 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1744 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1745 }
1746 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1747 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301748 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001749}
1750
1751/**
1752 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1753 * @ioc: per adapter object
1754 * @handle: device handle
1755 *
1756 * During taskmangement request, we need to freeze the device queue.
1757 */
1758void
1759mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1760{
1761 struct MPT2SAS_DEVICE *sas_device_priv_data;
1762 struct scsi_device *sdev;
1763 u8 skip = 0;
1764
1765 shost_for_each_device(sdev, ioc->shost) {
1766 if (skip)
1767 continue;
1768 sas_device_priv_data = sdev->hostdata;
1769 if (!sas_device_priv_data)
1770 continue;
1771 if (sas_device_priv_data->sas_target->handle == handle) {
1772 sas_device_priv_data->sas_target->tm_busy = 1;
1773 skip = 1;
1774 ioc->ignore_loginfos = 1;
1775 }
1776 }
1777}
1778
1779/**
1780 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1781 * @ioc: per adapter object
1782 * @handle: device handle
1783 *
1784 * During taskmangement request, we need to freeze the device queue.
1785 */
1786void
1787mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1788{
1789 struct MPT2SAS_DEVICE *sas_device_priv_data;
1790 struct scsi_device *sdev;
1791 u8 skip = 0;
1792
1793 shost_for_each_device(sdev, ioc->shost) {
1794 if (skip)
1795 continue;
1796 sas_device_priv_data = sdev->hostdata;
1797 if (!sas_device_priv_data)
1798 continue;
1799 if (sas_device_priv_data->sas_target->handle == handle) {
1800 sas_device_priv_data->sas_target->tm_busy = 0;
1801 skip = 1;
1802 ioc->ignore_loginfos = 0;
1803 }
1804 }
1805}
1806
1807/**
1808 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1809 * @ioc: per adapter struct
1810 * @device_handle: device handle
1811 * @lun: lun number
1812 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1813 * @smid_task: smid assigned to the task
1814 * @timeout: timeout in seconds
1815 * Context: The calling function needs to acquire the tm_cmds.mutex
1816 *
1817 * A generic API for sending task management requests to firmware.
1818 *
1819 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1820 * this API.
1821 *
1822 * The callback index is set inside `ioc->tm_cb_idx`.
1823 *
1824 * Return nothing.
1825 */
1826void
1827mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1828 u8 type, u16 smid_task, ulong timeout)
1829{
1830 Mpi2SCSITaskManagementRequest_t *mpi_request;
1831 Mpi2SCSITaskManagementReply_t *mpi_reply;
1832 u16 smid = 0;
1833 u32 ioc_state;
1834 unsigned long timeleft;
Eric Moore635374e2009-03-09 01:21:12 -06001835
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301836 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1837 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1838 __func__, ioc->name);
1839 return;
1840 }
1841
1842 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001843 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1844 __func__, ioc->name);
1845 return;
1846 }
Eric Moore635374e2009-03-09 01:21:12 -06001847
1848 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1849 if (ioc_state & MPI2_DOORBELL_USED) {
1850 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1851 "active!\n", ioc->name));
1852 goto issue_host_reset;
1853 }
1854
1855 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1856 mpt2sas_base_fault_info(ioc, ioc_state &
1857 MPI2_DOORBELL_DATA_MASK);
1858 goto issue_host_reset;
1859 }
1860
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301861 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06001862 if (!smid) {
1863 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1864 ioc->name, __func__);
1865 return;
1866 }
1867
1868 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301869 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
1870 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06001871 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1872 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1873 ioc->tm_cmds.smid = smid;
1874 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1875 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1876 mpi_request->DevHandle = cpu_to_le16(handle);
1877 mpi_request->TaskType = type;
1878 mpi_request->TaskMID = cpu_to_le16(smid_task);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301879 mpi_request->VP_ID = 0; /* TODO */
1880 mpi_request->VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001881 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1882 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05301883 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301884 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06001885 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1886 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1887 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1888 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1889 ioc->name, __func__);
1890 _debug_dump_mf(mpi_request,
1891 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1892 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1893 goto issue_host_reset;
1894 }
1895
1896 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1897 mpi_reply = ioc->tm_cmds.reply;
1898 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1899 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1900 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1901 le32_to_cpu(mpi_reply->IOCLogInfo),
1902 le32_to_cpu(mpi_reply->TerminationCount)));
1903 if (ioc->logging_level & MPT_DEBUG_TM)
1904 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1905 }
1906 return;
1907 issue_host_reset:
1908 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1909}
1910
1911/**
Eric Moored5d135b2009-05-18 13:02:08 -06001912 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06001913 * @sdev: scsi device struct
1914 *
1915 * Returns SUCCESS if command aborted else FAILED
1916 */
1917static int
Eric Moored5d135b2009-05-18 13:02:08 -06001918_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001919{
1920 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1921 struct MPT2SAS_DEVICE *sas_device_priv_data;
1922 u16 smid;
1923 u16 handle;
1924 int r;
1925 struct scsi_cmnd *scmd_lookup;
1926
1927 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1928 ioc->name, scmd);
1929 scsi_print_command(scmd);
1930
1931 sas_device_priv_data = scmd->device->hostdata;
1932 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1933 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1934 ioc->name, scmd);
1935 scmd->result = DID_NO_CONNECT << 16;
1936 scmd->scsi_done(scmd);
1937 r = SUCCESS;
1938 goto out;
1939 }
1940
1941 /* search for the command */
1942 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1943 if (!smid) {
1944 scmd->result = DID_RESET << 16;
1945 r = SUCCESS;
1946 goto out;
1947 }
1948
1949 /* for hidden raid components and volumes this is not supported */
1950 if (sas_device_priv_data->sas_target->flags &
1951 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1952 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1953 scmd->result = DID_RESET << 16;
1954 r = FAILED;
1955 goto out;
1956 }
1957
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05301958 mpt2sas_halt_firmware(ioc);
1959
Eric Moore635374e2009-03-09 01:21:12 -06001960 mutex_lock(&ioc->tm_cmds.mutex);
1961 handle = sas_device_priv_data->sas_target->handle;
1962 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1963 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1964
1965 /* sanity check - see whether command actually completed */
1966 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1967 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1968 r = FAILED;
1969 else
1970 r = SUCCESS;
1971 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1972 mutex_unlock(&ioc->tm_cmds.mutex);
1973
1974 out:
1975 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1976 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1977 return r;
1978}
1979
Eric Moore635374e2009-03-09 01:21:12 -06001980/**
Eric Moored5d135b2009-05-18 13:02:08 -06001981 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06001982 * @sdev: scsi device struct
1983 *
1984 * Returns SUCCESS if command aborted else FAILED
1985 */
1986static int
Eric Moored5d135b2009-05-18 13:02:08 -06001987_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001988{
1989 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1990 struct MPT2SAS_DEVICE *sas_device_priv_data;
1991 struct _sas_device *sas_device;
1992 unsigned long flags;
1993 u16 handle;
1994 int r;
1995
Eric Moore993e0da2009-05-18 13:00:45 -06001996 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001997 ioc->name, scmd);
1998 scsi_print_command(scmd);
1999
2000 sas_device_priv_data = scmd->device->hostdata;
2001 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2002 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2003 ioc->name, scmd);
2004 scmd->result = DID_NO_CONNECT << 16;
2005 scmd->scsi_done(scmd);
2006 r = SUCCESS;
2007 goto out;
2008 }
2009
2010 /* for hidden raid components obtain the volume_handle */
2011 handle = 0;
2012 if (sas_device_priv_data->sas_target->flags &
2013 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2014 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2015 sas_device = _scsih_sas_device_find_by_handle(ioc,
2016 sas_device_priv_data->sas_target->handle);
2017 if (sas_device)
2018 handle = sas_device->volume_handle;
2019 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2020 } else
2021 handle = sas_device_priv_data->sas_target->handle;
2022
2023 if (!handle) {
2024 scmd->result = DID_RESET << 16;
2025 r = FAILED;
2026 goto out;
2027 }
2028
2029 mutex_lock(&ioc->tm_cmds.mutex);
2030 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06002031 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2032 30);
2033
2034 /*
2035 * sanity check see whether all commands to this device been
2036 * completed
2037 */
2038 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2039 scmd->device->lun, scmd->device->channel))
2040 r = FAILED;
2041 else
2042 r = SUCCESS;
2043 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2044 mutex_unlock(&ioc->tm_cmds.mutex);
2045
2046 out:
2047 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2048 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2049 return r;
2050}
2051
2052/**
Eric Moored5d135b2009-05-18 13:02:08 -06002053 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002054 * @sdev: scsi device struct
2055 *
2056 * Returns SUCCESS if command aborted else FAILED
2057 */
2058static int
Eric Moored5d135b2009-05-18 13:02:08 -06002059_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002060{
2061 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2062 struct MPT2SAS_DEVICE *sas_device_priv_data;
2063 struct _sas_device *sas_device;
2064 unsigned long flags;
2065 u16 handle;
2066 int r;
2067
2068 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2069 ioc->name, scmd);
2070 scsi_print_command(scmd);
2071
2072 sas_device_priv_data = scmd->device->hostdata;
2073 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2074 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2075 ioc->name, scmd);
2076 scmd->result = DID_NO_CONNECT << 16;
2077 scmd->scsi_done(scmd);
2078 r = SUCCESS;
2079 goto out;
2080 }
2081
2082 /* for hidden raid components obtain the volume_handle */
2083 handle = 0;
2084 if (sas_device_priv_data->sas_target->flags &
2085 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2086 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2087 sas_device = _scsih_sas_device_find_by_handle(ioc,
2088 sas_device_priv_data->sas_target->handle);
2089 if (sas_device)
2090 handle = sas_device->volume_handle;
2091 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2092 } else
2093 handle = sas_device_priv_data->sas_target->handle;
2094
2095 if (!handle) {
2096 scmd->result = DID_RESET << 16;
2097 r = FAILED;
2098 goto out;
2099 }
2100
2101 mutex_lock(&ioc->tm_cmds.mutex);
2102 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002103 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2104
2105 /*
2106 * sanity check see whether all commands to this target been
2107 * completed
2108 */
2109 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2110 scmd->device->channel))
2111 r = FAILED;
2112 else
2113 r = SUCCESS;
2114 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2115 mutex_unlock(&ioc->tm_cmds.mutex);
2116
2117 out:
2118 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2119 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2120 return r;
2121}
2122
2123/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302124 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002125 * @sdev: scsi device struct
2126 *
2127 * Returns SUCCESS if command aborted else FAILED
2128 */
2129static int
Eric Moored5d135b2009-05-18 13:02:08 -06002130_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002131{
2132 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2133 int r, retval;
2134
2135 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2136 ioc->name, scmd);
2137 scsi_print_command(scmd);
2138
2139 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2140 FORCE_BIG_HAMMER);
2141 r = (retval < 0) ? FAILED : SUCCESS;
2142 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2143 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2144
2145 return r;
2146}
2147
2148/**
2149 * _scsih_fw_event_add - insert and queue up fw_event
2150 * @ioc: per adapter object
2151 * @fw_event: object describing the event
2152 * Context: This function will acquire ioc->fw_event_lock.
2153 *
2154 * This adds the firmware event object into link list, then queues it up to
2155 * be processed from user context.
2156 *
2157 * Return nothing.
2158 */
2159static void
2160_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2161{
2162 unsigned long flags;
2163
2164 if (ioc->firmware_event_thread == NULL)
2165 return;
2166
2167 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2168 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002169 INIT_WORK(&fw_event->work, _firmware_event_work);
2170 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002171 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2172}
2173
2174/**
2175 * _scsih_fw_event_free - delete fw_event
2176 * @ioc: per adapter object
2177 * @fw_event: object describing the event
2178 * Context: This function will acquire ioc->fw_event_lock.
2179 *
2180 * This removes firmware event object from link list, frees associated memory.
2181 *
2182 * Return nothing.
2183 */
2184static void
2185_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2186 *fw_event)
2187{
2188 unsigned long flags;
2189
2190 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2191 list_del(&fw_event->list);
2192 kfree(fw_event->event_data);
2193 kfree(fw_event);
2194 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2195}
2196
2197/**
2198 * _scsih_fw_event_add - requeue an event
2199 * @ioc: per adapter object
2200 * @fw_event: object describing the event
2201 * Context: This function will acquire ioc->fw_event_lock.
2202 *
2203 * Return nothing.
2204 */
2205static void
2206_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2207 *fw_event, unsigned long delay)
2208{
2209 unsigned long flags;
2210 if (ioc->firmware_event_thread == NULL)
2211 return;
2212
2213 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002214 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002215 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2216}
2217
2218/**
2219 * _scsih_fw_event_off - turn flag off preventing event handling
2220 * @ioc: per adapter object
2221 *
2222 * Used to prevent handling of firmware events during adapter reset
2223 * driver unload.
2224 *
2225 * Return nothing.
2226 */
2227static void
2228_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2229{
2230 unsigned long flags;
2231
2232 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2233 ioc->fw_events_off = 1;
2234 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2235
2236}
2237
2238/**
2239 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2240 * @ioc: per adapter object
2241 *
2242 * Returns nothing.
2243 */
2244static void
2245_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2246{
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2250 ioc->fw_events_off = 0;
2251 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2252}
2253
2254/**
2255 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2256 * @ioc: per adapter object
2257 * @handle: device handle
2258 *
2259 * During device pull we need to appropiately set the sdev state.
2260 */
2261static void
2262_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2263{
2264 struct MPT2SAS_DEVICE *sas_device_priv_data;
2265 struct scsi_device *sdev;
2266
2267 shost_for_each_device(sdev, ioc->shost) {
2268 sas_device_priv_data = sdev->hostdata;
2269 if (!sas_device_priv_data)
2270 continue;
2271 if (!sas_device_priv_data->block)
2272 continue;
2273 if (sas_device_priv_data->sas_target->handle == handle) {
2274 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2275 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2276 "handle(0x%04x)\n", ioc->name, handle));
2277 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302278 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002279 }
2280 }
2281}
2282
2283/**
2284 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2285 * @ioc: per adapter object
2286 * @handle: device handle
2287 *
2288 * During device pull we need to appropiately set the sdev state.
2289 */
2290static void
2291_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2292{
2293 struct MPT2SAS_DEVICE *sas_device_priv_data;
2294 struct scsi_device *sdev;
2295
2296 shost_for_each_device(sdev, ioc->shost) {
2297 sas_device_priv_data = sdev->hostdata;
2298 if (!sas_device_priv_data)
2299 continue;
2300 if (sas_device_priv_data->block)
2301 continue;
2302 if (sas_device_priv_data->sas_target->handle == handle) {
2303 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2304 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2305 "handle(0x%04x)\n", ioc->name, handle));
2306 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302307 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002308 }
2309 }
2310}
2311
2312/**
2313 * _scsih_block_io_to_children_attached_to_ex
2314 * @ioc: per adapter object
2315 * @sas_expander: the sas_device object
2316 *
2317 * This routine set sdev state to SDEV_BLOCK for all devices
2318 * attached to this expander. This function called when expander is
2319 * pulled.
2320 */
2321static void
2322_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2323 struct _sas_node *sas_expander)
2324{
2325 struct _sas_port *mpt2sas_port;
2326 struct _sas_device *sas_device;
2327 struct _sas_node *expander_sibling;
2328 unsigned long flags;
2329
2330 if (!sas_expander)
2331 return;
2332
2333 list_for_each_entry(mpt2sas_port,
2334 &sas_expander->sas_port_list, port_list) {
2335 if (mpt2sas_port->remote_identify.device_type ==
2336 SAS_END_DEVICE) {
2337 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2338 sas_device =
2339 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2340 mpt2sas_port->remote_identify.sas_address);
2341 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2342 if (!sas_device)
2343 continue;
2344 _scsih_block_io_device(ioc, sas_device->handle);
2345 }
2346 }
2347
2348 list_for_each_entry(mpt2sas_port,
2349 &sas_expander->sas_port_list, port_list) {
2350
2351 if (mpt2sas_port->remote_identify.device_type ==
2352 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2353 mpt2sas_port->remote_identify.device_type ==
2354 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2355
2356 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2357 expander_sibling =
2358 mpt2sas_scsih_expander_find_by_sas_address(
2359 ioc, mpt2sas_port->remote_identify.sas_address);
2360 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2361 _scsih_block_io_to_children_attached_to_ex(ioc,
2362 expander_sibling);
2363 }
2364 }
2365}
2366
2367/**
2368 * _scsih_block_io_to_children_attached_directly
2369 * @ioc: per adapter object
2370 * @event_data: topology change event data
2371 *
2372 * This routine set sdev state to SDEV_BLOCK for all devices
2373 * direct attached during device pull.
2374 */
2375static void
2376_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2377 Mpi2EventDataSasTopologyChangeList_t *event_data)
2378{
2379 int i;
2380 u16 handle;
2381 u16 reason_code;
2382 u8 phy_number;
2383
2384 for (i = 0; i < event_data->NumEntries; i++) {
2385 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2386 if (!handle)
2387 continue;
2388 phy_number = event_data->StartPhyNum + i;
2389 reason_code = event_data->PHY[i].PhyStatus &
2390 MPI2_EVENT_SAS_TOPO_RC_MASK;
2391 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2392 _scsih_block_io_device(ioc, handle);
2393 }
2394}
2395
2396/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302397 * _scsih_tm_tr_send - send task management request
2398 * @ioc: per adapter object
2399 * @handle: device handle
2400 * Context: interrupt time.
2401 *
2402 * This code is to initiate the device removal handshake protocal
2403 * with controller firmware. This function will issue target reset
2404 * using high priority request queue. It will send a sas iounit
2405 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2406 *
2407 * This is designed to send muliple task management request at the same
2408 * time to the fifo. If the fifo is full, we will append the request,
2409 * and process it in a future completion.
2410 */
2411static void
2412_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2413{
2414 Mpi2SCSITaskManagementRequest_t *mpi_request;
2415 struct MPT2SAS_TARGET *sas_target_priv_data;
2416 u16 smid;
2417 struct _sas_device *sas_device;
2418 unsigned long flags;
2419 struct _tr_list *delayed_tr;
2420
2421 if (ioc->shost_recovery) {
2422 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2423 __func__, ioc->name);
2424 return;
2425 }
2426
2427 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2428 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302429 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2430
2431 /* skip is hidden raid component */
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302432 if (sas_device && sas_device->hidden_raid_component)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302433 return;
2434
2435 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2436 if (!smid) {
2437 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2438 if (!delayed_tr)
2439 return;
2440 INIT_LIST_HEAD(&delayed_tr->list);
2441 delayed_tr->handle = handle;
2442 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2443 list_add_tail(&delayed_tr->list,
2444 &ioc->delayed_tr_list);
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302445 if (sas_device && sas_device->starget) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302446 dewtprintk(ioc, starget_printk(KERN_INFO,
2447 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302448 "(open)\n", handle));
2449 } else {
2450 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2451 "DELAYED:tr:handle(0x%04x), (open)\n",
2452 ioc->name, handle));
2453 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302454 return;
2455 }
2456
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302457 if (sas_device) {
2458 sas_device->state |= MPTSAS_STATE_TR_SEND;
2459 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2460 if (sas_device->starget && sas_device->starget->hostdata) {
2461 sas_target_priv_data = sas_device->starget->hostdata;
2462 sas_target_priv_data->tm_busy = 1;
2463 dewtprintk(ioc, starget_printk(KERN_INFO,
2464 sas_device->starget, "tr:handle(0x%04x), (open)\n",
2465 handle));
2466 }
2467 } else {
2468 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2469 "tr:handle(0x%04x), (open)\n", ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302470 }
2471
2472 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2473 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2474 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2475 mpi_request->DevHandle = cpu_to_le16(handle);
2476 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302477 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2478}
2479
2480
2481
2482/**
2483 * _scsih_sas_control_complete - completion routine
2484 * @ioc: per adapter object
2485 * @smid: system request message index
2486 * @msix_index: MSIX table index supplied by the OS
2487 * @reply: reply message frame(lower 32bit addr)
2488 * Context: interrupt time.
2489 *
2490 * This is the sas iounit controll completion routine.
2491 * This code is part of the code to initiate the device removal
2492 * handshake protocal with controller firmware.
2493 *
2494 * Return 1 meaning mf should be freed from _base_interrupt
2495 * 0 means the mf is freed from this function.
2496 */
2497static u8
2498_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2499 u8 msix_index, u32 reply)
2500{
2501 unsigned long flags;
2502 u16 handle;
2503 struct _sas_device *sas_device;
2504 Mpi2SasIoUnitControlReply_t *mpi_reply =
2505 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2506
2507 handle = le16_to_cpu(mpi_reply->DevHandle);
2508
2509 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2510 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302511 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2512
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302513 if (sas_device) {
2514 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2515 if (sas_device->starget)
2516 dewtprintk(ioc, starget_printk(KERN_INFO,
2517 sas_device->starget,
2518 "sc_complete:handle(0x%04x), "
2519 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2520 handle, le16_to_cpu(mpi_reply->IOCStatus),
2521 le32_to_cpu(mpi_reply->IOCLogInfo)));
2522 } else {
2523 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302524 "sc_complete:handle(0x%04x), "
2525 "ioc_status(0x%04x), loginfo(0x%08x)\n",
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302526 ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302527 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302528 }
2529
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302530 return 1;
2531}
2532
2533/**
2534 * _scsih_tm_tr_complete -
2535 * @ioc: per adapter object
2536 * @smid: system request message index
2537 * @msix_index: MSIX table index supplied by the OS
2538 * @reply: reply message frame(lower 32bit addr)
2539 * Context: interrupt time.
2540 *
2541 * This is the target reset completion routine.
2542 * This code is part of the code to initiate the device removal
2543 * handshake protocal with controller firmware.
2544 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2545 *
2546 * Return 1 meaning mf should be freed from _base_interrupt
2547 * 0 means the mf is freed from this function.
2548 */
2549static u8
2550_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2551 u32 reply)
2552{
2553 unsigned long flags;
2554 u16 handle;
2555 struct _sas_device *sas_device;
2556 Mpi2SCSITaskManagementReply_t *mpi_reply =
2557 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2558 Mpi2SasIoUnitControlRequest_t *mpi_request;
2559 u16 smid_sas_ctrl;
2560 struct MPT2SAS_TARGET *sas_target_priv_data;
2561 struct _tr_list *delayed_tr;
2562 u8 rc;
2563
2564 handle = le16_to_cpu(mpi_reply->DevHandle);
2565 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2566 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302567 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2568
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302569 if (sas_device) {
2570 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2571 if (sas_device->starget) {
2572 dewtprintk(ioc, starget_printk(KERN_INFO,
2573 sas_device->starget, "tr_complete:handle(0x%04x), "
2574 "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
2575 "completed(%d)\n", sas_device->handle,
2576 (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
2577 "open" : "active",
2578 le16_to_cpu(mpi_reply->IOCStatus),
2579 le32_to_cpu(mpi_reply->IOCLogInfo),
2580 le32_to_cpu(mpi_reply->TerminationCount)));
2581 if (sas_device->starget->hostdata) {
2582 sas_target_priv_data =
2583 sas_device->starget->hostdata;
2584 sas_target_priv_data->tm_busy = 0;
2585 }
2586 }
2587 } else {
2588 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2589 "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
2590 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2591 handle, le16_to_cpu(mpi_reply->IOCStatus),
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302592 le32_to_cpu(mpi_reply->IOCLogInfo),
2593 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302594 }
2595
2596 if (!list_empty(&ioc->delayed_tr_list)) {
2597 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2598 struct _tr_list, list);
2599 mpt2sas_base_free_smid(ioc, smid);
2600 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2601 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2602 list_del(&delayed_tr->list);
2603 kfree(delayed_tr);
2604 rc = 0; /* tells base_interrupt not to free mf */
2605 } else
2606 rc = 1;
2607
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302608 if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302609 return rc;
2610
2611 if (ioc->shost_recovery) {
2612 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2613 __func__, ioc->name);
2614 return rc;
2615 }
2616
2617 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2618 if (!smid_sas_ctrl) {
2619 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2620 ioc->name, __func__);
2621 return rc;
2622 }
2623
Kashyap, Desaia28eb222009-09-23 17:22:37 +05302624 if (sas_device)
2625 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2626
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302627 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2628 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2629 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2630 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2631 mpi_request->DevHandle = mpi_reply->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302632 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2633 return rc;
2634}
2635
2636/**
Eric Moore635374e2009-03-09 01:21:12 -06002637 * _scsih_check_topo_delete_events - sanity check on topo events
2638 * @ioc: per adapter object
2639 * @event_data: the event data payload
2640 *
2641 * This routine added to better handle cable breaker.
2642 *
2643 * This handles the case where driver recieves multiple expander
2644 * add and delete events in a single shot. When there is a delete event
2645 * the routine will void any pending add events waiting in the event queue.
2646 *
2647 * Return nothing.
2648 */
2649static void
2650_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2651 Mpi2EventDataSasTopologyChangeList_t *event_data)
2652{
2653 struct fw_event_work *fw_event;
2654 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2655 u16 expander_handle;
2656 struct _sas_node *sas_expander;
2657 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302658 int i, reason_code;
2659 u16 handle;
2660
2661 for (i = 0 ; i < event_data->NumEntries; i++) {
2662 if (event_data->PHY[i].PhyStatus &
2663 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2664 continue;
2665 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2666 if (!handle)
2667 continue;
2668 reason_code = event_data->PHY[i].PhyStatus &
2669 MPI2_EVENT_SAS_TOPO_RC_MASK;
2670 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2671 _scsih_tm_tr_send(ioc, handle);
2672 }
Eric Moore635374e2009-03-09 01:21:12 -06002673
2674 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2675 if (expander_handle < ioc->sas_hba.num_phys) {
2676 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2677 return;
2678 }
2679
2680 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2681 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2682 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2683 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2684 expander_handle);
2685 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2686 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2687 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2688 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2689
2690 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2691 return;
2692
2693 /* mark ignore flag for pending events */
2694 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2695 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2696 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2697 fw_event->ignore)
2698 continue;
2699 local_event_data = fw_event->event_data;
2700 if (local_event_data->ExpStatus ==
2701 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2702 local_event_data->ExpStatus ==
2703 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2704 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2705 expander_handle) {
2706 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2707 "setting ignoring flag\n", ioc->name));
2708 fw_event->ignore = 1;
2709 }
2710 }
2711 }
2712 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2713}
2714
2715/**
Eric Moore635374e2009-03-09 01:21:12 -06002716 * _scsih_flush_running_cmds - completing outstanding commands.
2717 * @ioc: per adapter object
2718 *
2719 * The flushing out of all pending scmd commands following host reset,
2720 * where all IO is dropped to the floor.
2721 *
2722 * Return nothing.
2723 */
2724static void
2725_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2726{
2727 struct scsi_cmnd *scmd;
2728 u16 smid;
2729 u16 count = 0;
2730
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302731 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2732 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002733 if (!scmd)
2734 continue;
2735 count++;
2736 mpt2sas_base_free_smid(ioc, smid);
2737 scsi_dma_unmap(scmd);
2738 scmd->result = DID_RESET << 16;
2739 scmd->scsi_done(scmd);
2740 }
2741 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2742 ioc->name, count));
2743}
2744
2745/**
Eric Moore3c621b32009-05-18 12:59:41 -06002746 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2747 * @scmd: pointer to scsi command object
2748 * @mpi_request: pointer to the SCSI_IO reqest message frame
2749 *
2750 * Supporting protection 1 and 3.
2751 *
2752 * Returns nothing
2753 */
2754static void
2755_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2756{
2757 u16 eedp_flags;
2758 unsigned char prot_op = scsi_get_prot_op(scmd);
2759 unsigned char prot_type = scsi_get_prot_type(scmd);
2760
2761 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2762 prot_type == SCSI_PROT_DIF_TYPE2 ||
2763 prot_op == SCSI_PROT_NORMAL)
2764 return;
2765
2766 if (prot_op == SCSI_PROT_READ_STRIP)
2767 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2768 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2769 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2770 else
2771 return;
2772
Eric Moore3c621b32009-05-18 12:59:41 -06002773 switch (prot_type) {
2774 case SCSI_PROT_DIF_TYPE1:
2775
2776 /*
2777 * enable ref/guard checking
2778 * auto increment ref tag
2779 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302780 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06002781 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2782 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2783 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2784 cpu_to_be32(scsi_get_lba(scmd));
2785
2786 break;
2787
2788 case SCSI_PROT_DIF_TYPE3:
2789
2790 /*
2791 * enable guard checking
2792 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05302793 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06002794 break;
2795 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05302796 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
2797 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06002798}
2799
2800/**
2801 * _scsih_eedp_error_handling - return sense code for EEDP errors
2802 * @scmd: pointer to scsi command object
2803 * @ioc_status: ioc status
2804 *
2805 * Returns nothing
2806 */
2807static void
2808_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2809{
2810 u8 ascq;
2811 u8 sk;
2812 u8 host_byte;
2813
2814 switch (ioc_status) {
2815 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2816 ascq = 0x01;
2817 break;
2818 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2819 ascq = 0x02;
2820 break;
2821 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2822 ascq = 0x03;
2823 break;
2824 default:
2825 ascq = 0x00;
2826 break;
2827 }
2828
2829 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2830 sk = ILLEGAL_REQUEST;
2831 host_byte = DID_ABORT;
2832 } else {
2833 sk = ABORTED_COMMAND;
2834 host_byte = DID_OK;
2835 }
2836
2837 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2838 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2839 SAM_STAT_CHECK_CONDITION;
2840}
2841
2842/**
Eric Moored5d135b2009-05-18 13:02:08 -06002843 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002844 * @scmd: pointer to scsi command object
2845 * @done: function pointer to be invoked on completion
2846 *
2847 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2848 *
2849 * Returns 0 on success. If there's a failure, return either:
2850 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2851 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2852 */
2853static int
Eric Moored5d135b2009-05-18 13:02:08 -06002854_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002855{
2856 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2857 struct MPT2SAS_DEVICE *sas_device_priv_data;
2858 struct MPT2SAS_TARGET *sas_target_priv_data;
2859 Mpi2SCSIIORequest_t *mpi_request;
2860 u32 mpi_control;
2861 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002862
2863 scmd->scsi_done = done;
2864 sas_device_priv_data = scmd->device->hostdata;
2865 if (!sas_device_priv_data) {
2866 scmd->result = DID_NO_CONNECT << 16;
2867 scmd->scsi_done(scmd);
2868 return 0;
2869 }
2870
2871 sas_target_priv_data = sas_device_priv_data->sas_target;
2872 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2873 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2874 scmd->result = DID_NO_CONNECT << 16;
2875 scmd->scsi_done(scmd);
2876 return 0;
2877 }
2878
2879 /* see if we are busy with task managment stuff */
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05302880 if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302881 return SCSI_MLQUEUE_DEVICE_BUSY;
2882 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002883 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06002884
2885 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2886 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2887 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2888 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2889 else
2890 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2891
2892 /* set tags */
2893 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2894 if (scmd->device->tagged_supported) {
2895 if (scmd->device->ordered_tags)
2896 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2897 else
2898 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2899 } else
2900/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2901/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2902 */
2903 mpi_control |= (0x500);
2904
2905 } else
2906 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2907
2908 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2909 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2910
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302911 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002912 if (!smid) {
2913 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2914 ioc->name, __func__);
2915 goto out;
2916 }
2917 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2918 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06002919 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06002920 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2921 if (sas_device_priv_data->sas_target->flags &
2922 MPT_TARGET_FLAGS_RAID_COMPONENT)
2923 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2924 else
2925 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2926 mpi_request->DevHandle =
2927 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2928 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2929 mpi_request->Control = cpu_to_le32(mpi_control);
2930 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2931 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2932 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2933 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05302934 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002935 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2936 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2937 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302938 mpi_request->VF_ID = 0; /* TODO */
2939 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002940 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2941 mpi_request->LUN);
2942 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2943
2944 if (!mpi_request->DataLength) {
2945 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2946 } else {
2947 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2948 mpt2sas_base_free_smid(ioc, smid);
2949 goto out;
2950 }
2951 }
2952
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302953 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Eric Moore635374e2009-03-09 01:21:12 -06002954 sas_device_priv_data->sas_target->handle);
2955 return 0;
2956
2957 out:
2958 return SCSI_MLQUEUE_HOST_BUSY;
2959}
2960
2961/**
2962 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2963 * @sense_buffer: sense data returned by target
2964 * @data: normalized skey/asc/ascq
2965 *
2966 * Return nothing.
2967 */
2968static void
2969_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2970{
2971 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2972 /* descriptor format */
2973 data->skey = sense_buffer[1] & 0x0F;
2974 data->asc = sense_buffer[2];
2975 data->ascq = sense_buffer[3];
2976 } else {
2977 /* fixed format */
2978 data->skey = sense_buffer[2] & 0x0F;
2979 data->asc = sense_buffer[12];
2980 data->ascq = sense_buffer[13];
2981 }
2982}
2983
2984#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2985/**
2986 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2987 * @ioc: per adapter object
2988 * @scmd: pointer to scsi command object
2989 * @mpi_reply: reply mf payload returned from firmware
2990 *
2991 * scsi_status - SCSI Status code returned from target device
2992 * scsi_state - state info associated with SCSI_IO determined by ioc
2993 * ioc_status - ioc supplied status info
2994 *
2995 * Return nothing.
2996 */
2997static void
2998_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2999 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3000{
3001 u32 response_info;
3002 u8 *response_bytes;
3003 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3004 MPI2_IOCSTATUS_MASK;
3005 u8 scsi_state = mpi_reply->SCSIState;
3006 u8 scsi_status = mpi_reply->SCSIStatus;
3007 char *desc_ioc_state = NULL;
3008 char *desc_scsi_status = NULL;
3009 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303010 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3011
3012 if (log_info == 0x31170000)
3013 return;
Eric Moore635374e2009-03-09 01:21:12 -06003014
3015 switch (ioc_status) {
3016 case MPI2_IOCSTATUS_SUCCESS:
3017 desc_ioc_state = "success";
3018 break;
3019 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3020 desc_ioc_state = "invalid function";
3021 break;
3022 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3023 desc_ioc_state = "scsi recovered error";
3024 break;
3025 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3026 desc_ioc_state = "scsi invalid dev handle";
3027 break;
3028 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3029 desc_ioc_state = "scsi device not there";
3030 break;
3031 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3032 desc_ioc_state = "scsi data overrun";
3033 break;
3034 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3035 desc_ioc_state = "scsi data underrun";
3036 break;
3037 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3038 desc_ioc_state = "scsi io data error";
3039 break;
3040 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3041 desc_ioc_state = "scsi protocol error";
3042 break;
3043 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3044 desc_ioc_state = "scsi task terminated";
3045 break;
3046 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3047 desc_ioc_state = "scsi residual mismatch";
3048 break;
3049 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3050 desc_ioc_state = "scsi task mgmt failed";
3051 break;
3052 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3053 desc_ioc_state = "scsi ioc terminated";
3054 break;
3055 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3056 desc_ioc_state = "scsi ext terminated";
3057 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003058 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3059 desc_ioc_state = "eedp guard error";
3060 break;
3061 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3062 desc_ioc_state = "eedp ref tag error";
3063 break;
3064 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3065 desc_ioc_state = "eedp app tag error";
3066 break;
Eric Moore635374e2009-03-09 01:21:12 -06003067 default:
3068 desc_ioc_state = "unknown";
3069 break;
3070 }
3071
3072 switch (scsi_status) {
3073 case MPI2_SCSI_STATUS_GOOD:
3074 desc_scsi_status = "good";
3075 break;
3076 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3077 desc_scsi_status = "check condition";
3078 break;
3079 case MPI2_SCSI_STATUS_CONDITION_MET:
3080 desc_scsi_status = "condition met";
3081 break;
3082 case MPI2_SCSI_STATUS_BUSY:
3083 desc_scsi_status = "busy";
3084 break;
3085 case MPI2_SCSI_STATUS_INTERMEDIATE:
3086 desc_scsi_status = "intermediate";
3087 break;
3088 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3089 desc_scsi_status = "intermediate condmet";
3090 break;
3091 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3092 desc_scsi_status = "reservation conflict";
3093 break;
3094 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3095 desc_scsi_status = "command terminated";
3096 break;
3097 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3098 desc_scsi_status = "task set full";
3099 break;
3100 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3101 desc_scsi_status = "aca active";
3102 break;
3103 case MPI2_SCSI_STATUS_TASK_ABORTED:
3104 desc_scsi_status = "task aborted";
3105 break;
3106 default:
3107 desc_scsi_status = "unknown";
3108 break;
3109 }
3110
3111 desc_scsi_state[0] = '\0';
3112 if (!scsi_state)
3113 desc_scsi_state = " ";
3114 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3115 strcat(desc_scsi_state, "response info ");
3116 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3117 strcat(desc_scsi_state, "state terminated ");
3118 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3119 strcat(desc_scsi_state, "no status ");
3120 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3121 strcat(desc_scsi_state, "autosense failed ");
3122 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3123 strcat(desc_scsi_state, "autosense valid ");
3124
3125 scsi_print_command(scmd);
3126 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3127 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3128 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3129 ioc_status, smid);
3130 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3131 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3132 scsi_get_resid(scmd));
3133 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3134 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3135 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3136 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3137 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3138 scsi_status, desc_scsi_state, scsi_state);
3139
3140 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3141 struct sense_info data;
3142 _scsih_normalize_sense(scmd->sense_buffer, &data);
3143 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3144 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3145 data.asc, data.ascq);
3146 }
3147
3148 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3149 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3150 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303151 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003152 }
3153}
3154#endif
3155
3156/**
3157 * _scsih_smart_predicted_fault - illuminate Fault LED
3158 * @ioc: per adapter object
3159 * @handle: device handle
3160 *
3161 * Return nothing.
3162 */
3163static void
3164_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3165{
3166 Mpi2SepReply_t mpi_reply;
3167 Mpi2SepRequest_t mpi_request;
3168 struct scsi_target *starget;
3169 struct MPT2SAS_TARGET *sas_target_priv_data;
3170 Mpi2EventNotificationReply_t *event_reply;
3171 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3172 struct _sas_device *sas_device;
3173 ssize_t sz;
3174 unsigned long flags;
3175
3176 /* only handle non-raid devices */
3177 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3178 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3179 if (!sas_device) {
3180 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3181 return;
3182 }
3183 starget = sas_device->starget;
3184 sas_target_priv_data = starget->hostdata;
3185
3186 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3187 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3188 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3189 return;
3190 }
3191 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3193
3194 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3195 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3196 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3197 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3198 mpi_request.SlotStatus =
3199 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3200 mpi_request.DevHandle = cpu_to_le16(handle);
3201 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3202 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3203 &mpi_request)) != 0) {
3204 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3205 ioc->name, __FILE__, __LINE__, __func__);
3206 return;
3207 }
3208
3209 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3210 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3211 "enclosure_processor: ioc_status (0x%04x), "
3212 "loginfo(0x%08x)\n", ioc->name,
3213 le16_to_cpu(mpi_reply.IOCStatus),
3214 le32_to_cpu(mpi_reply.IOCLogInfo)));
3215 return;
3216 }
3217 }
3218
3219 /* insert into event log */
3220 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3221 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3222 event_reply = kzalloc(sz, GFP_KERNEL);
3223 if (!event_reply) {
3224 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3225 ioc->name, __FILE__, __LINE__, __func__);
3226 return;
3227 }
3228
3229 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3230 event_reply->Event =
3231 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3232 event_reply->MsgLength = sz/4;
3233 event_reply->EventDataLength =
3234 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3235 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3236 event_reply->EventData;
3237 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3238 event_data->ASC = 0x5D;
3239 event_data->DevHandle = cpu_to_le16(handle);
3240 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3241 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3242 kfree(event_reply);
3243}
3244
3245/**
Eric Moored5d135b2009-05-18 13:02:08 -06003246 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003247 * @ioc: per adapter object
3248 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303249 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003250 * @reply: reply message frame(lower 32bit addr)
3251 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303252 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003253 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303254 * Return 1 meaning mf should be freed from _base_interrupt
3255 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003256 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303257static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303258_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003259{
3260 Mpi2SCSIIORequest_t *mpi_request;
3261 Mpi2SCSIIOReply_t *mpi_reply;
3262 struct scsi_cmnd *scmd;
3263 u16 ioc_status;
3264 u32 xfer_cnt;
3265 u8 scsi_state;
3266 u8 scsi_status;
3267 u32 log_info;
3268 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303269 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003270
3271 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303272 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003273 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303274 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003275
3276 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3277
3278 if (mpi_reply == NULL) {
3279 scmd->result = DID_OK << 16;
3280 goto out;
3281 }
3282
3283 sas_device_priv_data = scmd->device->hostdata;
3284 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3285 sas_device_priv_data->sas_target->deleted) {
3286 scmd->result = DID_NO_CONNECT << 16;
3287 goto out;
3288 }
3289
3290 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303291 scsi_state = mpi_reply->SCSIState;
3292 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3293 response_code =
3294 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003295 if (!sas_device_priv_data->tlr_snoop_check) {
3296 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303297 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3298 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3299 sas_device_priv_data->flags &=
3300 ~MPT_DEVICE_TLR_ON;
Eric Moore635374e2009-03-09 01:21:12 -06003301 }
3302
3303 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3304 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3305 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3306 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3307 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3308 else
3309 log_info = 0;
3310 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003311 scsi_status = mpi_reply->SCSIStatus;
3312
3313 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3314 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3315 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3316 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3317 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3318 }
3319
3320 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3321 struct sense_info data;
3322 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3323 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003324 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003325 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003326 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003327 _scsih_normalize_sense(scmd->sense_buffer, &data);
3328 /* failure prediction threshold exceeded */
3329 if (data.asc == 0x5D)
3330 _scsih_smart_predicted_fault(ioc,
3331 le16_to_cpu(mpi_reply->DevHandle));
3332 }
3333
3334 switch (ioc_status) {
3335 case MPI2_IOCSTATUS_BUSY:
3336 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3337 scmd->result = SAM_STAT_BUSY;
3338 break;
3339
3340 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3341 scmd->result = DID_NO_CONNECT << 16;
3342 break;
3343
3344 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3345 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303346 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3347 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003348 }
Eric Moore635374e2009-03-09 01:21:12 -06003349 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3350 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3351 scmd->result = DID_RESET << 16;
3352 break;
3353
3354 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3355 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3356 scmd->result = DID_SOFT_ERROR << 16;
3357 else
3358 scmd->result = (DID_OK << 16) | scsi_status;
3359 break;
3360
3361 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3362 scmd->result = (DID_OK << 16) | scsi_status;
3363
3364 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3365 break;
3366
3367 if (xfer_cnt < scmd->underflow) {
3368 if (scsi_status == SAM_STAT_BUSY)
3369 scmd->result = SAM_STAT_BUSY;
3370 else
3371 scmd->result = DID_SOFT_ERROR << 16;
3372 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3373 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3374 scmd->result = DID_SOFT_ERROR << 16;
3375 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3376 scmd->result = DID_RESET << 16;
3377 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3378 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3379 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3380 scmd->result = (DRIVER_SENSE << 24) |
3381 SAM_STAT_CHECK_CONDITION;
3382 scmd->sense_buffer[0] = 0x70;
3383 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3384 scmd->sense_buffer[12] = 0x20;
3385 scmd->sense_buffer[13] = 0;
3386 }
3387 break;
3388
3389 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3390 scsi_set_resid(scmd, 0);
3391 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3392 case MPI2_IOCSTATUS_SUCCESS:
3393 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303394 if (response_code ==
3395 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3396 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3397 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003398 scmd->result = DID_SOFT_ERROR << 16;
3399 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3400 scmd->result = DID_RESET << 16;
3401 break;
3402
Eric Moore3c621b32009-05-18 12:59:41 -06003403 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3404 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3405 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3406 _scsih_eedp_error_handling(scmd, ioc_status);
3407 break;
Eric Moore635374e2009-03-09 01:21:12 -06003408 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3409 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3410 case MPI2_IOCSTATUS_INVALID_SGL:
3411 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3412 case MPI2_IOCSTATUS_INVALID_FIELD:
3413 case MPI2_IOCSTATUS_INVALID_STATE:
3414 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3415 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3416 default:
3417 scmd->result = DID_SOFT_ERROR << 16;
3418 break;
3419
3420 }
3421
3422#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3423 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3424 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3425#endif
3426
3427 out:
3428 scsi_dma_unmap(scmd);
3429 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303430 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003431}
3432
3433/**
Eric Moore635374e2009-03-09 01:21:12 -06003434 * _scsih_sas_host_refresh - refreshing sas host object contents
3435 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003436 * Context: user
3437 *
3438 * During port enable, fw will send topology events for every device. Its
3439 * possible that the handles may change from the previous setting, so this
3440 * code keeping handles updating if changed.
3441 *
3442 * Return nothing.
3443 */
3444static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303445_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003446{
3447 u16 sz;
3448 u16 ioc_status;
3449 int i;
3450 Mpi2ConfigReply_t mpi_reply;
3451 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303452 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003453
3454 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3455 "updating handles for sas_host(0x%016llx)\n",
3456 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3457
3458 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3459 * sizeof(Mpi2SasIOUnit0PhyData_t));
3460 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3461 if (!sas_iounit_pg0) {
3462 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3463 ioc->name, __FILE__, __LINE__, __func__);
3464 return;
3465 }
Eric Moore635374e2009-03-09 01:21:12 -06003466
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303467 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3468 sas_iounit_pg0, sz)) != 0)
3469 goto out;
3470 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3471 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3472 goto out;
3473 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3474 if (i == 0)
3475 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3476 PhyData[0].ControllerDevHandle);
3477 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3478 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3479 AttachedDevHandle);
3480 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3481 attached_handle, i, sas_iounit_pg0->PhyData[i].
3482 NegotiatedLinkRate >> 4);
3483 }
Eric Moore635374e2009-03-09 01:21:12 -06003484 out:
3485 kfree(sas_iounit_pg0);
3486}
3487
3488/**
3489 * _scsih_sas_host_add - create sas host object
3490 * @ioc: per adapter object
3491 *
3492 * Creating host side data object, stored in ioc->sas_hba
3493 *
3494 * Return nothing.
3495 */
3496static void
3497_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3498{
3499 int i;
3500 Mpi2ConfigReply_t mpi_reply;
3501 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3502 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3503 Mpi2SasPhyPage0_t phy_pg0;
3504 Mpi2SasDevicePage0_t sas_device_pg0;
3505 Mpi2SasEnclosurePage0_t enclosure_pg0;
3506 u16 ioc_status;
3507 u16 sz;
3508 u16 device_missing_delay;
3509
3510 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3511 if (!ioc->sas_hba.num_phys) {
3512 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3513 ioc->name, __FILE__, __LINE__, __func__);
3514 return;
3515 }
3516
3517 /* sas_iounit page 0 */
3518 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3519 sizeof(Mpi2SasIOUnit0PhyData_t));
3520 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3521 if (!sas_iounit_pg0) {
3522 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3523 ioc->name, __FILE__, __LINE__, __func__);
3524 return;
3525 }
3526 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3527 sas_iounit_pg0, sz))) {
3528 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3529 ioc->name, __FILE__, __LINE__, __func__);
3530 goto out;
3531 }
3532 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3533 MPI2_IOCSTATUS_MASK;
3534 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3535 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3536 ioc->name, __FILE__, __LINE__, __func__);
3537 goto out;
3538 }
3539
3540 /* sas_iounit page 1 */
3541 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3542 sizeof(Mpi2SasIOUnit1PhyData_t));
3543 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3544 if (!sas_iounit_pg1) {
3545 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3546 ioc->name, __FILE__, __LINE__, __func__);
3547 goto out;
3548 }
3549 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3550 sas_iounit_pg1, sz))) {
3551 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3552 ioc->name, __FILE__, __LINE__, __func__);
3553 goto out;
3554 }
3555 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3556 MPI2_IOCSTATUS_MASK;
3557 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3558 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3559 ioc->name, __FILE__, __LINE__, __func__);
3560 goto out;
3561 }
3562
3563 ioc->io_missing_delay =
3564 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3565 device_missing_delay =
3566 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3567 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3568 ioc->device_missing_delay = (device_missing_delay &
3569 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3570 else
3571 ioc->device_missing_delay = device_missing_delay &
3572 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3573
3574 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3575 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3576 sizeof(struct _sas_phy), GFP_KERNEL);
3577 if (!ioc->sas_hba.phy) {
3578 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3579 ioc->name, __FILE__, __LINE__, __func__);
3580 goto out;
3581 }
3582 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3583 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3584 i))) {
3585 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3586 ioc->name, __FILE__, __LINE__, __func__);
3587 goto out;
3588 }
3589 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3590 MPI2_IOCSTATUS_MASK;
3591 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3592 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3593 ioc->name, __FILE__, __LINE__, __func__);
3594 goto out;
3595 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303596
3597 if (i == 0)
3598 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3599 PhyData[0].ControllerDevHandle);
3600 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06003601 ioc->sas_hba.phy[i].phy_id = i;
3602 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3603 phy_pg0, ioc->sas_hba.parent_dev);
3604 }
3605 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303606 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06003607 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3608 ioc->name, __FILE__, __LINE__, __func__);
3609 goto out;
3610 }
Eric Moore635374e2009-03-09 01:21:12 -06003611 ioc->sas_hba.enclosure_handle =
3612 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3613 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3614 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3615 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3616 (unsigned long long) ioc->sas_hba.sas_address,
3617 ioc->sas_hba.num_phys) ;
3618
3619 if (ioc->sas_hba.enclosure_handle) {
3620 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3621 &enclosure_pg0,
3622 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3623 ioc->sas_hba.enclosure_handle))) {
3624 ioc->sas_hba.enclosure_logical_id =
3625 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3626 }
3627 }
3628
3629 out:
3630 kfree(sas_iounit_pg1);
3631 kfree(sas_iounit_pg0);
3632}
3633
3634/**
3635 * _scsih_expander_add - creating expander object
3636 * @ioc: per adapter object
3637 * @handle: expander handle
3638 *
3639 * Creating expander object, stored in ioc->sas_expander_list.
3640 *
3641 * Return 0 for success, else error.
3642 */
3643static int
3644_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3645{
3646 struct _sas_node *sas_expander;
3647 Mpi2ConfigReply_t mpi_reply;
3648 Mpi2ExpanderPage0_t expander_pg0;
3649 Mpi2ExpanderPage1_t expander_pg1;
3650 Mpi2SasEnclosurePage0_t enclosure_pg0;
3651 u32 ioc_status;
3652 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303653 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003654 int i;
3655 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303656 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003657 int rc = 0;
3658
3659 if (!handle)
3660 return -1;
3661
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303662 if (ioc->shost_recovery)
3663 return -1;
3664
Eric Moore635374e2009-03-09 01:21:12 -06003665 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3666 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3667 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3668 ioc->name, __FILE__, __LINE__, __func__);
3669 return -1;
3670 }
3671
3672 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3673 MPI2_IOCSTATUS_MASK;
3674 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3675 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3676 ioc->name, __FILE__, __LINE__, __func__);
3677 return -1;
3678 }
3679
3680 /* handle out of order topology events */
3681 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303682 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3683 != 0) {
3684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3685 ioc->name, __FILE__, __LINE__, __func__);
3686 return -1;
3687 }
3688 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06003689 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303690 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3691 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003692 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3693 if (!sas_expander) {
3694 rc = _scsih_expander_add(ioc, parent_handle);
3695 if (rc != 0)
3696 return rc;
3697 }
3698 }
3699
Eric Moore635374e2009-03-09 01:21:12 -06003700 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303701 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06003702 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3703 sas_address);
3704 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3705
3706 if (sas_expander)
3707 return 0;
3708
3709 sas_expander = kzalloc(sizeof(struct _sas_node),
3710 GFP_KERNEL);
3711 if (!sas_expander) {
3712 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3713 ioc->name, __FILE__, __LINE__, __func__);
3714 return -1;
3715 }
3716
3717 sas_expander->handle = handle;
3718 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303719 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06003720 sas_expander->sas_address = sas_address;
3721
3722 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3723 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303724 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06003725 sas_expander->sas_address, sas_expander->num_phys);
3726
3727 if (!sas_expander->num_phys)
3728 goto out_fail;
3729 sas_expander->phy = kcalloc(sas_expander->num_phys,
3730 sizeof(struct _sas_phy), GFP_KERNEL);
3731 if (!sas_expander->phy) {
3732 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3733 ioc->name, __FILE__, __LINE__, __func__);
3734 rc = -1;
3735 goto out_fail;
3736 }
3737
3738 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3739 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303740 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003741 if (!mpt2sas_port) {
3742 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3743 ioc->name, __FILE__, __LINE__, __func__);
3744 rc = -1;
3745 goto out_fail;
3746 }
3747 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3748
3749 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3750 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3751 &expander_pg1, i, handle))) {
3752 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3753 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303754 rc = -1;
3755 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003756 }
3757 sas_expander->phy[i].handle = handle;
3758 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303759
3760 if ((mpt2sas_transport_add_expander_phy(ioc,
3761 &sas_expander->phy[i], expander_pg1,
3762 sas_expander->parent_dev))) {
3763 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3764 ioc->name, __FILE__, __LINE__, __func__);
3765 rc = -1;
3766 goto out_fail;
3767 }
Eric Moore635374e2009-03-09 01:21:12 -06003768 }
3769
3770 if (sas_expander->enclosure_handle) {
3771 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3772 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3773 sas_expander->enclosure_handle))) {
3774 sas_expander->enclosure_logical_id =
3775 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3776 }
3777 }
3778
3779 _scsih_expander_node_add(ioc, sas_expander);
3780 return 0;
3781
3782 out_fail:
3783
Kashyap, Desai20f58952009-08-07 19:34:26 +05303784 if (mpt2sas_port)
3785 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303786 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06003787 kfree(sas_expander);
3788 return rc;
3789}
3790
3791/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05303792 * _scsih_done - scsih callback handler.
3793 * @ioc: per adapter object
3794 * @smid: system request message index
3795 * @msix_index: MSIX table index supplied by the OS
3796 * @reply: reply message frame(lower 32bit addr)
3797 *
3798 * Callback handler when sending internal generated message frames.
3799 * The callback index passed is `ioc->scsih_cb_idx`
3800 *
3801 * Return 1 meaning mf should be freed from _base_interrupt
3802 * 0 means the mf is freed from this function.
3803 */
3804static u8
3805_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
3806{
3807 MPI2DefaultReply_t *mpi_reply;
3808
3809 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3810 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
3811 return 1;
3812 if (ioc->scsih_cmds.smid != smid)
3813 return 1;
3814 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
3815 if (mpi_reply) {
3816 memcpy(ioc->scsih_cmds.reply, mpi_reply,
3817 mpi_reply->MsgLength*4);
3818 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
3819 }
3820 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
3821 complete(&ioc->scsih_cmds.done);
3822 return 1;
3823}
3824
3825/**
Eric Moore635374e2009-03-09 01:21:12 -06003826 * _scsih_expander_remove - removing expander object
3827 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303828 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06003829 *
3830 * Return nothing.
3831 */
3832static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303833_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06003834{
3835 struct _sas_node *sas_expander;
3836 unsigned long flags;
3837
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303838 if (ioc->shost_recovery)
3839 return;
3840
Eric Moore635374e2009-03-09 01:21:12 -06003841 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303842 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3843 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003844 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3845 _scsih_expander_node_remove(ioc, sas_expander);
3846}
3847
3848/**
3849 * _scsih_add_device - creating sas device object
3850 * @ioc: per adapter object
3851 * @handle: sas device handle
3852 * @phy_num: phy number end device attached to
3853 * @is_pd: is this hidden raid component
3854 *
3855 * Creating end device object, stored in ioc->sas_device_list.
3856 *
3857 * Returns 0 for success, non-zero for failure.
3858 */
3859static int
3860_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3861{
3862 Mpi2ConfigReply_t mpi_reply;
3863 Mpi2SasDevicePage0_t sas_device_pg0;
3864 Mpi2SasEnclosurePage0_t enclosure_pg0;
3865 struct _sas_device *sas_device;
3866 u32 ioc_status;
3867 __le64 sas_address;
3868 u32 device_info;
3869 unsigned long flags;
3870
3871 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3872 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3873 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3874 ioc->name, __FILE__, __LINE__, __func__);
3875 return -1;
3876 }
3877
3878 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3879 MPI2_IOCSTATUS_MASK;
3880 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3881 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3882 ioc->name, __FILE__, __LINE__, __func__);
3883 return -1;
3884 }
3885
3886 /* check if device is present */
3887 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3888 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3889 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3890 ioc->name, __FILE__, __LINE__, __func__);
3891 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3892 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3893 return -1;
3894 }
3895
3896 /* check if there were any issus with discovery */
3897 if (sas_device_pg0.AccessStatus ==
3898 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3899 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3900 ioc->name, __FILE__, __LINE__, __func__);
3901 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3902 ioc->name, sas_device_pg0.AccessStatus);
3903 return -1;
3904 }
3905
3906 /* check if this is end device */
3907 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3908 if (!(_scsih_is_end_device(device_info))) {
3909 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3910 ioc->name, __FILE__, __LINE__, __func__);
3911 return -1;
3912 }
3913
3914 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3915
3916 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3917 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3918 sas_address);
3919 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3920
3921 if (sas_device) {
3922 _scsih_ublock_io_device(ioc, handle);
3923 return 0;
3924 }
3925
3926 sas_device = kzalloc(sizeof(struct _sas_device),
3927 GFP_KERNEL);
3928 if (!sas_device) {
3929 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3930 ioc->name, __FILE__, __LINE__, __func__);
3931 return -1;
3932 }
3933
3934 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303935 if (_scsih_get_sas_address(ioc, le16_to_cpu
3936 (sas_device_pg0.ParentDevHandle),
3937 &sas_device->sas_address_parent) != 0)
3938 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3939 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06003940 sas_device->enclosure_handle =
3941 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3942 sas_device->slot =
3943 le16_to_cpu(sas_device_pg0.Slot);
3944 sas_device->device_info = device_info;
3945 sas_device->sas_address = sas_address;
3946 sas_device->hidden_raid_component = is_pd;
3947
3948 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05303949 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3950 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3951 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06003952 sas_device->enclosure_logical_id =
3953 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06003954
3955 /* get device name */
3956 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3957
3958 if (ioc->wait_for_port_enable_to_complete)
3959 _scsih_sas_device_init_add(ioc, sas_device);
3960 else
3961 _scsih_sas_device_add(ioc, sas_device);
3962
3963 return 0;
3964}
3965
3966/**
3967 * _scsih_remove_device - removing sas device object
3968 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303969 * @sas_device: the sas_device object
Eric Moore635374e2009-03-09 01:21:12 -06003970 *
3971 * Return nothing.
3972 */
3973static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303974_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
3975 *sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06003976{
3977 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06003978 Mpi2SasIoUnitControlReply_t mpi_reply;
3979 Mpi2SasIoUnitControlRequest_t mpi_request;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303980 u16 device_handle, handle;
Eric Moore635374e2009-03-09 01:21:12 -06003981
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303982 if (!sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06003983 return;
Eric Moore635374e2009-03-09 01:21:12 -06003984
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303985 handle = sas_device->handle;
3986 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
3987 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
3988 (unsigned long long) sas_device->sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06003989
3990 if (sas_device->starget && sas_device->starget->hostdata) {
3991 sas_target_priv_data = sas_device->starget->hostdata;
3992 sas_target_priv_data->deleted = 1;
3993 }
Eric Moore635374e2009-03-09 01:21:12 -06003994
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303995 if (ioc->remove_host || ioc->shost_recovery || !handle)
Eric Moore635374e2009-03-09 01:21:12 -06003996 goto out;
3997
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303998 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
3999 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304000 "target_reset handle(0x%04x)\n", ioc->name,
4001 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304002 goto skip_tr;
4003 }
4004
Eric Moore635374e2009-03-09 01:21:12 -06004005 /* Target Reset to flush out all the outstanding IO */
4006 device_handle = (sas_device->hidden_raid_component) ?
4007 sas_device->volume_handle : handle;
4008 if (device_handle) {
4009 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
4010 "handle(0x%04x)\n", ioc->name, device_handle));
4011 mutex_lock(&ioc->tm_cmds.mutex);
4012 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
4013 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
4014 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4015 mutex_unlock(&ioc->tm_cmds.mutex);
4016 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
4017 "done: handle(0x%04x)\n", ioc->name, device_handle));
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304018 if (ioc->shost_recovery)
4019 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004020 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304021 skip_tr:
4022
4023 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
4024 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
4025 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
4026 goto out;
4027 }
Eric Moore635374e2009-03-09 01:21:12 -06004028
4029 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
4030 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
4031 "(0x%04x)\n", ioc->name, handle));
4032 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
4033 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
4034 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
4035 mpi_request.DevHandle = handle;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304036 mpi_request.VF_ID = 0; /* TODO */
4037 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004038 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
4039 &mpi_request)) != 0) {
4040 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4041 ioc->name, __FILE__, __LINE__, __func__);
4042 }
4043
4044 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
4045 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
4046 le16_to_cpu(mpi_reply.IOCStatus),
4047 le32_to_cpu(mpi_reply.IOCLogInfo)));
4048
4049 out:
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304050
4051 _scsih_ublock_io_device(ioc, handle);
4052
Eric Moore635374e2009-03-09 01:21:12 -06004053 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304054 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004055
4056 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304057 "(0x%016llx)\n", ioc->name, handle,
Eric Moore635374e2009-03-09 01:21:12 -06004058 (unsigned long long) sas_device->sas_address);
4059 _scsih_sas_device_remove(ioc, sas_device);
4060
4061 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
4062 "(0x%04x)\n", ioc->name, __func__, handle));
4063}
4064
4065#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4066/**
4067 * _scsih_sas_topology_change_event_debug - debug for topology event
4068 * @ioc: per adapter object
4069 * @event_data: event data payload
4070 * Context: user.
4071 */
4072static void
4073_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4074 Mpi2EventDataSasTopologyChangeList_t *event_data)
4075{
4076 int i;
4077 u16 handle;
4078 u16 reason_code;
4079 u8 phy_number;
4080 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304081 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004082
4083 switch (event_data->ExpStatus) {
4084 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4085 status_str = "add";
4086 break;
4087 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4088 status_str = "remove";
4089 break;
4090 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304091 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004092 status_str = "responding";
4093 break;
4094 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4095 status_str = "remove delay";
4096 break;
4097 default:
4098 status_str = "unknown status";
4099 break;
4100 }
4101 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4102 ioc->name, status_str);
4103 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4104 "start_phy(%02d), count(%d)\n",
4105 le16_to_cpu(event_data->ExpanderDevHandle),
4106 le16_to_cpu(event_data->EnclosureHandle),
4107 event_data->StartPhyNum, event_data->NumEntries);
4108 for (i = 0; i < event_data->NumEntries; i++) {
4109 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4110 if (!handle)
4111 continue;
4112 phy_number = event_data->StartPhyNum + i;
4113 reason_code = event_data->PHY[i].PhyStatus &
4114 MPI2_EVENT_SAS_TOPO_RC_MASK;
4115 switch (reason_code) {
4116 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304117 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004118 break;
4119 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304120 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004121 break;
4122 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304123 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004124 break;
4125 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304126 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004127 break;
4128 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304129 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004130 break;
4131 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304132 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004133 break;
4134 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304135 link_rate = event_data->PHY[i].LinkRate >> 4;
4136 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
4137 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
4138 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4139 handle, status_str, link_rate, prev_link_rate);
4140
Eric Moore635374e2009-03-09 01:21:12 -06004141 }
4142}
4143#endif
4144
4145/**
4146 * _scsih_sas_topology_change_event - handle topology changes
4147 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304148 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004149 * Context: user.
4150 *
4151 */
4152static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304153_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004154 struct fw_event_work *fw_event)
4155{
4156 int i;
4157 u16 parent_handle, handle;
4158 u16 reason_code;
4159 u8 phy_number;
4160 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304161 struct _sas_device *sas_device;
4162 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004163 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304164 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304165 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004166
4167#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4168 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4169 _scsih_sas_topology_change_event_debug(ioc, event_data);
4170#endif
4171
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304172 if (ioc->shost_recovery)
4173 return;
4174
Eric Moore635374e2009-03-09 01:21:12 -06004175 if (!ioc->sas_hba.num_phys)
4176 _scsih_sas_host_add(ioc);
4177 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304178 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004179
4180 if (fw_event->ignore) {
4181 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4182 "event\n", ioc->name));
4183 return;
4184 }
4185
4186 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4187
4188 /* handle expander add */
4189 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4190 if (_scsih_expander_add(ioc, parent_handle) != 0)
4191 return;
4192
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304193 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4194 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4195 parent_handle);
4196 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4197 if (sas_expander)
4198 sas_address = sas_expander->sas_address;
4199 else if (parent_handle < ioc->sas_hba.num_phys)
4200 sas_address = ioc->sas_hba.sas_address;
4201 else
4202 return;
4203
Eric Moore635374e2009-03-09 01:21:12 -06004204 /* handle siblings events */
4205 for (i = 0; i < event_data->NumEntries; i++) {
4206 if (fw_event->ignore) {
4207 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4208 "expander event\n", ioc->name));
4209 return;
4210 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304211 if (ioc->shost_recovery)
4212 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304213 phy_number = event_data->StartPhyNum + i;
4214 reason_code = event_data->PHY[i].PhyStatus &
4215 MPI2_EVENT_SAS_TOPO_RC_MASK;
4216 if ((event_data->PHY[i].PhyStatus &
4217 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4218 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004219 continue;
4220 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4221 if (!handle)
4222 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304223 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304224 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004225 switch (reason_code) {
4226 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304227
4228 if (link_rate == prev_link_rate)
4229 break;
4230
4231 mpt2sas_transport_update_links(ioc, sas_address,
4232 handle, phy_number, link_rate);
4233
4234 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
4235 _scsih_ublock_io_device(ioc, handle);
4236 break;
Eric Moore635374e2009-03-09 01:21:12 -06004237 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304238
4239 mpt2sas_transport_update_links(ioc, sas_address,
4240 handle, phy_number, link_rate);
4241
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304242 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004243 break;
4244 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304245
4246 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4247 sas_device = _scsih_sas_device_find_by_handle(ioc,
4248 handle);
4249 if (!sas_device) {
4250 spin_unlock_irqrestore(&ioc->sas_device_lock,
4251 flags);
4252 break;
4253 }
4254 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4255 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004256 break;
4257 }
4258 }
4259
4260 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304261 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4262 sas_expander)
4263 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004264
4265}
4266
4267#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4268/**
4269 * _scsih_sas_device_status_change_event_debug - debug for device event
4270 * @event_data: event data payload
4271 * Context: user.
4272 *
4273 * Return nothing.
4274 */
4275static void
4276_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4277 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4278{
4279 char *reason_str = NULL;
4280
4281 switch (event_data->ReasonCode) {
4282 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4283 reason_str = "smart data";
4284 break;
4285 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4286 reason_str = "unsupported device discovered";
4287 break;
4288 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4289 reason_str = "internal device reset";
4290 break;
4291 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4292 reason_str = "internal task abort";
4293 break;
4294 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4295 reason_str = "internal task abort set";
4296 break;
4297 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4298 reason_str = "internal clear task set";
4299 break;
4300 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4301 reason_str = "internal query task";
4302 break;
4303 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4304 reason_str = "sata init failure";
4305 break;
4306 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4307 reason_str = "internal device reset complete";
4308 break;
4309 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4310 reason_str = "internal task abort complete";
4311 break;
4312 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4313 reason_str = "internal async notification";
4314 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304315 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4316 reason_str = "expander reduced functionality";
4317 break;
4318 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4319 reason_str = "expander reduced functionality complete";
4320 break;
Eric Moore635374e2009-03-09 01:21:12 -06004321 default:
4322 reason_str = "unknown reason";
4323 break;
4324 }
4325 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4326 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4327 reason_str, le16_to_cpu(event_data->DevHandle),
4328 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4329 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4330 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4331 event_data->ASC, event_data->ASCQ);
4332 printk(KERN_INFO "\n");
4333}
4334#endif
4335
4336/**
4337 * _scsih_sas_device_status_change_event - handle device status change
4338 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304339 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004340 * Context: user.
4341 *
4342 * Return nothing.
4343 */
4344static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304345_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4346 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004347{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304348 struct MPT2SAS_TARGET *target_priv_data;
4349 struct _sas_device *sas_device;
4350 __le64 sas_address;
4351 unsigned long flags;
4352 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4353 fw_event->event_data;
4354
Eric Moore635374e2009-03-09 01:21:12 -06004355#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4356 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304357 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304358 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004359#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304360
4361 if (!(event_data->ReasonCode ==
4362 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
4363 event_data->ReasonCode ==
4364 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET))
4365 return;
4366
4367 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4368 sas_address = le64_to_cpu(event_data->SASAddress);
4369 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4370 sas_address);
4371 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4372
4373 if (!sas_device || !sas_device->starget)
4374 return;
4375
4376 target_priv_data = sas_device->starget->hostdata;
4377 if (!target_priv_data)
4378 return;
4379
4380 if (event_data->ReasonCode ==
4381 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4382 target_priv_data->tm_busy = 1;
4383 else
4384 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004385}
4386
4387#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4388/**
4389 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4390 * @ioc: per adapter object
4391 * @event_data: event data payload
4392 * Context: user.
4393 *
4394 * Return nothing.
4395 */
4396static void
4397_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4398 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4399{
4400 char *reason_str = NULL;
4401
4402 switch (event_data->ReasonCode) {
4403 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4404 reason_str = "enclosure add";
4405 break;
4406 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4407 reason_str = "enclosure remove";
4408 break;
4409 default:
4410 reason_str = "unknown reason";
4411 break;
4412 }
4413
4414 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4415 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4416 " number slots(%d)\n", ioc->name, reason_str,
4417 le16_to_cpu(event_data->EnclosureHandle),
4418 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4419 le16_to_cpu(event_data->StartSlot));
4420}
4421#endif
4422
4423/**
4424 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4425 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304426 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004427 * Context: user.
4428 *
4429 * Return nothing.
4430 */
4431static void
4432_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304433 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004434{
4435#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4436 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4437 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304438 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004439#endif
4440}
4441
4442/**
4443 * _scsih_sas_broadcast_primative_event - handle broadcast events
4444 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304445 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004446 * Context: user.
4447 *
4448 * Return nothing.
4449 */
4450static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304451_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4452 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004453{
4454 struct scsi_cmnd *scmd;
4455 u16 smid, handle;
4456 u32 lun;
4457 struct MPT2SAS_DEVICE *sas_device_priv_data;
4458 u32 termination_count;
4459 u32 query_count;
4460 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304461#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4462 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4463#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05304464 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004465 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4466 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4467 event_data->PortWidth));
Eric Moore635374e2009-03-09 01:21:12 -06004468 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4469 __func__));
4470
4471 mutex_lock(&ioc->tm_cmds.mutex);
4472 termination_count = 0;
4473 query_count = 0;
4474 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304475 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004476 scmd = _scsih_scsi_lookup_get(ioc, smid);
4477 if (!scmd)
4478 continue;
4479 sas_device_priv_data = scmd->device->hostdata;
4480 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4481 continue;
4482 /* skip hidden raid components */
4483 if (sas_device_priv_data->sas_target->flags &
4484 MPT_TARGET_FLAGS_RAID_COMPONENT)
4485 continue;
4486 /* skip volumes */
4487 if (sas_device_priv_data->sas_target->flags &
4488 MPT_TARGET_FLAGS_VOLUME)
4489 continue;
4490
4491 handle = sas_device_priv_data->sas_target->handle;
4492 lun = sas_device_priv_data->lun;
4493 query_count++;
4494
4495 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4496 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004497 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05304498 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
4499 & MPI2_IOCSTATUS_MASK;
4500 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06004501 (mpi_reply->ResponseCode ==
4502 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4503 mpi_reply->ResponseCode ==
4504 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4505 continue;
4506
4507 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004508 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4509 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004510 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4511 }
Eric Moore635374e2009-03-09 01:21:12 -06004512 ioc->broadcast_aen_busy = 0;
4513 mutex_unlock(&ioc->tm_cmds.mutex);
4514
4515 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4516 "%s - exit, query_count = %d termination_count = %d\n",
4517 ioc->name, __func__, query_count, termination_count));
4518}
4519
4520/**
4521 * _scsih_sas_discovery_event - handle discovery events
4522 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304523 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004524 * Context: user.
4525 *
4526 * Return nothing.
4527 */
4528static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304529_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4530 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004531{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304532 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4533
Eric Moore635374e2009-03-09 01:21:12 -06004534#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4535 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4536 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4537 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4538 "start" : "stop");
4539 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304540 printk("discovery_status(0x%08x)",
4541 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06004542 printk("\n");
4543 }
4544#endif
4545
4546 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4547 !ioc->sas_hba.num_phys)
4548 _scsih_sas_host_add(ioc);
4549}
4550
4551/**
4552 * _scsih_reprobe_lun - reprobing lun
4553 * @sdev: scsi device struct
4554 * @no_uld_attach: sdev->no_uld_attach flag setting
4555 *
4556 **/
4557static void
4558_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4559{
4560 int rc;
4561
4562 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4563 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4564 sdev->no_uld_attach ? "hidding" : "exposing");
4565 rc = scsi_device_reprobe(sdev);
4566}
4567
4568/**
4569 * _scsih_reprobe_target - reprobing target
4570 * @starget: scsi target struct
4571 * @no_uld_attach: sdev->no_uld_attach flag setting
4572 *
4573 * Note: no_uld_attach flag determines whether the disk device is attached
4574 * to block layer. A value of `1` means to not attach.
4575 **/
4576static void
4577_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4578{
4579 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4580
4581 if (no_uld_attach)
4582 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4583 else
4584 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4585
4586 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4587 _scsih_reprobe_lun);
4588}
4589/**
4590 * _scsih_sas_volume_add - add new volume
4591 * @ioc: per adapter object
4592 * @element: IR config element data
4593 * Context: user.
4594 *
4595 * Return nothing.
4596 */
4597static void
4598_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4599 Mpi2EventIrConfigElement_t *element)
4600{
4601 struct _raid_device *raid_device;
4602 unsigned long flags;
4603 u64 wwid;
4604 u16 handle = le16_to_cpu(element->VolDevHandle);
4605 int rc;
4606
Eric Moore635374e2009-03-09 01:21:12 -06004607 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4608 if (!wwid) {
4609 printk(MPT2SAS_ERR_FMT
4610 "failure at %s:%d/%s()!\n", ioc->name,
4611 __FILE__, __LINE__, __func__);
4612 return;
4613 }
4614
4615 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4616 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4617 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4618
4619 if (raid_device)
4620 return;
4621
4622 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4623 if (!raid_device) {
4624 printk(MPT2SAS_ERR_FMT
4625 "failure at %s:%d/%s()!\n", ioc->name,
4626 __FILE__, __LINE__, __func__);
4627 return;
4628 }
4629
4630 raid_device->id = ioc->sas_id++;
4631 raid_device->channel = RAID_CHANNEL;
4632 raid_device->handle = handle;
4633 raid_device->wwid = wwid;
4634 _scsih_raid_device_add(ioc, raid_device);
4635 if (!ioc->wait_for_port_enable_to_complete) {
4636 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4637 raid_device->id, 0);
4638 if (rc)
4639 _scsih_raid_device_remove(ioc, raid_device);
4640 } else
4641 _scsih_determine_boot_device(ioc, raid_device, 1);
4642}
4643
4644/**
4645 * _scsih_sas_volume_delete - delete volume
4646 * @ioc: per adapter object
4647 * @element: IR config element data
4648 * Context: user.
4649 *
4650 * Return nothing.
4651 */
4652static void
4653_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4654 Mpi2EventIrConfigElement_t *element)
4655{
4656 struct _raid_device *raid_device;
4657 u16 handle = le16_to_cpu(element->VolDevHandle);
4658 unsigned long flags;
4659 struct MPT2SAS_TARGET *sas_target_priv_data;
4660
Eric Moore635374e2009-03-09 01:21:12 -06004661 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4662 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4663 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4664 if (!raid_device)
4665 return;
4666 if (raid_device->starget) {
4667 sas_target_priv_data = raid_device->starget->hostdata;
4668 sas_target_priv_data->deleted = 1;
4669 scsi_remove_target(&raid_device->starget->dev);
4670 }
4671 _scsih_raid_device_remove(ioc, raid_device);
4672}
4673
4674/**
4675 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4676 * @ioc: per adapter object
4677 * @element: IR config element data
4678 * Context: user.
4679 *
4680 * Return nothing.
4681 */
4682static void
4683_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4684 Mpi2EventIrConfigElement_t *element)
4685{
4686 struct _sas_device *sas_device;
4687 unsigned long flags;
4688 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4689
4690 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4691 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4692 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4693 if (!sas_device)
4694 return;
4695
4696 /* exposing raid component */
4697 sas_device->volume_handle = 0;
4698 sas_device->volume_wwid = 0;
4699 sas_device->hidden_raid_component = 0;
4700 _scsih_reprobe_target(sas_device->starget, 0);
4701}
4702
4703/**
4704 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4705 * @ioc: per adapter object
4706 * @element: IR config element data
4707 * Context: user.
4708 *
4709 * Return nothing.
4710 */
4711static void
4712_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4713 Mpi2EventIrConfigElement_t *element)
4714{
4715 struct _sas_device *sas_device;
4716 unsigned long flags;
4717 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4718
4719 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4720 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4721 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4722 if (!sas_device)
4723 return;
4724
4725 /* hiding raid component */
4726 mpt2sas_config_get_volume_handle(ioc, handle,
4727 &sas_device->volume_handle);
4728 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4729 &sas_device->volume_wwid);
4730 sas_device->hidden_raid_component = 1;
4731 _scsih_reprobe_target(sas_device->starget, 1);
4732}
4733
4734/**
4735 * _scsih_sas_pd_delete - delete pd component
4736 * @ioc: per adapter object
4737 * @element: IR config element data
4738 * Context: user.
4739 *
4740 * Return nothing.
4741 */
4742static void
4743_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4744 Mpi2EventIrConfigElement_t *element)
4745{
4746 struct _sas_device *sas_device;
4747 unsigned long flags;
4748 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4749
4750 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4751 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4752 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4753 if (!sas_device)
4754 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304755 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004756}
4757
4758/**
4759 * _scsih_sas_pd_add - remove pd component
4760 * @ioc: per adapter object
4761 * @element: IR config element data
4762 * Context: user.
4763 *
4764 * Return nothing.
4765 */
4766static void
4767_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4768 Mpi2EventIrConfigElement_t *element)
4769{
4770 struct _sas_device *sas_device;
4771 unsigned long flags;
4772 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304773 Mpi2ConfigReply_t mpi_reply;
4774 Mpi2SasDevicePage0_t sas_device_pg0;
4775 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304776 u64 sas_address;
4777 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06004778
4779 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4780 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4781 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304782 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004783 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304784 return;
4785 }
4786
4787 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4788 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4789 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4790 ioc->name, __FILE__, __LINE__, __func__);
4791 return;
4792 }
4793
4794 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4795 MPI2_IOCSTATUS_MASK;
4796 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4797 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4798 ioc->name, __FILE__, __LINE__, __func__);
4799 return;
4800 }
4801
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304802 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4803 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4804 mpt2sas_transport_update_links(ioc, sas_address, handle,
4805 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304806
4807 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004808}
4809
4810#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4811/**
4812 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4813 * @ioc: per adapter object
4814 * @event_data: event data payload
4815 * Context: user.
4816 *
4817 * Return nothing.
4818 */
4819static void
4820_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4821 Mpi2EventDataIrConfigChangeList_t *event_data)
4822{
4823 Mpi2EventIrConfigElement_t *element;
4824 u8 element_type;
4825 int i;
4826 char *reason_str = NULL, *element_str = NULL;
4827
4828 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4829
4830 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4831 ioc->name, (le32_to_cpu(event_data->Flags) &
4832 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4833 "foreign" : "native", event_data->NumElements);
4834 for (i = 0; i < event_data->NumElements; i++, element++) {
4835 switch (element->ReasonCode) {
4836 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4837 reason_str = "add";
4838 break;
4839 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4840 reason_str = "remove";
4841 break;
4842 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4843 reason_str = "no change";
4844 break;
4845 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4846 reason_str = "hide";
4847 break;
4848 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4849 reason_str = "unhide";
4850 break;
4851 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4852 reason_str = "volume_created";
4853 break;
4854 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4855 reason_str = "volume_deleted";
4856 break;
4857 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4858 reason_str = "pd_created";
4859 break;
4860 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4861 reason_str = "pd_deleted";
4862 break;
4863 default:
4864 reason_str = "unknown reason";
4865 break;
4866 }
4867 element_type = le16_to_cpu(element->ElementFlags) &
4868 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4869 switch (element_type) {
4870 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4871 element_str = "volume";
4872 break;
4873 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4874 element_str = "phys disk";
4875 break;
4876 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4877 element_str = "hot spare";
4878 break;
4879 default:
4880 element_str = "unknown element";
4881 break;
4882 }
4883 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4884 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4885 reason_str, le16_to_cpu(element->VolDevHandle),
4886 le16_to_cpu(element->PhysDiskDevHandle),
4887 element->PhysDiskNum);
4888 }
4889}
4890#endif
4891
4892/**
4893 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4894 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304895 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004896 * Context: user.
4897 *
4898 * Return nothing.
4899 */
4900static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304901_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4902 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004903{
4904 Mpi2EventIrConfigElement_t *element;
4905 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304906 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304907 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004908
4909#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4910 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4911 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4912
4913#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05304914 foreign_config = (le32_to_cpu(event_data->Flags) &
4915 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06004916
4917 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4918 for (i = 0; i < event_data->NumElements; i++, element++) {
4919
4920 switch (element->ReasonCode) {
4921 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4922 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304923 if (!foreign_config)
4924 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004925 break;
4926 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4927 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304928 if (!foreign_config)
4929 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004930 break;
4931 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4932 _scsih_sas_pd_hide(ioc, element);
4933 break;
4934 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4935 _scsih_sas_pd_expose(ioc, element);
4936 break;
4937 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4938 _scsih_sas_pd_add(ioc, element);
4939 break;
4940 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4941 _scsih_sas_pd_delete(ioc, element);
4942 break;
4943 }
4944 }
4945}
4946
4947/**
4948 * _scsih_sas_ir_volume_event - IR volume event
4949 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304950 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004951 * Context: user.
4952 *
4953 * Return nothing.
4954 */
4955static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304956_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
4957 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004958{
4959 u64 wwid;
4960 unsigned long flags;
4961 struct _raid_device *raid_device;
4962 u16 handle;
4963 u32 state;
4964 int rc;
4965 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304966 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004967
4968 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4969 return;
4970
4971 handle = le16_to_cpu(event_data->VolDevHandle);
4972 state = le32_to_cpu(event_data->NewValue);
4973 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4974 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4975 le32_to_cpu(event_data->PreviousValue), state));
4976
4977 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4978 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4979 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4980
4981 switch (state) {
4982 case MPI2_RAID_VOL_STATE_MISSING:
4983 case MPI2_RAID_VOL_STATE_FAILED:
4984 if (!raid_device)
4985 break;
4986 if (raid_device->starget) {
4987 sas_target_priv_data = raid_device->starget->hostdata;
4988 sas_target_priv_data->deleted = 1;
4989 scsi_remove_target(&raid_device->starget->dev);
4990 }
4991 _scsih_raid_device_remove(ioc, raid_device);
4992 break;
4993
4994 case MPI2_RAID_VOL_STATE_ONLINE:
4995 case MPI2_RAID_VOL_STATE_DEGRADED:
4996 case MPI2_RAID_VOL_STATE_OPTIMAL:
4997 if (raid_device)
4998 break;
4999
5000 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5001 if (!wwid) {
5002 printk(MPT2SAS_ERR_FMT
5003 "failure at %s:%d/%s()!\n", ioc->name,
5004 __FILE__, __LINE__, __func__);
5005 break;
5006 }
5007
5008 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5009 if (!raid_device) {
5010 printk(MPT2SAS_ERR_FMT
5011 "failure at %s:%d/%s()!\n", ioc->name,
5012 __FILE__, __LINE__, __func__);
5013 break;
5014 }
5015
5016 raid_device->id = ioc->sas_id++;
5017 raid_device->channel = RAID_CHANNEL;
5018 raid_device->handle = handle;
5019 raid_device->wwid = wwid;
5020 _scsih_raid_device_add(ioc, raid_device);
5021 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5022 raid_device->id, 0);
5023 if (rc)
5024 _scsih_raid_device_remove(ioc, raid_device);
5025 break;
5026
5027 case MPI2_RAID_VOL_STATE_INITIALIZING:
5028 default:
5029 break;
5030 }
5031}
5032
5033/**
5034 * _scsih_sas_ir_physical_disk_event - PD event
5035 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305036 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005037 * Context: user.
5038 *
5039 * Return nothing.
5040 */
5041static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305042_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5043 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005044{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305045 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005046 u32 state;
5047 struct _sas_device *sas_device;
5048 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305049 Mpi2ConfigReply_t mpi_reply;
5050 Mpi2SasDevicePage0_t sas_device_pg0;
5051 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305052 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305053 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005054
5055 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5056 return;
5057
5058 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5059 state = le32_to_cpu(event_data->NewValue);
5060
5061 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
5062 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5063 le32_to_cpu(event_data->PreviousValue), state));
5064
5065 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5066 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5067 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5068
5069 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005070 case MPI2_RAID_PD_STATE_ONLINE:
5071 case MPI2_RAID_PD_STATE_DEGRADED:
5072 case MPI2_RAID_PD_STATE_REBUILDING:
5073 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305074 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06005075 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305076 return;
5077 }
5078
5079 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5080 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5081 handle))) {
5082 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5083 ioc->name, __FILE__, __LINE__, __func__);
5084 return;
5085 }
5086
5087 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5088 MPI2_IOCSTATUS_MASK;
5089 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5090 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5091 ioc->name, __FILE__, __LINE__, __func__);
5092 return;
5093 }
5094
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305095 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5096 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5097 mpt2sas_transport_update_links(ioc, sas_address, handle,
5098 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305099
5100 _scsih_add_device(ioc, handle, 0, 1);
5101
Eric Moore635374e2009-03-09 01:21:12 -06005102 break;
5103
Kashyap, Desai62727a72009-08-07 19:35:18 +05305104 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005105 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5106 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5107 case MPI2_RAID_PD_STATE_HOT_SPARE:
5108 default:
5109 break;
5110 }
5111}
5112
5113#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5114/**
5115 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5116 * @ioc: per adapter object
5117 * @event_data: event data payload
5118 * Context: user.
5119 *
5120 * Return nothing.
5121 */
5122static void
5123_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5124 Mpi2EventDataIrOperationStatus_t *event_data)
5125{
5126 char *reason_str = NULL;
5127
5128 switch (event_data->RAIDOperation) {
5129 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5130 reason_str = "resync";
5131 break;
5132 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5133 reason_str = "online capacity expansion";
5134 break;
5135 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5136 reason_str = "consistency check";
5137 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305138 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5139 reason_str = "background init";
5140 break;
5141 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5142 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005143 break;
5144 }
5145
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305146 if (!reason_str)
5147 return;
5148
Eric Moore635374e2009-03-09 01:21:12 -06005149 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5150 "\thandle(0x%04x), percent complete(%d)\n",
5151 ioc->name, reason_str,
5152 le16_to_cpu(event_data->VolDevHandle),
5153 event_data->PercentComplete);
5154}
5155#endif
5156
5157/**
5158 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5159 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305160 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005161 * Context: user.
5162 *
5163 * Return nothing.
5164 */
5165static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305166_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5167 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005168{
5169#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5170 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305171 _scsih_sas_ir_operation_status_event_debug(ioc,
5172 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005173#endif
5174}
5175
5176/**
5177 * _scsih_task_set_full - handle task set full
5178 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305179 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005180 * Context: user.
5181 *
5182 * Throttle back qdepth.
5183 */
5184static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305185_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5186 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005187{
5188 unsigned long flags;
5189 struct _sas_device *sas_device;
5190 static struct _raid_device *raid_device;
5191 struct scsi_device *sdev;
5192 int depth;
5193 u16 current_depth;
5194 u16 handle;
5195 int id, channel;
5196 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305197 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005198
5199 current_depth = le16_to_cpu(event_data->CurrentDepth);
5200 handle = le16_to_cpu(event_data->DevHandle);
5201 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5202 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5203 if (!sas_device) {
5204 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5205 return;
5206 }
5207 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5208 id = sas_device->id;
5209 channel = sas_device->channel;
5210 sas_address = sas_device->sas_address;
5211
5212 /* if hidden raid component, then change to volume characteristics */
5213 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5214 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5215 raid_device = _scsih_raid_device_find_by_handle(
5216 ioc, sas_device->volume_handle);
5217 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5218 if (raid_device) {
5219 id = raid_device->id;
5220 channel = raid_device->channel;
5221 handle = raid_device->handle;
5222 sas_address = raid_device->wwid;
5223 }
5224 }
5225
5226 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5227 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5228 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5229 handle, (unsigned long long)sas_address, current_depth);
5230
5231 shost_for_each_device(sdev, ioc->shost) {
5232 if (sdev->id == id && sdev->channel == channel) {
5233 if (current_depth > sdev->queue_depth) {
5234 if (ioc->logging_level &
5235 MPT_DEBUG_TASK_SET_FULL)
5236 sdev_printk(KERN_INFO, sdev, "strange "
5237 "observation, the queue depth is"
5238 " (%d) meanwhile fw queue depth "
5239 "is (%d)\n", sdev->queue_depth,
5240 current_depth);
5241 continue;
5242 }
5243 depth = scsi_track_queue_full(sdev,
5244 current_depth - 1);
5245 if (depth > 0)
5246 sdev_printk(KERN_INFO, sdev, "Queue depth "
5247 "reduced to (%d)\n", depth);
5248 else if (depth < 0)
5249 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5250 "Queueing is being disabled\n");
5251 else if (depth == 0)
5252 if (ioc->logging_level &
5253 MPT_DEBUG_TASK_SET_FULL)
5254 sdev_printk(KERN_INFO, sdev,
5255 "Queue depth not changed yet\n");
5256 }
5257 }
5258}
5259
5260/**
5261 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5262 * @ioc: per adapter object
5263 * @sas_address: sas address
5264 * @slot: enclosure slot id
5265 * @handle: device handle
5266 *
5267 * After host reset, find out whether devices are still responding.
5268 * Used in _scsi_remove_unresponsive_sas_devices.
5269 *
5270 * Return nothing.
5271 */
5272static void
5273_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5274 u16 slot, u16 handle)
5275{
5276 struct MPT2SAS_TARGET *sas_target_priv_data;
5277 struct scsi_target *starget;
5278 struct _sas_device *sas_device;
5279 unsigned long flags;
5280
5281 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5282 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5283 if (sas_device->sas_address == sas_address &&
5284 sas_device->slot == slot && sas_device->starget) {
5285 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305286 sas_device->state = 0;
5287 starget = sas_device->starget;
5288 sas_target_priv_data = starget->hostdata;
5289 sas_target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005290 starget_printk(KERN_INFO, sas_device->starget,
5291 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5292 "logical id(0x%016llx), slot(%d)\n", handle,
5293 (unsigned long long)sas_device->sas_address,
5294 (unsigned long long)
5295 sas_device->enclosure_logical_id,
5296 sas_device->slot);
5297 if (sas_device->handle == handle)
5298 goto out;
5299 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5300 sas_device->handle);
5301 sas_device->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005302 sas_target_priv_data->handle = handle;
5303 goto out;
5304 }
5305 }
5306 out:
5307 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5308}
5309
5310/**
5311 * _scsih_search_responding_sas_devices -
5312 * @ioc: per adapter object
5313 *
5314 * After host reset, find out whether devices are still responding.
5315 * If not remove.
5316 *
5317 * Return nothing.
5318 */
5319static void
5320_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5321{
5322 Mpi2SasDevicePage0_t sas_device_pg0;
5323 Mpi2ConfigReply_t mpi_reply;
5324 u16 ioc_status;
5325 __le64 sas_address;
5326 u16 handle;
5327 u32 device_info;
5328 u16 slot;
5329
5330 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5331
5332 if (list_empty(&ioc->sas_device_list))
5333 return;
5334
5335 handle = 0xFFFF;
5336 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5337 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5338 handle))) {
5339 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5340 MPI2_IOCSTATUS_MASK;
5341 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5342 break;
5343 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5344 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5345 if (!(_scsih_is_end_device(device_info)))
5346 continue;
5347 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5348 slot = le16_to_cpu(sas_device_pg0.Slot);
5349 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5350 handle);
5351 }
5352}
5353
5354/**
5355 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5356 * @ioc: per adapter object
5357 * @wwid: world wide identifier for raid volume
5358 * @handle: device handle
5359 *
5360 * After host reset, find out whether devices are still responding.
5361 * Used in _scsi_remove_unresponsive_raid_devices.
5362 *
5363 * Return nothing.
5364 */
5365static void
5366_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5367 u16 handle)
5368{
5369 struct MPT2SAS_TARGET *sas_target_priv_data;
5370 struct scsi_target *starget;
5371 struct _raid_device *raid_device;
5372 unsigned long flags;
5373
5374 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5375 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5376 if (raid_device->wwid == wwid && raid_device->starget) {
5377 raid_device->responding = 1;
5378 starget_printk(KERN_INFO, raid_device->starget,
5379 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5380 (unsigned long long)raid_device->wwid);
5381 if (raid_device->handle == handle)
5382 goto out;
5383 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5384 raid_device->handle);
5385 raid_device->handle = handle;
5386 starget = raid_device->starget;
5387 sas_target_priv_data = starget->hostdata;
5388 sas_target_priv_data->handle = handle;
5389 goto out;
5390 }
5391 }
5392 out:
5393 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5394}
5395
5396/**
5397 * _scsih_search_responding_raid_devices -
5398 * @ioc: per adapter object
5399 *
5400 * After host reset, find out whether devices are still responding.
5401 * If not remove.
5402 *
5403 * Return nothing.
5404 */
5405static void
5406_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5407{
5408 Mpi2RaidVolPage1_t volume_pg1;
5409 Mpi2ConfigReply_t mpi_reply;
5410 u16 ioc_status;
5411 u16 handle;
5412
5413 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5414
5415 if (list_empty(&ioc->raid_device_list))
5416 return;
5417
5418 handle = 0xFFFF;
5419 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5420 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5421 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5422 MPI2_IOCSTATUS_MASK;
5423 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5424 break;
5425 handle = le16_to_cpu(volume_pg1.DevHandle);
5426 _scsih_mark_responding_raid_device(ioc,
5427 le64_to_cpu(volume_pg1.WWID), handle);
5428 }
5429}
5430
5431/**
5432 * _scsih_mark_responding_expander - mark a expander as responding
5433 * @ioc: per adapter object
5434 * @sas_address: sas address
5435 * @handle:
5436 *
5437 * After host reset, find out whether devices are still responding.
5438 * Used in _scsi_remove_unresponsive_expanders.
5439 *
5440 * Return nothing.
5441 */
5442static void
5443_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5444 u16 handle)
5445{
5446 struct _sas_node *sas_expander;
5447 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305448 int i;
Eric Moore635374e2009-03-09 01:21:12 -06005449
5450 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5451 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305452 if (sas_expander->sas_address != sas_address)
5453 continue;
5454 sas_expander->responding = 1;
5455 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06005456 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305457 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5458 " from(0x%04x) to (0x%04x)!!!\n",
5459 (unsigned long long)sas_expander->sas_address,
5460 sas_expander->handle, handle);
5461 sas_expander->handle = handle;
5462 for (i = 0 ; i < sas_expander->num_phys ; i++)
5463 sas_expander->phy[i].handle = handle;
5464 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005465 }
5466 out:
5467 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5468}
5469
5470/**
5471 * _scsih_search_responding_expanders -
5472 * @ioc: per adapter object
5473 *
5474 * After host reset, find out whether devices are still responding.
5475 * If not remove.
5476 *
5477 * Return nothing.
5478 */
5479static void
5480_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5481{
5482 Mpi2ExpanderPage0_t expander_pg0;
5483 Mpi2ConfigReply_t mpi_reply;
5484 u16 ioc_status;
5485 __le64 sas_address;
5486 u16 handle;
5487
5488 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5489
5490 if (list_empty(&ioc->sas_expander_list))
5491 return;
5492
5493 handle = 0xFFFF;
5494 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5495 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5496
5497 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5498 MPI2_IOCSTATUS_MASK;
5499 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5500 break;
5501
5502 handle = le16_to_cpu(expander_pg0.DevHandle);
5503 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5504 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5505 "sas_addr(0x%016llx)\n", handle,
5506 (unsigned long long)sas_address);
5507 _scsih_mark_responding_expander(ioc, sas_address, handle);
5508 }
5509
5510}
5511
5512/**
5513 * _scsih_remove_unresponding_devices - removing unresponding devices
5514 * @ioc: per adapter object
5515 *
5516 * Return nothing.
5517 */
5518static void
5519_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5520{
5521 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305522 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005523 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005524
Eric Moore635374e2009-03-09 01:21:12 -06005525
5526 list_for_each_entry_safe(sas_device, sas_device_next,
5527 &ioc->sas_device_list, list) {
5528 if (sas_device->responding) {
5529 sas_device->responding = 0;
5530 continue;
5531 }
5532 if (sas_device->starget)
5533 starget_printk(KERN_INFO, sas_device->starget,
5534 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5535 "enclosure logical id(0x%016llx), slot(%d)\n",
5536 sas_device->handle,
5537 (unsigned long long)sas_device->sas_address,
5538 (unsigned long long)
5539 sas_device->enclosure_logical_id,
5540 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305541 /* invalidate the device handle */
5542 sas_device->handle = 0;
5543 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005544 }
5545
5546 list_for_each_entry_safe(raid_device, raid_device_next,
5547 &ioc->raid_device_list, list) {
5548 if (raid_device->responding) {
5549 raid_device->responding = 0;
5550 continue;
5551 }
5552 if (raid_device->starget) {
5553 starget_printk(KERN_INFO, raid_device->starget,
5554 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5555 raid_device->handle,
5556 (unsigned long long)raid_device->wwid);
5557 scsi_remove_target(&raid_device->starget->dev);
5558 }
5559 _scsih_raid_device_remove(ioc, raid_device);
5560 }
5561
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305562 retry_expander_search:
5563 sas_expander = NULL;
5564 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005565 if (sas_expander->responding) {
5566 sas_expander->responding = 0;
5567 continue;
5568 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305569 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305570 goto retry_expander_search;
5571 }
5572}
5573
5574/**
5575 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5576 * @ioc: per adapter object
5577 * @reset_phase: phase
5578 *
5579 * The handler for doing any required cleanup or initialization.
5580 *
5581 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5582 * MPT2_IOC_DONE_RESET
5583 *
5584 * Return nothing.
5585 */
5586void
5587mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5588{
5589 switch (reset_phase) {
5590 case MPT2_IOC_PRE_RESET:
5591 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5592 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5593 _scsih_fw_event_off(ioc);
5594 break;
5595 case MPT2_IOC_AFTER_RESET:
5596 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5597 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5598 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5599 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5600 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5601 complete(&ioc->tm_cmds.done);
5602 }
5603 _scsih_fw_event_on(ioc);
5604 _scsih_flush_running_cmds(ioc);
5605 break;
5606 case MPT2_IOC_DONE_RESET:
5607 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5608 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305609 _scsih_sas_host_refresh(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305610 _scsih_search_responding_sas_devices(ioc);
5611 _scsih_search_responding_raid_devices(ioc);
5612 _scsih_search_responding_expanders(ioc);
5613 break;
5614 case MPT2_IOC_RUNNING:
5615 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5616 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5617 _scsih_remove_unresponding_devices(ioc);
5618 break;
Eric Moore635374e2009-03-09 01:21:12 -06005619 }
5620}
5621
5622/**
5623 * _firmware_event_work - delayed task for processing firmware events
5624 * @ioc: per adapter object
5625 * @work: equal to the fw_event_work object
5626 * Context: user.
5627 *
5628 * Return nothing.
5629 */
5630static void
5631_firmware_event_work(struct work_struct *work)
5632{
5633 struct fw_event_work *fw_event = container_of(work,
Eric Moore6f92a7a2009-04-21 15:43:33 -06005634 struct fw_event_work, work);
Eric Moore635374e2009-03-09 01:21:12 -06005635 unsigned long flags;
5636 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5637
Eric Moore635374e2009-03-09 01:21:12 -06005638 /* the queue is being flushed so ignore this event */
5639 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5640 if (ioc->fw_events_off || ioc->remove_host) {
5641 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5642 _scsih_fw_event_free(ioc, fw_event);
5643 return;
5644 }
5645 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5646
Eric Moore635374e2009-03-09 01:21:12 -06005647 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06005648 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5649 return;
5650 }
Eric Moore635374e2009-03-09 01:21:12 -06005651
5652 switch (fw_event->event) {
5653 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305654 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005655 break;
5656 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305657 _scsih_sas_device_status_change_event(ioc,
5658 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005659 break;
5660 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305661 _scsih_sas_discovery_event(ioc,
5662 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005663 break;
5664 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305665 _scsih_sas_broadcast_primative_event(ioc,
5666 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005667 break;
5668 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5669 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305670 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005671 break;
5672 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305673 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005674 break;
5675 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305676 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005677 break;
5678 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305679 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005680 break;
5681 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305682 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005683 break;
5684 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305685 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06005686 break;
5687 }
5688 _scsih_fw_event_free(ioc, fw_event);
5689}
5690
5691/**
5692 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5693 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305694 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06005695 * @reply: reply message frame(lower 32bit addr)
5696 * Context: interrupt.
5697 *
5698 * This function merely adds a new work task into ioc->firmware_event_thread.
5699 * The tasks are worked from _firmware_event_work in user context.
5700 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305701 * Return 1 meaning mf should be freed from _base_interrupt
5702 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06005703 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305704u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305705mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5706 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06005707{
5708 struct fw_event_work *fw_event;
5709 Mpi2EventNotificationReply_t *mpi_reply;
5710 unsigned long flags;
5711 u16 event;
5712
5713 /* events turned off due to host reset or driver unloading */
5714 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5715 if (ioc->fw_events_off || ioc->remove_host) {
5716 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305717 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005718 }
5719 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5720
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305721 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06005722 event = le16_to_cpu(mpi_reply->Event);
5723
5724 switch (event) {
5725 /* handle these */
5726 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5727 {
5728 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5729 (Mpi2EventDataSasBroadcastPrimitive_t *)
5730 mpi_reply->EventData;
5731
5732 if (baen_data->Primitive !=
5733 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5734 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305735 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005736 ioc->broadcast_aen_busy = 1;
5737 break;
5738 }
5739
5740 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5741 _scsih_check_topo_delete_events(ioc,
5742 (Mpi2EventDataSasTopologyChangeList_t *)
5743 mpi_reply->EventData);
5744 break;
5745
5746 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5747 case MPI2_EVENT_IR_OPERATION_STATUS:
5748 case MPI2_EVENT_SAS_DISCOVERY:
5749 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5750 case MPI2_EVENT_IR_VOLUME:
5751 case MPI2_EVENT_IR_PHYSICAL_DISK:
5752 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5753 case MPI2_EVENT_TASK_SET_FULL:
5754 break;
5755
5756 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305757 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005758 }
5759
5760 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5761 if (!fw_event) {
5762 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5763 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305764 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005765 }
5766 fw_event->event_data =
5767 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5768 if (!fw_event->event_data) {
5769 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5770 ioc->name, __FILE__, __LINE__, __func__);
5771 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305772 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005773 }
5774
5775 memcpy(fw_event->event_data, mpi_reply->EventData,
5776 mpi_reply->EventDataLength*4);
5777 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305778 fw_event->VF_ID = mpi_reply->VF_ID;
5779 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06005780 fw_event->event = event;
5781 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305782 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06005783}
5784
5785/* shost template */
5786static struct scsi_host_template scsih_driver_template = {
5787 .module = THIS_MODULE,
5788 .name = "Fusion MPT SAS Host",
5789 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005790 .queuecommand = _scsih_qcmd,
5791 .target_alloc = _scsih_target_alloc,
5792 .slave_alloc = _scsih_slave_alloc,
5793 .slave_configure = _scsih_slave_configure,
5794 .target_destroy = _scsih_target_destroy,
5795 .slave_destroy = _scsih_slave_destroy,
5796 .change_queue_depth = _scsih_change_queue_depth,
5797 .change_queue_type = _scsih_change_queue_type,
5798 .eh_abort_handler = _scsih_abort,
5799 .eh_device_reset_handler = _scsih_dev_reset,
5800 .eh_target_reset_handler = _scsih_target_reset,
5801 .eh_host_reset_handler = _scsih_host_reset,
5802 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005803 .can_queue = 1,
5804 .this_id = -1,
5805 .sg_tablesize = MPT2SAS_SG_DEPTH,
5806 .max_sectors = 8192,
5807 .cmd_per_lun = 7,
5808 .use_clustering = ENABLE_CLUSTERING,
5809 .shost_attrs = mpt2sas_host_attrs,
5810 .sdev_attrs = mpt2sas_dev_attrs,
5811};
5812
5813/**
5814 * _scsih_expander_node_remove - removing expander device from list.
5815 * @ioc: per adapter object
5816 * @sas_expander: the sas_device object
5817 * Context: Calling function should acquire ioc->sas_node_lock.
5818 *
5819 * Removing object and freeing associated memory from the
5820 * ioc->sas_expander_list.
5821 *
5822 * Return nothing.
5823 */
5824static void
5825_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5826 struct _sas_node *sas_expander)
5827{
5828 struct _sas_port *mpt2sas_port;
5829 struct _sas_device *sas_device;
5830 struct _sas_node *expander_sibling;
5831 unsigned long flags;
5832
5833 if (!sas_expander)
5834 return;
5835
5836 /* remove sibling ports attached to this expander */
5837 retry_device_search:
5838 list_for_each_entry(mpt2sas_port,
5839 &sas_expander->sas_port_list, port_list) {
5840 if (mpt2sas_port->remote_identify.device_type ==
5841 SAS_END_DEVICE) {
5842 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5843 sas_device =
5844 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5845 mpt2sas_port->remote_identify.sas_address);
5846 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5847 if (!sas_device)
5848 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305849 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305850 if (ioc->shost_recovery)
5851 return;
Eric Moore635374e2009-03-09 01:21:12 -06005852 goto retry_device_search;
5853 }
5854 }
5855
5856 retry_expander_search:
5857 list_for_each_entry(mpt2sas_port,
5858 &sas_expander->sas_port_list, port_list) {
5859
5860 if (mpt2sas_port->remote_identify.device_type ==
5861 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5862 mpt2sas_port->remote_identify.device_type ==
5863 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5864
5865 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5866 expander_sibling =
5867 mpt2sas_scsih_expander_find_by_sas_address(
5868 ioc, mpt2sas_port->remote_identify.sas_address);
5869 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5870 if (!expander_sibling)
5871 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305872 _scsih_expander_remove(ioc,
5873 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305874 if (ioc->shost_recovery)
5875 return;
Eric Moore635374e2009-03-09 01:21:12 -06005876 goto retry_expander_search;
5877 }
5878 }
5879
5880 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305881 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005882
5883 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5884 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5885 sas_expander->handle, (unsigned long long)
5886 sas_expander->sas_address);
5887
5888 list_del(&sas_expander->list);
5889 kfree(sas_expander->phy);
5890 kfree(sas_expander);
5891}
5892
5893/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305894 * _scsih_ir_shutdown - IR shutdown notification
5895 * @ioc: per adapter object
5896 *
5897 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
5898 * the host system is shutting down.
5899 *
5900 * Return nothing.
5901 */
5902static void
5903_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
5904{
5905 Mpi2RaidActionRequest_t *mpi_request;
5906 Mpi2RaidActionReply_t *mpi_reply;
5907 u16 smid;
5908
5909 /* is IR firmware build loaded ? */
5910 if (!ioc->ir_firmware)
5911 return;
5912
5913 /* are there any volumes ? */
5914 if (list_empty(&ioc->raid_device_list))
5915 return;
5916
5917 mutex_lock(&ioc->scsih_cmds.mutex);
5918
5919 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
5920 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
5921 ioc->name, __func__);
5922 goto out;
5923 }
5924 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
5925
5926 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
5927 if (!smid) {
5928 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
5929 ioc->name, __func__);
5930 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
5931 goto out;
5932 }
5933
5934 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
5935 ioc->scsih_cmds.smid = smid;
5936 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
5937
5938 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
5939 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
5940
5941 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
5942 init_completion(&ioc->scsih_cmds.done);
5943 mpt2sas_base_put_smid_default(ioc, smid);
5944 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
5945
5946 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
5947 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
5948 ioc->name, __func__);
5949 goto out;
5950 }
5951
5952 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
5953 mpi_reply = ioc->scsih_cmds.reply;
5954
5955 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
5956 "ioc_status(0x%04x), loginfo(0x%08x)\n",
5957 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
5958 le32_to_cpu(mpi_reply->IOCLogInfo));
5959 }
5960
5961 out:
5962 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
5963 mutex_unlock(&ioc->scsih_cmds.mutex);
5964}
5965
5966/**
5967 * _scsih_shutdown - routine call during system shutdown
5968 * @pdev: PCI device struct
5969 *
5970 * Return nothing.
5971 */
5972static void
5973_scsih_shutdown(struct pci_dev *pdev)
5974{
5975 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5976 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5977
5978 _scsih_ir_shutdown(ioc);
5979 mpt2sas_base_detach(ioc);
5980}
5981
5982/**
Eric Moored5d135b2009-05-18 13:02:08 -06005983 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06005984 * @pdev: PCI device struct
5985 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05305986 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06005987 * Return nothing.
5988 */
5989static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06005990_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005991{
5992 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5993 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5994 struct _sas_port *mpt2sas_port;
5995 struct _sas_device *sas_device;
5996 struct _sas_node *expander_sibling;
5997 struct workqueue_struct *wq;
5998 unsigned long flags;
5999
6000 ioc->remove_host = 1;
6001 _scsih_fw_event_off(ioc);
6002
6003 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6004 wq = ioc->firmware_event_thread;
6005 ioc->firmware_event_thread = NULL;
6006 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6007 if (wq)
6008 destroy_workqueue(wq);
6009
6010 /* free ports attached to the sas_host */
6011 retry_again:
6012 list_for_each_entry(mpt2sas_port,
6013 &ioc->sas_hba.sas_port_list, port_list) {
6014 if (mpt2sas_port->remote_identify.device_type ==
6015 SAS_END_DEVICE) {
6016 sas_device =
6017 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6018 mpt2sas_port->remote_identify.sas_address);
6019 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306020 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006021 goto retry_again;
6022 }
6023 } else {
6024 expander_sibling =
6025 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6026 mpt2sas_port->remote_identify.sas_address);
6027 if (expander_sibling) {
6028 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306029 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006030 goto retry_again;
6031 }
6032 }
6033 }
6034
6035 /* free phys attached to the sas_host */
6036 if (ioc->sas_hba.num_phys) {
6037 kfree(ioc->sas_hba.phy);
6038 ioc->sas_hba.phy = NULL;
6039 ioc->sas_hba.num_phys = 0;
6040 }
6041
6042 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306043 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006044 list_del(&ioc->list);
6045 scsi_remove_host(shost);
6046 scsi_host_put(shost);
6047}
6048
6049/**
6050 * _scsih_probe_boot_devices - reports 1st device
6051 * @ioc: per adapter object
6052 *
6053 * If specified in bios page 2, this routine reports the 1st
6054 * device scsi-ml or sas transport for persistent boot device
6055 * purposes. Please refer to function _scsih_determine_boot_device()
6056 */
6057static void
6058_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6059{
6060 u8 is_raid;
6061 void *device;
6062 struct _sas_device *sas_device;
6063 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306064 u16 handle;
6065 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006066 u64 sas_address;
6067 unsigned long flags;
6068 int rc;
6069
6070 device = NULL;
6071 if (ioc->req_boot_device.device) {
6072 device = ioc->req_boot_device.device;
6073 is_raid = ioc->req_boot_device.is_raid;
6074 } else if (ioc->req_alt_boot_device.device) {
6075 device = ioc->req_alt_boot_device.device;
6076 is_raid = ioc->req_alt_boot_device.is_raid;
6077 } else if (ioc->current_boot_device.device) {
6078 device = ioc->current_boot_device.device;
6079 is_raid = ioc->current_boot_device.is_raid;
6080 }
6081
6082 if (!device)
6083 return;
6084
6085 if (is_raid) {
6086 raid_device = device;
6087 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6088 raid_device->id, 0);
6089 if (rc)
6090 _scsih_raid_device_remove(ioc, raid_device);
6091 } else {
6092 sas_device = device;
6093 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306094 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006095 sas_address = sas_device->sas_address;
6096 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6097 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6098 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6099 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306100 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006101 _scsih_sas_device_remove(ioc, sas_device);
6102 } else if (!sas_device->starget) {
6103 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306104 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006105 _scsih_sas_device_remove(ioc, sas_device);
6106 }
6107 }
6108}
6109
6110/**
6111 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6112 * @ioc: per adapter object
6113 *
6114 * Called during initial loading of the driver.
6115 */
6116static void
6117_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6118{
6119 struct _raid_device *raid_device, *raid_next;
6120 int rc;
6121
6122 list_for_each_entry_safe(raid_device, raid_next,
6123 &ioc->raid_device_list, list) {
6124 if (raid_device->starget)
6125 continue;
6126 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6127 raid_device->id, 0);
6128 if (rc)
6129 _scsih_raid_device_remove(ioc, raid_device);
6130 }
6131}
6132
6133/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306134 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006135 * @ioc: per adapter object
6136 *
6137 * Called during initial loading of the driver.
6138 */
6139static void
6140_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6141{
6142 struct _sas_device *sas_device, *next;
6143 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006144
6145 /* SAS Device List */
6146 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6147 list) {
6148 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6149 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6150 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6151
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306152 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6153 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006154 _scsih_sas_device_remove(ioc, sas_device);
6155 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306156 mpt2sas_transport_port_remove(ioc,
6157 sas_device->sas_address,
6158 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006159 _scsih_sas_device_remove(ioc, sas_device);
6160 }
6161 }
6162}
6163
6164/**
6165 * _scsih_probe_devices - probing for devices
6166 * @ioc: per adapter object
6167 *
6168 * Called during initial loading of the driver.
6169 */
6170static void
6171_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6172{
6173 u16 volume_mapping_flags =
6174 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6175 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6176
6177 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6178 return; /* return when IOC doesn't support initiator mode */
6179
6180 _scsih_probe_boot_devices(ioc);
6181
6182 if (ioc->ir_firmware) {
6183 if ((volume_mapping_flags &
6184 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6185 _scsih_probe_sas(ioc);
6186 _scsih_probe_raid(ioc);
6187 } else {
6188 _scsih_probe_raid(ioc);
6189 _scsih_probe_sas(ioc);
6190 }
6191 } else
6192 _scsih_probe_sas(ioc);
6193}
6194
6195/**
Eric Moored5d135b2009-05-18 13:02:08 -06006196 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006197 * @pdev: PCI device struct
6198 * @id: pci device id
6199 *
6200 * Returns 0 success, anything else error.
6201 */
6202static int
Eric Moored5d135b2009-05-18 13:02:08 -06006203_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006204{
6205 struct MPT2SAS_ADAPTER *ioc;
6206 struct Scsi_Host *shost;
6207
6208 shost = scsi_host_alloc(&scsih_driver_template,
6209 sizeof(struct MPT2SAS_ADAPTER));
6210 if (!shost)
6211 return -ENODEV;
6212
6213 /* init local params */
6214 ioc = shost_priv(shost);
6215 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6216 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006217 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006218 ioc->shost = shost;
6219 ioc->id = mpt_ids++;
6220 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6221 ioc->pdev = pdev;
6222 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6223 ioc->tm_cb_idx = tm_cb_idx;
6224 ioc->ctl_cb_idx = ctl_cb_idx;
6225 ioc->base_cb_idx = base_cb_idx;
6226 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306227 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006228 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306229 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6230 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006231 ioc->logging_level = logging_level;
6232 /* misc semaphores and spin locks */
6233 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6234 spin_lock_init(&ioc->scsi_lookup_lock);
6235 spin_lock_init(&ioc->sas_device_lock);
6236 spin_lock_init(&ioc->sas_node_lock);
6237 spin_lock_init(&ioc->fw_event_lock);
6238 spin_lock_init(&ioc->raid_device_lock);
6239
6240 INIT_LIST_HEAD(&ioc->sas_device_list);
6241 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6242 INIT_LIST_HEAD(&ioc->sas_expander_list);
6243 INIT_LIST_HEAD(&ioc->fw_event_list);
6244 INIT_LIST_HEAD(&ioc->raid_device_list);
6245 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306246 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Eric Moore635374e2009-03-09 01:21:12 -06006247
6248 /* init shost parameters */
6249 shost->max_cmd_len = 16;
6250 shost->max_lun = max_lun;
6251 shost->transportt = mpt2sas_transport_template;
6252 shost->unique_id = ioc->id;
6253
6254 if ((scsi_add_host(shost, &pdev->dev))) {
6255 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6256 ioc->name, __FILE__, __LINE__, __func__);
6257 list_del(&ioc->list);
6258 goto out_add_shost_fail;
6259 }
6260
Eric Moore3c621b32009-05-18 12:59:41 -06006261 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6262 | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306263 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006264
Eric Moore635374e2009-03-09 01:21:12 -06006265 /* event thread */
6266 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6267 "fw_event%d", ioc->id);
6268 ioc->firmware_event_thread = create_singlethread_workqueue(
6269 ioc->firmware_event_name);
6270 if (!ioc->firmware_event_thread) {
6271 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6272 ioc->name, __FILE__, __LINE__, __func__);
6273 goto out_thread_fail;
6274 }
6275
6276 ioc->wait_for_port_enable_to_complete = 1;
6277 if ((mpt2sas_base_attach(ioc))) {
6278 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6279 ioc->name, __FILE__, __LINE__, __func__);
6280 goto out_attach_fail;
6281 }
6282
6283 ioc->wait_for_port_enable_to_complete = 0;
6284 _scsih_probe_devices(ioc);
6285 return 0;
6286
6287 out_attach_fail:
6288 destroy_workqueue(ioc->firmware_event_thread);
6289 out_thread_fail:
6290 list_del(&ioc->list);
6291 scsi_remove_host(shost);
6292 out_add_shost_fail:
6293 return -ENODEV;
6294}
6295
6296#ifdef CONFIG_PM
6297/**
Eric Moored5d135b2009-05-18 13:02:08 -06006298 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006299 * @pdev: PCI device struct
6300 * @state: PM state change to (usually PCI_D3)
6301 *
6302 * Returns 0 success, anything else error.
6303 */
6304static int
Eric Moored5d135b2009-05-18 13:02:08 -06006305_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006306{
6307 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6308 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6309 u32 device_state;
6310
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306311 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006312 flush_scheduled_work();
6313 scsi_block_requests(shost);
6314 device_state = pci_choose_state(pdev, state);
6315 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6316 "operating state [D%d]\n", ioc->name, pdev,
6317 pci_name(pdev), device_state);
6318
6319 mpt2sas_base_free_resources(ioc);
6320 pci_save_state(pdev);
6321 pci_disable_device(pdev);
6322 pci_set_power_state(pdev, device_state);
6323 return 0;
6324}
6325
6326/**
Eric Moored5d135b2009-05-18 13:02:08 -06006327 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006328 * @pdev: PCI device struct
6329 *
6330 * Returns 0 success, anything else error.
6331 */
6332static int
Eric Moored5d135b2009-05-18 13:02:08 -06006333_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006334{
6335 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6336 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6337 u32 device_state = pdev->current_state;
6338 int r;
6339
6340 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6341 "operating state [D%d]\n", ioc->name, pdev,
6342 pci_name(pdev), device_state);
6343
6344 pci_set_power_state(pdev, PCI_D0);
6345 pci_enable_wake(pdev, PCI_D0, 0);
6346 pci_restore_state(pdev);
6347 ioc->pdev = pdev;
6348 r = mpt2sas_base_map_resources(ioc);
6349 if (r)
6350 return r;
6351
6352 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6353 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306354 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006355 return 0;
6356}
6357#endif /* CONFIG_PM */
6358
6359
6360static struct pci_driver scsih_driver = {
6361 .name = MPT2SAS_DRIVER_NAME,
6362 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06006363 .probe = _scsih_probe,
6364 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05306365 .shutdown = _scsih_shutdown,
Eric Moore635374e2009-03-09 01:21:12 -06006366#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06006367 .suspend = _scsih_suspend,
6368 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06006369#endif
6370};
6371
6372
6373/**
Eric Moored5d135b2009-05-18 13:02:08 -06006374 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06006375 *
6376 * Returns 0 success, anything else error.
6377 */
6378static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06006379_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06006380{
6381 int error;
6382
6383 mpt_ids = 0;
6384 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6385 MPT2SAS_DRIVER_VERSION);
6386
6387 mpt2sas_transport_template =
6388 sas_attach_transport(&mpt2sas_transport_functions);
6389 if (!mpt2sas_transport_template)
6390 return -ENODEV;
6391
6392 mpt2sas_base_initialize_callback_handler();
6393
6394 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06006395 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06006396
6397 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06006398 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06006399
6400 /* base internal commands callback handler */
6401 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6402
6403 /* transport internal commands callback handler */
6404 transport_cb_idx = mpt2sas_base_register_callback_handler(
6405 mpt2sas_transport_done);
6406
Kashyap, Desai744090d2009-10-05 15:56:56 +05306407 /* scsih internal commands callback handler */
6408 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
6409
Eric Moore635374e2009-03-09 01:21:12 -06006410 /* configuration page API internal commands callback handler */
6411 config_cb_idx = mpt2sas_base_register_callback_handler(
6412 mpt2sas_config_done);
6413
6414 /* ctl module callback handler */
6415 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6416
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306417 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6418 _scsih_tm_tr_complete);
6419 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6420 _scsih_sas_control_complete);
6421
Eric Moore635374e2009-03-09 01:21:12 -06006422 mpt2sas_ctl_init();
6423
6424 error = pci_register_driver(&scsih_driver);
6425 if (error)
6426 sas_release_transport(mpt2sas_transport_template);
6427
6428 return error;
6429}
6430
6431/**
Eric Moored5d135b2009-05-18 13:02:08 -06006432 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06006433 *
6434 * Returns 0 success, anything else error.
6435 */
6436static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06006437_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06006438{
6439 printk(KERN_INFO "mpt2sas version %s unloading\n",
6440 MPT2SAS_DRIVER_VERSION);
6441
6442 pci_unregister_driver(&scsih_driver);
6443
6444 sas_release_transport(mpt2sas_transport_template);
6445 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6446 mpt2sas_base_release_callback_handler(tm_cb_idx);
6447 mpt2sas_base_release_callback_handler(base_cb_idx);
6448 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306449 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06006450 mpt2sas_base_release_callback_handler(config_cb_idx);
6451 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6452
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306453 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6454 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6455
Eric Moore635374e2009-03-09 01:21:12 -06006456 mpt2sas_ctl_exit();
6457}
6458
Eric Moored5d135b2009-05-18 13:02:08 -06006459module_init(_scsih_init);
6460module_exit(_scsih_exit);