1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2026 LeapIO Tech Inc.
4 *
5 * LeapRAID storage and RAID controller driver.
6 */
7 #include <linux/module.h>
8
9 #include "leapraid_func.h"
10 #include "leapraid.h"
11
12 LIST_HEAD(leapraid_adapter_list);
13 DEFINE_SPINLOCK(leapraid_adapter_lock);
14
15 MODULE_AUTHOR(LEAPRAID_AUTHOR);
16 MODULE_DESCRIPTION(LEAPRAID_DESCRIPTION);
17 MODULE_LICENSE("GPL");
18 MODULE_VERSION(LEAPRAID_DRIVER_VERSION);
19
20 static atomic_t leapraid_ids = ATOMIC_INIT(0);
21
22 static int open_pcie_trace = 1;
23 module_param(open_pcie_trace, int, 0644);
24 MODULE_PARM_DESC(open_pcie_trace, "Default=1(open)/0(close)");
25
26 static int enable_mp = 1;
27 module_param(enable_mp, int, 0444);
28 MODULE_PARM_DESC(enable_mp,
29 "Enable multipath on target device. default=1(enable)");
30
leapraid_get_sense_data(char * sense,struct sense_info * data)31 static inline void leapraid_get_sense_data(char *sense,
32 struct sense_info *data)
33 {
34 bool desc_format = (sense[0] & SCSI_SENSE_RESPONSE_CODE_MASK) >=
35 DESC_FORMAT_THRESHOLD;
36
37 if (desc_format) {
38 data->sense_key = sense[1] & SENSE_KEY_MASK;
39 data->asc = sense[2];
40 data->ascq = sense[3];
41 } else {
42 data->sense_key = sense[2] & SENSE_KEY_MASK;
43 data->asc = sense[12];
44 data->ascq = sense[13];
45 }
46 }
47
pdev_to_adapter(struct pci_dev * pdev)48 static struct leapraid_adapter *pdev_to_adapter(struct pci_dev *pdev)
49 {
50 struct Scsi_Host *shost = pci_get_drvdata(pdev);
51
52 if (!shost)
53 return NULL;
54
55 return shost_priv(shost);
56 }
57
leapraid_set_tm_flg(struct leapraid_adapter * adapter,u16 hdl)58 void leapraid_set_tm_flg(struct leapraid_adapter *adapter, u16 hdl)
59 {
60 struct leapraid_sdev_priv *sdev_priv;
61 struct scsi_device *sdev;
62 bool skip = false;
63
64 /* Iterate over all devices. */
65 shost_for_each_device(sdev, adapter->shost) {
66 if (skip)
67 continue;
68
69 sdev_priv = sdev->hostdata;
70 if (!sdev_priv)
71 continue;
72
73 if (sdev_priv->starget_priv->hdl == hdl) {
74 sdev_priv->starget_priv->tm_busy = 1;
75 skip = true;
76 }
77 }
78 }
79
leapraid_clear_tm_flg(struct leapraid_adapter * adapter,u16 hdl)80 void leapraid_clear_tm_flg(struct leapraid_adapter *adapter, u16 hdl)
81 {
82 struct leapraid_sdev_priv *sdev_priv;
83 struct scsi_device *sdev;
84 bool skip = false;
85
86 /* Iterate over all devices. */
87 shost_for_each_device(sdev, adapter->shost) {
88 if (skip)
89 continue;
90
91 sdev_priv = sdev->hostdata;
92 if (!sdev_priv)
93 continue;
94
95 if (sdev_priv->starget_priv->hdl == hdl) {
96 sdev_priv->starget_priv->tm_busy = 0;
97 skip = true;
98 }
99 }
100 }
101
leapraid_tm_cmd_map_status(struct leapraid_adapter * adapter,uint channel,uint id,uint lun,u8 type,u16 taskid_task)102 static int leapraid_tm_cmd_map_status(struct leapraid_adapter *adapter,
103 uint channel,
104 uint id,
105 uint lun,
106 u8 type,
107 u16 taskid_task)
108 {
109 int rc = FAILED;
110
111 if (taskid_task <= adapter->shost->can_queue) {
112 switch (type) {
113 case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET:
114 case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET:
115 if (!leapraid_scmd_find_by_lun(adapter, id, lun,
116 channel))
117 rc = SUCCESS;
118 break;
119 case LEAPRAID_TM_TASKTYPE_TARGET_RESET:
120 if (!leapraid_scmd_find_by_tgt(adapter, id, channel))
121 rc = SUCCESS;
122 break;
123 default:
124 rc = SUCCESS;
125 }
126 }
127
128 if (taskid_task == adapter->driver_cmds.ctl_cmd.hp_taskid &&
129 (adapter->driver_cmds.ctl_cmd.status &
130 LEAPRAID_CMD_DONE ||
131 adapter->driver_cmds.ctl_cmd.status &
132 LEAPRAID_CMD_NOT_USED))
133 rc = SUCCESS;
134
135 return rc;
136 }
137
leapraid_tm_post_processing(struct leapraid_adapter * adapter,u16 hdl,uint channel,uint id,uint lun,u8 type,u16 taskid_task)138 static int leapraid_tm_post_processing(struct leapraid_adapter *adapter,
139 u16 hdl, uint channel, uint id,
140 uint lun, u8 type, u16 taskid_task)
141 {
142 int rc;
143
144 rc = leapraid_tm_cmd_map_status(adapter, channel, id, lun,
145 type, taskid_task);
146 if (rc == SUCCESS)
147 return rc;
148
149 leapraid_mask_int(adapter);
150 leapraid_sync_irqs(adapter, true);
151 leapraid_unmask_int(adapter);
152
153 return leapraid_tm_cmd_map_status(adapter, channel, id, lun, type,
154 taskid_task);
155 }
156
leapraid_build_tm_req(struct leapraid_scsi_tm_req * scsi_tm_req,u16 hdl,uint lun,u8 type,u8 tr_method,u16 target_taskid)157 static void leapraid_build_tm_req(struct leapraid_scsi_tm_req *scsi_tm_req,
158 u16 hdl, uint lun, u8 type, u8 tr_method,
159 u16 target_taskid)
160 {
161 memset(scsi_tm_req, 0, sizeof(*scsi_tm_req));
162 scsi_tm_req->func = LEAPRAID_FUNC_SCSI_TMF;
163 scsi_tm_req->dev_hdl = cpu_to_le16(hdl);
164 scsi_tm_req->task_type = type;
165 scsi_tm_req->msg_flg = tr_method;
166 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ||
167 type == LEAPRAID_TM_TASKTYPE_QUERY_TASK)
168 scsi_tm_req->task_mid = cpu_to_le16(target_taskid);
169 int_to_scsilun(lun, (struct scsi_lun *)scsi_tm_req->lun);
170 }
171
leapraid_issue_tm(struct leapraid_adapter * adapter,u16 hdl,uint channel,uint id,uint lun,u8 type,u16 target_taskid,u8 tr_method)172 int leapraid_issue_tm(struct leapraid_adapter *adapter, u16 hdl, uint channel,
173 uint id, uint lun, u8 type,
174 u16 target_taskid, u8 tr_method)
175 {
176 struct leapraid_scsi_tm_req *scsi_tm_req;
177 struct leapraid_scsiio_req *scsiio_req;
178 struct leapraid_io_req_tracker *io_req_tracker = NULL;
179 u16 msix_task;
180 u16 taskid;
181 bool issue_reset = false;
182 u32 db;
183 int rc;
184
185 lockdep_assert_held(&adapter->driver_cmds.tm_cmd.mutex);
186
187 if (adapter->access_ctrl.shost_recovering ||
188 adapter->access_ctrl.host_removing ||
189 adapter->access_ctrl.pcie_recovering) {
190 dev_info(&adapter->pdev->dev,
191 "%s %s: Host is recovering, skip tm command!\n",
192 __func__, adapter->adapter_attr.name);
193 return FAILED;
194 }
195
196 db = leapraid_readl(&adapter->iomem_base->db);
197 if (db & LEAPRAID_DB_USED) {
198 dev_info(&adapter->pdev->dev,
199 "%s Unexpected db status, issuing hard reset!\n",
200 adapter->adapter_attr.name);
201 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
202 __func__, __LINE__);
203 rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
204 return !rc ? SUCCESS : FAILED;
205 }
206
207 if ((db & LEAPRAID_DB_MASK) == LEAPRAID_DB_FAULT) {
208 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
209 __func__, __LINE__);
210 rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
211 return !rc ? SUCCESS : FAILED;
212 }
213
214 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK)
215 io_req_tracker =
216 leapraid_get_io_tracker_from_taskid(adapter,
217 target_taskid);
218
219 adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_PENDING;
220 scsi_tm_req =
221 leapraid_get_task_desc(adapter,
222 adapter->driver_cmds.tm_cmd.hp_taskid);
223 leapraid_build_tm_req(scsi_tm_req, hdl, lun, type, tr_method,
224 target_taskid);
225 memset(&adapter->driver_cmds.tm_cmd.reply, 0,
226 sizeof(struct leapraid_scsi_tm_rep));
227 leapraid_set_tm_flg(adapter, hdl);
228 init_completion(&adapter->driver_cmds.tm_cmd.done);
229 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK &&
230 io_req_tracker &&
231 io_req_tracker->msix_io < adapter->adapter_attr.rq_cnt)
232 msix_task = io_req_tracker->msix_io;
233 else
234 msix_task = 0;
235 taskid = adapter->driver_cmds.tm_cmd.hp_taskid;
236 leapraid_fire_hpr_task(adapter, taskid, msix_task);
237 wait_for_completion_timeout(&adapter->driver_cmds.tm_cmd.done,
238 LEAPRAID_TM_CMD_TIMEOUT * HZ);
239 if (!(adapter->driver_cmds.tm_cmd.status & LEAPRAID_CMD_DONE)) {
240 dev_err(&adapter->pdev->dev,
241 "%s: TM cmd timeout, status=0x%x\n",
242 __func__, adapter->driver_cmds.tm_cmd.status);
243 leapraid_log_req_context(adapter, taskid, scsi_tm_req);
244 issue_reset =
245 leapraid_check_reset(
246 adapter->driver_cmds.tm_cmd.status);
247 if (issue_reset) {
248 dev_info(&adapter->pdev->dev,
249 "%s:%d: call hard_reset\n",
250 __func__, __LINE__);
251 rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
252 rc = !rc ? SUCCESS : FAILED;
253 goto out_cleanup;
254 }
255 }
256
257 leapraid_sync_irqs(adapter, false);
258
259 switch (type) {
260 case LEAPRAID_TM_TASKTYPE_TARGET_RESET:
261 case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET:
262 case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET:
263 rc = leapraid_tm_post_processing(adapter, hdl, channel, id,
264 lun, type, target_taskid);
265 break;
266 case LEAPRAID_TM_TASKTYPE_ABORT_TASK:
267 rc = SUCCESS;
268 scsiio_req = leapraid_get_task_desc(adapter, target_taskid);
269 if (le16_to_cpu(scsiio_req->dev_hdl) != hdl)
270 break;
271 dev_err(&adapter->pdev->dev, "%s: Abort failed, hdl=0x%04x\n",
272 adapter->adapter_attr.name, hdl);
273 rc = FAILED;
274 break;
275 case LEAPRAID_TM_TASKTYPE_QUERY_TASK:
276 rc = SUCCESS;
277 break;
278 default:
279 rc = FAILED;
280 break;
281 }
282
283 out_cleanup:
284 leapraid_clear_tm_flg(adapter, hdl);
285 adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_NOT_USED;
286 return rc;
287 }
288
leapraid_issue_locked_tm(struct leapraid_adapter * adapter,u16 hdl,uint channel,uint id,uint lun,u8 type,u16 target_taskid,u8 tr_method)289 int leapraid_issue_locked_tm(struct leapraid_adapter *adapter, u16 hdl,
290 uint channel, uint id, uint lun, u8 type,
291 u16 target_taskid, u8 tr_method)
292 {
293 int rc;
294
295 mutex_lock(&adapter->driver_cmds.tm_cmd.mutex);
296 rc = leapraid_issue_tm(adapter, hdl, channel, id, lun, type,
297 target_taskid, tr_method);
298 mutex_unlock(&adapter->driver_cmds.tm_cmd.mutex);
299
300 return rc;
301 }
302
leapraid_smart_fault_detect(struct leapraid_adapter * adapter,u16 hdl)303 void leapraid_smart_fault_detect(struct leapraid_adapter *adapter, u16 hdl)
304 {
305 struct leapraid_starget_priv *starget_priv;
306 struct leapraid_sas_dev *sas_dev;
307 struct scsi_target *starget;
308 unsigned long flags;
309
310 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
311 sas_dev = leapraid_hold_lock_get_sas_dev_by_hdl(adapter, hdl);
312 if (!sas_dev) {
313 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
314 return;
315 }
316
317 starget = sas_dev->starget;
318 starget_priv = starget ? starget->hostdata : NULL;
319 if (!starget_priv) {
320 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
321 goto release_sdev;
322 }
323
324 if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER ||
325 starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
326 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
327 goto release_sdev;
328 }
329
330 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
331 leapraid_async_turn_on_led(adapter, hdl);
332 release_sdev:
333 if (sas_dev)
334 leapraid_sdev_put(sas_dev);
335 }
336
leapraid_process_sense_data(struct leapraid_adapter * adapter,struct leapraid_scsiio_rep * scsiio_rep,struct scsi_cmnd * scmd,u16 taskid)337 static void leapraid_process_sense_data(struct leapraid_adapter *adapter,
338 struct leapraid_scsiio_rep *scsiio_rep,
339 struct scsi_cmnd *scmd, u16 taskid)
340 {
341 struct sense_info data;
342 const void *sense_data;
343 u32 sz;
344
345 if (!(scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID))
346 return;
347
348 sense_data = leapraid_get_sense_buffer(adapter, taskid);
349 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
350 le32_to_cpu(scsiio_rep->sense_count));
351
352 memcpy(scmd->sense_buffer, sense_data, sz);
353 leapraid_get_sense_data(scmd->sense_buffer, &data);
354 if (data.asc == ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED)
355 leapraid_smart_fault_detect(adapter,
356 le16_to_cpu(scsiio_rep->dev_hdl));
357 }
358
leapraid_handle_data_underrun(struct leapraid_scsiio_rep * scsiio_rep,struct scsi_cmnd * scmd,u32 xfer_cnt)359 static void leapraid_handle_data_underrun(
360 struct leapraid_scsiio_rep *scsiio_rep,
361 struct scsi_cmnd *scmd, u32 xfer_cnt)
362 {
363 u8 scsi_status = scsiio_rep->scsi_status;
364 u8 scsi_state = scsiio_rep->scsi_state;
365
366 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status;
367
368 if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID)
369 return;
370
371 if (xfer_cnt < scmd->underflow) {
372 if (scsi_status == SAM_STAT_BUSY)
373 scmd->result = SAM_STAT_BUSY;
374 else
375 scmd->result = DID_SOFT_ERROR <<
376 LEAPRAID_SCSI_HOST_SHIFT;
377 } else if (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED |
378 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) {
379 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
380 } else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED) {
381 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
382 } else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
383 scsiio_rep->scsi_state = LEAPRAID_SCSI_STATE_AUTOSENSE_VALID;
384 scsiio_rep->scsi_status = SAM_STAT_CHECK_CONDITION;
385 scsi_build_sense(scmd, 0, ILLEGAL_REQUEST,
386 LEAPRAID_SCSI_ASC_INVALID_CMD_CODE,
387 LEAPRAID_SCSI_ASCQ_DEFAULT);
388 }
389 }
390
leapraid_handle_success_status(struct leapraid_scsiio_rep * scsiio_rep,struct scsi_cmnd * scmd,u32 response_code)391 static void leapraid_handle_success_status(
392 struct leapraid_scsiio_rep *scsiio_rep,
393 struct scsi_cmnd *scmd,
394 u32 response_code)
395 {
396 u8 scsi_status = scsiio_rep->scsi_status;
397 u8 scsi_state = scsiio_rep->scsi_state;
398
399 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status;
400
401 if (response_code == LEAPRAID_TM_RSP_INVALID_FRAME ||
402 (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED |
403 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)))
404 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
405 else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED)
406 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
407 }
408
leapraid_scsiio_done_dispatch(struct leapraid_adapter * adapter,struct leapraid_scsiio_rep * scsiio_rep,struct leapraid_sdev_priv * sdev_priv,struct scsi_cmnd * scmd,u16 taskid,u32 response_code)409 static void leapraid_scsiio_done_dispatch(
410 struct leapraid_adapter *adapter,
411 struct leapraid_scsiio_rep *scsiio_rep,
412 struct leapraid_sdev_priv *sdev_priv,
413 struct scsi_cmnd *scmd,
414 u16 taskid, u32 response_code)
415 {
416 u8 scsi_status = scsiio_rep->scsi_status;
417 u8 scsi_state = scsiio_rep->scsi_state;
418 u16 adapter_status;
419 u32 xfer_cnt;
420 u32 sz;
421
422 adapter_status = le16_to_cpu(scsiio_rep->adapter_status) &
423 LEAPRAID_ADAPTER_STATUS_MASK;
424
425 xfer_cnt = le32_to_cpu(scsiio_rep->transfer_count);
426 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
427
428 if (adapter_status == LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN &&
429 xfer_cnt == 0 &&
430 (scsi_status == LEAPRAID_SCSI_STATUS_BUSY ||
431 scsi_status == LEAPRAID_SCSI_STATUS_RESERVATION_CONFLICT ||
432 scsi_status == LEAPRAID_SCSI_STATUS_TASK_SET_FULL))
433 adapter_status = LEAPRAID_ADAPTER_STATUS_SUCCESS;
434
435 switch (adapter_status) {
436 case LEAPRAID_ADAPTER_STATUS_SCSI_DEVICE_NOT_THERE:
437 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
438 break;
439
440 case LEAPRAID_ADAPTER_STATUS_BUSY:
441 case LEAPRAID_ADAPTER_STATUS_INSUFFICIENT_RESOURCES:
442 scmd->result = SAM_STAT_BUSY;
443 break;
444
445 case LEAPRAID_ADAPTER_STATUS_SCSI_RESIDUAL_MISMATCH:
446 if (xfer_cnt == 0 || scmd->underflow > xfer_cnt)
447 scmd->result = DID_SOFT_ERROR <<
448 LEAPRAID_SCSI_HOST_SHIFT;
449 else
450 scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) |
451 scsi_status;
452 break;
453
454 case LEAPRAID_ADAPTER_STATUS_SCSI_ADAPTER_TERMINATED:
455 if (sdev_priv->block) {
456 scmd->result = DID_TRANSPORT_DISRUPTED <<
457 LEAPRAID_SCSI_HOST_SHIFT;
458 return;
459 }
460
461 if (scmd->device->channel == RAID_CHANNEL &&
462 scsi_state == (LEAPRAID_SCSI_STATE_TERMINATED |
463 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) {
464 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
465 break;
466 }
467
468 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
469 break;
470
471 case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_TERMINATED:
472 case LEAPRAID_ADAPTER_STATUS_SCSI_EXT_TERMINATED:
473 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
474 break;
475
476 case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN:
477 leapraid_handle_data_underrun(scsiio_rep, scmd, xfer_cnt);
478 break;
479
480 case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_OVERRUN:
481 scsi_set_resid(scmd, 0);
482 leapraid_handle_success_status(scsiio_rep, scmd,
483 response_code);
484 break;
485 case LEAPRAID_ADAPTER_STATUS_SCSI_RECOVERED_ERROR:
486 case LEAPRAID_ADAPTER_STATUS_SUCCESS:
487 leapraid_handle_success_status(scsiio_rep, scmd,
488 response_code);
489 break;
490
491 case LEAPRAID_ADAPTER_STATUS_SCSI_PROTOCOL_ERROR:
492 case LEAPRAID_ADAPTER_STATUS_INTERNAL_ERROR:
493 case LEAPRAID_ADAPTER_STATUS_SCSI_IO_DATA_ERROR:
494 case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_MGMT_FAILED:
495 default:
496 scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
497 break;
498 }
499
500 if (!scmd->result)
501 return;
502
503 scsi_print_command(scmd);
504 dev_warn(&adapter->pdev->dev,
505 "SCSI I/O: hdl=0x%x, status: 0x%x, 0x%x, 0x%x\n",
506 le16_to_cpu(scsiio_rep->dev_hdl), adapter_status,
507 scsi_status, scsi_state);
508
509 if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID) {
510 struct scsi_sense_hdr sshdr;
511
512 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
513 le32_to_cpu(scsiio_rep->sense_count));
514 if (scsi_normalize_sense(scmd->sense_buffer, sz,
515 &sshdr))
516 dev_warn(&adapter->pdev->dev,
517 "Sense: key=0x%x asc=0x%x ascq=0x%x\n",
518 sshdr.sense_key, sshdr.asc,
519 sshdr.ascq);
520 else
521 dev_warn(&adapter->pdev->dev,
522 "Sense: Invalid sense data\n");
523 }
524 }
525
leapraid_scsiio_done(struct leapraid_adapter * adapter,u16 taskid,u8 msix_index,u32 rep)526 bool leapraid_scsiio_done(struct leapraid_adapter *adapter, u16 taskid,
527 u8 msix_index, u32 rep)
528 {
529 struct leapraid_scsiio_rep *scsiio_rep;
530 struct leapraid_sdev_priv *sdev_priv;
531 struct scsi_cmnd *scmd;
532 u32 response_code = 0;
533
534 scmd = leapraid_get_scmd_from_taskid(adapter, taskid);
535 if (!scmd)
536 return true;
537
538 scsiio_rep = leapraid_get_reply_vaddr(adapter, rep);
539 if (!scsiio_rep) {
540 scmd->result = DID_OK << LEAPRAID_SCSI_HOST_SHIFT;
541 goto out_scsiio_done;
542 }
543
544 sdev_priv = scmd->device->hostdata;
545 if (!sdev_priv ||
546 !sdev_priv->starget_priv ||
547 sdev_priv->starget_priv->deleted) {
548 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
549 goto out_scsiio_done;
550 }
551
552 if (scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_RESPONSE_INFO_VALID)
553 response_code = le32_to_cpu(scsiio_rep->resp_info) & 0xFF;
554
555 leapraid_process_sense_data(adapter, scsiio_rep, scmd, taskid);
556 leapraid_scsiio_done_dispatch(adapter, scsiio_rep, sdev_priv, scmd,
557 taskid, response_code);
558
559 out_scsiio_done:
560 scsi_dma_unmap(scmd);
561 leapraid_free_taskid(adapter, taskid);
562 scsi_done(scmd);
563 return false;
564 }
565
leapraid_probe_raid(struct leapraid_adapter * adapter)566 static void leapraid_probe_raid(struct leapraid_adapter *adapter)
567 {
568 struct leapraid_raid_volume *raid_volume, *next_raid_volume;
569 unsigned long flags;
570 LIST_HEAD(head);
571 int rc;
572
573 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
574 list_splice_init(&adapter->dev_topo.raid_volume_list, &head);
575 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock, flags);
576
577 list_for_each_entry_safe(raid_volume, next_raid_volume, &head, list) {
578 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
579 list_move_tail(&raid_volume->list,
580 &adapter->dev_topo.raid_volume_list);
581 if (raid_volume->starget) {
582 spin_unlock_irqrestore(
583 &adapter->dev_topo.raid_volume_lock, flags);
584 continue;
585 }
586
587 leapraid_raid_volume_get(raid_volume);
588 spin_unlock_irqrestore(
589 &adapter->dev_topo.raid_volume_lock, flags);
590
591 rc = scsi_add_device(adapter->shost, RAID_CHANNEL,
592 raid_volume->id, 0);
593 if (rc)
594 leapraid_raid_volume_remove(adapter, raid_volume);
595
596 leapraid_raid_volume_put(raid_volume);
597 }
598 }
599
leapraid_sas_dev_make_active(struct leapraid_adapter * adapter,struct leapraid_sas_dev * sas_dev)600 static void leapraid_sas_dev_make_active(struct leapraid_adapter *adapter,
601 struct leapraid_sas_dev *sas_dev)
602 {
603 unsigned long flags;
604
605 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
606 if (!list_empty(&sas_dev->list)) {
607 list_del_init(&sas_dev->list);
608 leapraid_sdev_put(sas_dev);
609 }
610
611 leapraid_sdev_get(sas_dev);
612 list_add_tail(&sas_dev->list, &adapter->dev_topo.sas_dev_list);
613 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
614 }
615
leapraid_probe_sas(struct leapraid_adapter * adapter)616 static void leapraid_probe_sas(struct leapraid_adapter *adapter)
617 {
618 struct leapraid_sas_dev *sas_dev;
619 bool added;
620
621 for (;;) {
622 sas_dev = leapraid_get_next_sas_dev_from_init_list(adapter);
623 if (!sas_dev)
624 break;
625 added = leapraid_transport_port_add(adapter,
626 sas_dev->hdl,
627 sas_dev->parent_sas_addr,
628 sas_dev->card_port);
629 if (!added)
630 goto remove_dev;
631
632 if (!sas_dev->starget &&
633 !adapter->scan_dev_desc.driver_loading) {
634 leapraid_transport_port_remove(adapter,
635 sas_dev->sas_addr,
636 sas_dev->parent_sas_addr,
637 sas_dev->card_port);
638 goto remove_dev;
639 }
640
641 leapraid_sas_dev_make_active(adapter, sas_dev);
642 leapraid_sdev_put(sas_dev);
643 continue;
644
645 remove_dev:
646 leapraid_sas_dev_remove(adapter, sas_dev);
647 leapraid_sdev_put(sas_dev);
648 }
649 }
650
leapraid_get_boot_dev(struct leapraid_adapter * adapter,struct leapraid_boot_dev * boot_dev,void ** pdev,u32 * pchnl)651 static bool leapraid_get_boot_dev(struct leapraid_adapter *adapter,
652 struct leapraid_boot_dev *boot_dev,
653 void **pdev, u32 *pchnl)
654 {
655 unsigned long flags;
656 void *dev;
657 u32 chnl;
658
659 spin_lock_irqsave(&adapter->boot_devs.lock, flags);
660 if (!boot_dev->dev) {
661 spin_unlock_irqrestore(&adapter->boot_devs.lock, flags);
662 return false;
663 }
664
665 dev = boot_dev->dev;
666 chnl = boot_dev->chnl;
667 leapraid_boot_dev_get(dev, chnl);
668 spin_unlock_irqrestore(&adapter->boot_devs.lock, flags);
669
670 *pdev = dev;
671 *pchnl = chnl;
672 return true;
673 }
674
leapraid_probe_boot_dev(struct leapraid_adapter * adapter)675 static void leapraid_probe_boot_dev(struct leapraid_adapter *adapter)
676 {
677 struct leapraid_raid_volume *raid_volume;
678 struct leapraid_sas_dev *sas_dev;
679 struct leapraid_sas_port *sport;
680 unsigned long flags;
681 void *dev = NULL;
682 u32 chnl;
683
684 if (leapraid_get_boot_dev(adapter,
685 &adapter->boot_devs.requested_boot_dev,
686 &dev, &chnl))
687 goto boot_dev_found;
688
689 if (leapraid_get_boot_dev(adapter,
690 &adapter->boot_devs.requested_alt_boot_dev,
691 &dev, &chnl))
692 goto boot_dev_found;
693
694 if (leapraid_get_boot_dev(adapter,
695 &adapter->boot_devs.current_boot_dev,
696 &dev, &chnl))
697 goto boot_dev_found;
698
699 return;
700
701 boot_dev_found:
702 switch (chnl) {
703 case RAID_CHANNEL:
704 raid_volume = dev;
705
706 if (raid_volume->starget)
707 break;
708
709 /* TODO eedp */
710
711 if (scsi_add_device(adapter->shost, RAID_CHANNEL,
712 raid_volume->id, 0))
713 leapraid_raid_volume_remove(adapter, raid_volume);
714 break;
715 default:
716 sas_dev = dev;
717
718 if (sas_dev->starget)
719 break;
720
721 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
722 list_move_tail(&sas_dev->list,
723 &adapter->dev_topo.sas_dev_list);
724 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
725
726 if (!sas_dev->card_port)
727 break;
728
729 sport = leapraid_transport_port_add(adapter, sas_dev->hdl,
730 sas_dev->parent_sas_addr,
731 sas_dev->card_port);
732 if (!sport)
733 leapraid_sas_dev_remove(adapter, sas_dev);
734 break;
735 }
736
737 leapraid_boot_dev_put(dev, chnl);
738 }
739
leapraid_probe_devices(struct leapraid_adapter * adapter)740 static void leapraid_probe_devices(struct leapraid_adapter *adapter)
741 {
742 leapraid_probe_boot_dev(adapter);
743
744 if (adapter->adapter_attr.raid_support) {
745 leapraid_probe_raid(adapter);
746 leapraid_probe_sas(adapter);
747 } else {
748 leapraid_probe_sas(adapter);
749 }
750 }
751
leapraid_scan_dev_done(struct leapraid_adapter * adapter)752 void leapraid_scan_dev_done(struct leapraid_adapter *adapter)
753 {
754 if (adapter->scan_dev_desc.wait_scan_dev_done) {
755 adapter->scan_dev_desc.wait_scan_dev_done = 0;
756 leapraid_probe_devices(adapter);
757 }
758
759 adapter->scan_dev_desc.scan_start = 0;
760
761 leapraid_check_scheduled_fault_start(adapter);
762 leapraid_fw_log_start(adapter);
763 adapter->scan_dev_desc.driver_loading = 0;
764 wake_up(&adapter->scan_dev_desc.wait_driver_loading);
765 }
766
767 static const struct pci_device_id leapraid_pci_table[] = {
768 { PCI_DEVICE_SUB(LEAPRAID_VENDOR_ID, LEAPRAID_DEVID_HBA,
769 LEAPRAID_SUBVENDOR_ID,
770 LEAPRAID_SUBDEVID_HBA) },
771 { 0, }
772 };
773
leapraid_is_scmd_permitted(struct leapraid_adapter * adapter,struct scsi_cmnd * scmd)774 static inline bool leapraid_is_scmd_permitted(struct leapraid_adapter *adapter,
775 struct scsi_cmnd *scmd)
776 {
777 u8 opcode;
778
779 if (adapter->access_ctrl.pcie_recovering ||
780 atomic_read(&adapter->overheat_desc.thermal_alert))
781 return false;
782
783 if (adapter->access_ctrl.host_removing) {
784 if (leapraid_pci_removed(adapter))
785 return false;
786
787 opcode = scmd->cmnd[0];
788 return opcode == SYNCHRONIZE_CACHE || opcode == START_STOP;
789 }
790 return true;
791 }
792
leapraid_should_queuecommand(struct leapraid_adapter * adapter,struct leapraid_sdev_priv * sdev_priv,struct scsi_cmnd * scmd,enum scsi_qc_status * rc)793 static bool leapraid_should_queuecommand(struct leapraid_adapter *adapter,
794 struct leapraid_sdev_priv *sdev_priv,
795 struct scsi_cmnd *scmd,
796 enum scsi_qc_status *rc)
797 {
798 struct leapraid_starget_priv *starget_priv;
799
800 if (!sdev_priv || !sdev_priv->starget_priv)
801 goto no_connect;
802
803 if (!leapraid_is_scmd_permitted(adapter, scmd))
804 goto no_connect;
805
806 starget_priv = sdev_priv->starget_priv;
807 if (starget_priv->hdl == LEAPRAID_INVALID_DEV_HANDLE)
808 goto no_connect;
809
810 if (sdev_priv->block &&
811 scsi_get_host_state(scmd->device->host) == SHOST_RECOVERY &&
812 scmd->cmnd[0] == TEST_UNIT_READY) {
813 scsi_build_sense(scmd, 0, UNIT_ATTENTION,
814 LEAPRAID_SCSI_ASC_POWER_ON_RESET,
815 LEAPRAID_SCSI_ASCQ_POWER_ON_RESET);
816 goto scsiio_done;
817 }
818
819 if (adapter->access_ctrl.shost_recovering ||
820 adapter->reset_desc.adapter_link_resetting) {
821 *rc = SCSI_MLQUEUE_HOST_BUSY;
822 return false;
823 }
824
825 if (starget_priv->deleted || sdev_priv->deleted)
826 goto no_connect;
827
828 if (starget_priv->tm_busy || sdev_priv->block) {
829 *rc = SCSI_MLQUEUE_DEVICE_BUSY;
830 return false;
831 }
832
833 return true;
834
835 no_connect:
836 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
837 scsiio_done:
838 scsi_done(scmd);
839
840 return false;
841 }
842
build_scsiio_req_control(struct scsi_cmnd * scmd,struct leapraid_sdev_priv * sdev_priv)843 static u32 build_scsiio_req_control(struct scsi_cmnd *scmd,
844 struct leapraid_sdev_priv *sdev_priv)
845 {
846 u32 control;
847
848 switch (scmd->sc_data_direction) {
849 case DMA_FROM_DEVICE:
850 control = LEAPRAID_SCSIIO_CTRL_READ;
851 break;
852 case DMA_TO_DEVICE:
853 control = LEAPRAID_SCSIIO_CTRL_WRITE;
854 break;
855 default:
856 control = LEAPRAID_SCSIIO_CTRL_NODATATRANSFER;
857 break;
858 }
859
860 control |= LEAPRAID_SCSIIO_CTRL_SIMPLEQ;
861
862 if (sdev_priv->ncq_prio_enable &&
863 (IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd))) ==
864 IOPRIO_CLASS_RT))
865 control |= LEAPRAID_SCSIIO_CTRL_CMDPRI;
866 if (scmd->cmd_len == 32)
867 control |= LEAPRAID_SCSIIO_CTRL_CDB_32BYTE <<
868 LEAPRAID_SCSIIO_CTRL_CDB_LEN_SHIFT;
869
870 return control;
871 }
872
leapraid_queuecommand(struct Scsi_Host * shost,struct scsi_cmnd * scmd)873 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost,
874 struct scsi_cmnd *scmd)
875 {
876 struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
877 struct leapraid_sdev_priv *sdev_priv = scmd->device->hostdata;
878 struct leapraid_starget_priv *starget_priv;
879 struct leapraid_scsiio_req *scsiio_req;
880 u32 control;
881 u16 taskid;
882 u16 hdl;
883 enum scsi_qc_status rc = 0;
884
885 if (!leapraid_should_queuecommand(adapter, sdev_priv, scmd, &rc))
886 return rc;
887
888 starget_priv = sdev_priv->starget_priv;
889 hdl = starget_priv->hdl;
890 control = build_scsiio_req_control(scmd, sdev_priv);
891
892 taskid = leapraid_alloc_scsiio_taskid(adapter, scmd);
893 scsiio_req = leapraid_get_task_desc(adapter, taskid);
894
895 scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
896 if (sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
897 scsiio_req->func = LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH;
898 else
899 scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
900
901 scsiio_req->dev_hdl = cpu_to_le16(hdl);
902 scsiio_req->data_len = cpu_to_le32(scsi_bufflen(scmd));
903 scsiio_req->ctrl = cpu_to_le32(control);
904 scsiio_req->io_flg = cpu_to_le16(scmd->cmd_len);
905 scsiio_req->msg_flg = 0;
906 scsiio_req->sense_buffer_len = SCSI_SENSE_BUFFERSIZE;
907 scsiio_req->sense_buffer_low_add =
908 leapraid_get_sense_buffer_dma(adapter, taskid);
909 scsiio_req->sgl_offset0 =
910 offsetof(struct leapraid_scsiio_req, sgl) /
911 LEAPRAID_DWORDS_BYTE_SIZE;
912 int_to_scsilun(sdev_priv->lun, (struct scsi_lun *)scsiio_req->lun);
913 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
914 if (scsiio_req->data_len) {
915 if (leapraid_build_scmd_ieee_sg(adapter, scmd, taskid)) {
916 leapraid_free_taskid(adapter, taskid);
917 return SCSI_MLQUEUE_HOST_BUSY;
918 }
919 } else {
920 leapraid_build_ieee_nodata_sg(adapter, &scsiio_req->sgl);
921 }
922
923 if (likely(scsiio_req->func == LEAPRAID_FUNC_SCSIIO))
924 leapraid_fire_scsi_io(adapter, taskid,
925 le16_to_cpu(scsiio_req->dev_hdl));
926 else
927 leapraid_fire_task(adapter, taskid);
928
929 dev_dbg(&adapter->pdev->dev,
930 "LEAPRAID_SCSIIO: Send Descriptor taskid %d, req type 0x%x\n",
931 taskid, scsiio_req->func);
932 return rc;
933 }
934
leapraid_init_cmd_priv(struct Scsi_Host * shost,struct scsi_cmnd * scmd)935 static int leapraid_init_cmd_priv(struct Scsi_Host *shost,
936 struct scsi_cmnd *scmd)
937 {
938 struct leapraid_adapter *adapter = shost_priv(shost);
939 struct leapraid_io_req_tracker *io_tracker;
940
941 io_tracker = scsi_cmd_priv(scmd);
942 leapraid_internal_init_cmd_priv(adapter, io_tracker);
943
944 return 0;
945 }
946
leapraid_exit_cmd_priv(struct Scsi_Host * shost,struct scsi_cmnd * scmd)947 static int leapraid_exit_cmd_priv(struct Scsi_Host *shost,
948 struct scsi_cmnd *scmd)
949 {
950 struct leapraid_adapter *adapter = shost_priv(shost);
951 struct leapraid_io_req_tracker *io_tracker;
952
953 io_tracker = scsi_cmd_priv(scmd);
954 leapraid_internal_exit_cmd_priv(adapter, io_tracker);
955
956 return 0;
957 }
958
leapraid_error_handler(struct scsi_cmnd * scmd,const char * str,u8 type)959 static int leapraid_error_handler(struct scsi_cmnd *scmd,
960 const char *str, u8 type)
961 {
962 struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
963 struct scsi_target *starget = scmd->device->sdev_target;
964 struct leapraid_starget_priv *starget_priv = starget->hostdata;
965 struct leapraid_io_req_tracker *io_req_tracker = NULL;
966 struct leapraid_sdev_priv *sdev_priv;
967 struct leapraid_sas_dev *sas_dev = NULL;
968 u16 hdl;
969 int rc;
970
971 dev_info(&adapter->pdev->dev,
972 "EH enter: type=%s, scmd=0x%p, req tag=%d\n", str, scmd,
973 scsi_cmd_to_rq(scmd)->tag);
974 scsi_print_command(scmd);
975
976 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
977 io_req_tracker = scsi_cmd_priv(scmd);
978 dev_info(&adapter->pdev->dev,
979 "EH ABORT: scmd=0x%p, pend=%ums, tout=%ums, tag=%d\n",
980 scmd,
981 jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
982 (scsi_cmd_to_rq(scmd)->timeout / HZ) * 1000,
983 scsi_cmd_to_rq(scmd)->tag);
984 }
985
986 if (leapraid_pci_removed(adapter) ||
987 adapter->access_ctrl.host_removing) {
988 dev_err(&adapter->pdev->dev,
989 "EH %s failed: %s scmd=0x%p\n", str,
990 (adapter->access_ctrl.host_removing ?
991 "shost removing!" : "pci_dev removed!"), scmd);
992 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK &&
993 io_req_tracker && io_req_tracker->taskid)
994 leapraid_free_taskid(adapter, io_req_tracker->taskid);
995 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
996 #ifdef FAST_IO_FAIL
997 rc = FAST_IO_FAIL;
998 #else
999 rc = FAILED;
1000 #endif
1001 goto out_eh_done;
1002 }
1003
1004 sdev_priv = scmd->device->hostdata;
1005 if (!sdev_priv || !sdev_priv->starget_priv) {
1006 dev_warn(&adapter->pdev->dev,
1007 "EH %s: SAS dev or starget gone, scmd=0x%p\n",
1008 str, scmd);
1009 scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
1010 scsi_done(scmd);
1011 rc = SUCCESS;
1012 goto out_eh_done;
1013 }
1014
1015 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1016 if (!io_req_tracker) {
1017 dev_warn(&adapter->pdev->dev,
1018 "EH ABORT: No I/O tracker, scmd 0x%p\n", scmd);
1019 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1020 rc = SUCCESS;
1021 goto out_eh_done;
1022 }
1023
1024 if (sdev_priv->starget_priv->flg &
1025 LEAPRAID_TGT_FLG_RAID_MEMBER ||
1026 sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1027 dev_err(&adapter->pdev->dev,
1028 "EH ABORT: Skip RAID/VOLUME, scmd=0x%p\n",
1029 scmd);
1030 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1031 rc = FAILED;
1032 goto out_eh_done;
1033 }
1034
1035 hdl = sdev_priv->starget_priv->hdl;
1036 } else {
1037 hdl = 0;
1038 if (sdev_priv->starget_priv->flg &
1039 LEAPRAID_TGT_FLG_RAID_MEMBER) {
1040 sas_dev = leapraid_get_sas_dev_from_tgt(adapter,
1041 starget_priv);
1042 if (sas_dev)
1043 hdl = sas_dev->volume_hdl;
1044 } else {
1045 hdl = sdev_priv->starget_priv->hdl;
1046 }
1047
1048 if (!hdl) {
1049 dev_err(&adapter->pdev->dev,
1050 "EH %s failed: Target handle 0, scmd=0x%p\n",
1051 str, scmd);
1052 scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1053 rc = FAILED;
1054 goto out_eh_done;
1055 }
1056 }
1057
1058 dev_info(&adapter->pdev->dev,
1059 "EH issue TM: type=%s, scmd=0x%p, hdl=0x%x\n",
1060 str, scmd, hdl);
1061
1062 rc = leapraid_issue_locked_tm(
1063 adapter,
1064 hdl,
1065 scmd->device->channel,
1066 scmd->device->id,
1067 (type == LEAPRAID_TM_TASKTYPE_TARGET_RESET ?
1068 0 : scmd->device->lun),
1069 type,
1070 (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ?
1071 io_req_tracker->taskid : 0),
1072 LEAPRAID_TM_MSGFLAGS_LINK_RESET);
1073
1074 out_eh_done:
1075 if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1076 if (rc != SUCCESS)
1077 dev_err(&adapter->pdev->dev,
1078 "EH ABORT result: failed, scmd=0x%p\n",
1079 scmd);
1080 } else {
1081 if (rc != SUCCESS)
1082 dev_err(&adapter->pdev->dev,
1083 "EH %s result: failed, scmd=0x%p\n",
1084 str, scmd);
1085 if (sas_dev)
1086 leapraid_sdev_put(sas_dev);
1087 }
1088 return rc;
1089 }
1090
leapraid_eh_abort_handler(struct scsi_cmnd * scmd)1091 static int leapraid_eh_abort_handler(struct scsi_cmnd *scmd)
1092 {
1093 return leapraid_error_handler(scmd, "ABORT TASK",
1094 LEAPRAID_TM_TASKTYPE_ABORT_TASK);
1095 }
1096
leapraid_eh_device_reset_handler(struct scsi_cmnd * scmd)1097 static int leapraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
1098 {
1099 return leapraid_error_handler(scmd, "UNIT RESET",
1100 LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET);
1101 }
1102
leapraid_eh_target_reset_handler(struct scsi_cmnd * scmd)1103 static int leapraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
1104 {
1105 return leapraid_error_handler(scmd, "TARGET RESET",
1106 LEAPRAID_TM_TASKTYPE_TARGET_RESET);
1107 }
1108
leapraid_eh_host_reset_handler(struct scsi_cmnd * scmd)1109 static int leapraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
1110 {
1111 struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
1112 int rc;
1113
1114 dev_info(&adapter->pdev->dev,
1115 "EH HOST RESET enter: scmd=%p, req tag=%d\n",
1116 scmd,
1117 scsi_cmd_to_rq(scmd)->tag);
1118 scsi_print_command(scmd);
1119
1120 if (adapter->scan_dev_desc.driver_loading ||
1121 adapter->access_ctrl.host_removing) {
1122 dev_err(&adapter->pdev->dev,
1123 "EH HOST RESET failed: %s scmd=0x%p\n",
1124 (adapter->access_ctrl.host_removing ?
1125 "shost removing!" : "driver loading!"), scmd);
1126 rc = FAILED;
1127 goto out_host_reset_done;
1128 }
1129
1130 dev_info(&adapter->pdev->dev, "%s:%d: Issuing hard reset\n",
1131 __func__, __LINE__);
1132 if (leapraid_hard_reset_handler(adapter, FULL_RESET) < 0)
1133 rc = FAILED;
1134 else
1135 rc = SUCCESS;
1136
1137 out_host_reset_done:
1138 if (rc != SUCCESS)
1139 dev_err(&adapter->pdev->dev,
1140 "EH HOST RESET result: failed, scmd=0x%p\n",
1141 scmd);
1142 return rc;
1143 }
1144
leapraid_sdev_init(struct scsi_device * sdev)1145 static int leapraid_sdev_init(struct scsi_device *sdev)
1146 {
1147 struct leapraid_raid_volume *raid_volume;
1148 struct leapraid_starget_priv *stgt_priv;
1149 struct leapraid_sdev_priv *sdev_priv;
1150 struct leapraid_adapter *adapter;
1151 struct leapraid_sas_dev *sas_dev;
1152 struct scsi_target *tgt;
1153 struct Scsi_Host *shost;
1154 unsigned long flags;
1155
1156 sdev_priv = kzalloc_obj(*sdev_priv);
1157 if (!sdev_priv)
1158 return -ENOMEM;
1159
1160 sdev_priv->lun = sdev->lun;
1161 sdev_priv->flg = LEAPRAID_DEVICE_FLG_INIT;
1162 tgt = scsi_target(sdev);
1163 stgt_priv = tgt->hostdata;
1164 stgt_priv->num_luns++;
1165 sdev_priv->starget_priv = stgt_priv;
1166 sdev->hostdata = sdev_priv;
1167 if (stgt_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
1168 sdev->no_uld_attach = LEAPRAID_NO_ULD_ATTACH;
1169
1170 shost = dev_to_shost(&tgt->dev);
1171 adapter = shost_priv(shost);
1172 if (tgt->channel == RAID_CHANNEL) {
1173 raid_volume = leapraid_raid_volume_find_by_id(adapter,
1174 tgt->id,
1175 tgt->channel);
1176 if (raid_volume) {
1177 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock,
1178 flags);
1179 raid_volume->sdev = sdev;
1180 spin_unlock_irqrestore(
1181 &adapter->dev_topo.raid_volume_lock, flags);
1182 leapraid_raid_volume_put(raid_volume);
1183 }
1184 }
1185
1186 if (!(stgt_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1187 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1188 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1189 adapter,
1190 stgt_priv->sas_address,
1191 stgt_priv->card_port);
1192 if (sas_dev && !sas_dev->starget) {
1193 sdev_printk(KERN_INFO, sdev,
1194 "%s: Assign starget to sas_dev\n",
1195 __func__);
1196 sas_dev->starget = tgt;
1197 }
1198
1199 if (sas_dev)
1200 leapraid_sdev_put(sas_dev);
1201 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1202 }
1203 return 0;
1204 }
1205
leapraid_slave_cfg_volume(struct scsi_device * sdev,struct queue_limits * lim)1206 static bool leapraid_slave_cfg_volume(struct scsi_device *sdev,
1207 struct queue_limits *lim)
1208 {
1209 struct Scsi_Host *shost = sdev->host;
1210 struct leapraid_adapter *adapter = shost_priv(shost);
1211 struct leapraid_raid_volume *raid_volume;
1212 struct leapraid_starget_priv *starget_priv;
1213 struct leapraid_sdev_priv *sdev_priv;
1214 int qd;
1215 u16 hdl;
1216
1217 sdev_priv = sdev->hostdata;
1218 starget_priv = sdev_priv->starget_priv;
1219 hdl = starget_priv->hdl;
1220
1221 raid_volume = leapraid_raid_volume_find_by_hdl(adapter, hdl);
1222 if (!raid_volume) {
1223 sdev_printk(KERN_WARNING, sdev,
1224 "%s: RAID volume not found, hdl=0x%x\n",
1225 __func__, hdl);
1226 return 1;
1227 }
1228
1229 if (leapraid_get_volume_cap(adapter, raid_volume)) {
1230 sdev_printk(KERN_ERR, sdev,
1231 "%s: Failed to get volume cap, hdl=0x%x\n",
1232 __func__, hdl);
1233 leapraid_raid_volume_put(raid_volume);
1234 return 1;
1235 }
1236
1237 qd = (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT) ?
1238 adapter->adapter_attr.narrowport_max_queue_depth :
1239 adapter->adapter_attr.sata_max_queue_depth;
1240 if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1241 qd = adapter->adapter_attr.raid_volume_max_queue_depth;
1242
1243 sdev_printk(KERN_INFO, sdev,
1244 "RAID volume: hdl=0x%04x, wwid=0x%016llx\n",
1245 raid_volume->hdl, (unsigned long long)raid_volume->wwid);
1246
1247 if (shost->max_sectors > LEAPRAID_MAX_SECTORS)
1248 lim->max_hw_sectors = LEAPRAID_MAX_SECTORS;
1249
1250 leapraid_change_queue_depth(sdev, qd);
1251 leapraid_raid_volume_put(raid_volume);
1252 return 0;
1253 }
1254
leapraid_slave_configure_extra(struct scsi_device * sdev,struct leapraid_sas_dev ** psas_dev,u16 vol_hdl,u64 volume_wwid,bool * is_target_ssp,int * qd)1255 static bool leapraid_slave_configure_extra(struct scsi_device *sdev,
1256 struct leapraid_sas_dev **psas_dev,
1257 u16 vol_hdl, u64 volume_wwid,
1258 bool *is_target_ssp, int *qd)
1259 {
1260 struct leapraid_sas_dev *sas_dev;
1261 struct leapraid_sdev_priv *sdev_priv;
1262 struct Scsi_Host *shost = sdev->host;
1263 struct leapraid_adapter *adapter = shost_priv(shost);
1264 unsigned long flags;
1265
1266 sdev_priv = sdev->hostdata;
1267 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1268 *is_target_ssp = false;
1269 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1270 adapter,
1271 sdev_priv->starget_priv->sas_address,
1272 sdev_priv->starget_priv->card_port);
1273 if (!sas_dev) {
1274 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1275 sdev_printk(KERN_WARNING, sdev,
1276 "%s: SAS dev not found, sas=0x%llx\n",
1277 __func__, sdev_priv->starget_priv->sas_address);
1278 return 1;
1279 }
1280
1281 *psas_dev = sas_dev;
1282 sas_dev->volume_hdl = vol_hdl;
1283 sas_dev->volume_wwid = volume_wwid;
1284 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1285 *qd = (sas_dev->port_connection > 1) ?
1286 adapter->adapter_attr.wideport_max_queue_depth :
1287 adapter->adapter_attr.narrowport_max_queue_depth;
1288 *is_target_ssp = true;
1289 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SEP)
1290 sdev_priv->sep = 1;
1291 } else {
1292 *qd = adapter->adapter_attr.sata_max_queue_depth;
1293 }
1294
1295 sdev_printk(KERN_INFO, sdev,
1296 "device name=0x%016llx, SAS addr=0x%016llx\n",
1297 (unsigned long long)sas_dev->dev_name,
1298 (unsigned long long)sas_dev->sas_addr);
1299 leapraid_sdev_put(sas_dev);
1300 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1301 return 0;
1302 }
1303
leapraid_sdev_configure(struct scsi_device * sdev,struct queue_limits * lim)1304 static int leapraid_sdev_configure(struct scsi_device *sdev,
1305 struct queue_limits *lim)
1306 {
1307 struct leapraid_sas_dev *sas_dev;
1308 struct leapraid_sdev_priv *sdev_priv;
1309 struct Scsi_Host *shost = sdev->host;
1310 struct leapraid_starget_priv *starget_priv;
1311 struct leapraid_adapter *adapter;
1312 u16 hdl, vol_hdl = 0;
1313 bool is_target_ssp = false;
1314 u64 volume_wwid = 0;
1315 int qd = 1;
1316
1317 adapter = shost_priv(shost);
1318 sdev_priv = sdev->hostdata;
1319 sdev_priv->flg &= ~LEAPRAID_DEVICE_FLG_INIT;
1320 starget_priv = sdev_priv->starget_priv;
1321 hdl = starget_priv->hdl;
1322 if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)
1323 return leapraid_slave_cfg_volume(sdev, lim);
1324
1325 if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) {
1326 if (leapraid_cfg_get_volume_hdl(adapter, hdl, &vol_hdl)) {
1327 sdev_printk(KERN_WARNING, sdev,
1328 "%s: Get volume hdl failed, hdl=0x%x\n",
1329 __func__, hdl);
1330 return 1;
1331 }
1332
1333 if (vol_hdl && leapraid_cfg_get_volume_wwid(adapter, vol_hdl,
1334 &volume_wwid)) {
1335 sdev_printk(KERN_WARNING, sdev,
1336 "%s: Get wwid failed, volume_hdl=0x%x\n",
1337 __func__, vol_hdl);
1338 return 1;
1339 }
1340 }
1341
1342 if (leapraid_slave_configure_extra(sdev, &sas_dev, vol_hdl,
1343 volume_wwid, &is_target_ssp, &qd)) {
1344 sdev_printk(KERN_WARNING, sdev,
1345 "%s: slave_configure_extra failed\n", __func__);
1346 return 1;
1347 }
1348
1349 leapraid_change_queue_depth(sdev, qd);
1350 if (is_target_ssp)
1351 sas_read_port_mode_page(sdev);
1352
1353 return 0;
1354 }
1355
leapraid_sdev_destroy(struct scsi_device * sdev)1356 static void leapraid_sdev_destroy(struct scsi_device *sdev)
1357 {
1358 struct leapraid_adapter *adapter;
1359 struct Scsi_Host *shost;
1360 struct leapraid_sas_dev *sas_dev;
1361 struct leapraid_starget_priv *starget_priv;
1362 struct scsi_target *stgt;
1363 unsigned long flags;
1364
1365 if (!sdev->hostdata)
1366 return;
1367
1368 stgt = scsi_target(sdev);
1369 starget_priv = stgt->hostdata;
1370 starget_priv->num_luns--;
1371 shost = dev_to_shost(&stgt->dev);
1372 adapter = shost_priv(shost);
1373 if (!(starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1374 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1375 sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1376 starget_priv);
1377 if (sas_dev && !starget_priv->num_luns)
1378 sas_dev->starget = NULL;
1379 if (sas_dev)
1380 leapraid_sdev_put(sas_dev);
1381 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1382 }
1383
1384 kfree(sdev->hostdata);
1385 sdev->hostdata = NULL;
1386 }
1387
leapraid_target_alloc_raid(struct scsi_target * tgt)1388 static int leapraid_target_alloc_raid(struct scsi_target *tgt)
1389 {
1390 struct leapraid_starget_priv *starget_priv;
1391 struct leapraid_raid_volume *raid_volume;
1392 struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1393 struct leapraid_adapter *adapter = shost_priv(shost);
1394 unsigned long flags;
1395
1396 starget_priv = tgt->hostdata;
1397 raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1398 tgt->channel);
1399 if (raid_volume) {
1400 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1401 starget_priv->hdl = raid_volume->hdl;
1402 starget_priv->sas_address = raid_volume->wwid;
1403 starget_priv->flg |= LEAPRAID_TGT_FLG_VOLUME;
1404 raid_volume->starget = tgt;
1405 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1406 flags);
1407 leapraid_raid_volume_put(raid_volume);
1408 }
1409 return 0;
1410 }
1411
leapraid_target_alloc_sas(struct scsi_target * tgt)1412 static int leapraid_target_alloc_sas(struct scsi_target *tgt)
1413 {
1414 struct sas_rphy *rphy;
1415 struct Scsi_Host *shost;
1416 struct leapraid_sas_dev *sas_dev;
1417 struct leapraid_adapter *adapter;
1418 struct leapraid_starget_priv *starget_priv;
1419 unsigned long flags;
1420
1421 shost = dev_to_shost(&tgt->dev);
1422 adapter = shost_priv(shost);
1423 starget_priv = tgt->hostdata;
1424 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1425 rphy = dev_to_rphy(tgt->dev.parent);
1426 sas_dev = leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
1427 adapter,
1428 rphy->identify.sas_address,
1429 rphy);
1430 if (sas_dev) {
1431 starget_priv->sas_dev = sas_dev;
1432 starget_priv->card_port = sas_dev->card_port;
1433 starget_priv->sas_address = sas_dev->sas_addr;
1434 starget_priv->hdl = sas_dev->hdl;
1435 sas_dev->channel = tgt->channel;
1436 sas_dev->id = tgt->id;
1437 sas_dev->starget = tgt;
1438 if (sas_dev->hdl &&
1439 sas_dev->hdl <=
1440 adapter->adapter_attr.features.max_dev_handle &&
1441 test_bit(sas_dev->hdl, adapter->dev_topo.pd_hdls))
1442 starget_priv->flg |= LEAPRAID_TGT_FLG_RAID_MEMBER;
1443 }
1444 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1445
1446 return 0;
1447 }
1448
leapraid_target_alloc(struct scsi_target * tgt)1449 static int leapraid_target_alloc(struct scsi_target *tgt)
1450 {
1451 struct leapraid_starget_priv *starget_priv;
1452
1453 starget_priv = kzalloc_obj(*starget_priv);
1454 if (!starget_priv)
1455 return -ENOMEM;
1456
1457 tgt->hostdata = starget_priv;
1458 starget_priv->starget = tgt;
1459 starget_priv->hdl = LEAPRAID_INVALID_DEV_HANDLE;
1460 if (tgt->channel == RAID_CHANNEL)
1461 return leapraid_target_alloc_raid(tgt);
1462
1463 return leapraid_target_alloc_sas(tgt);
1464 }
1465
leapraid_target_destroy_raid(struct scsi_target * tgt)1466 static void leapraid_target_destroy_raid(struct scsi_target *tgt)
1467 {
1468 struct leapraid_raid_volume *raid_volume;
1469 struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1470 struct leapraid_adapter *adapter = shost_priv(shost);
1471 unsigned long flags;
1472
1473 raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1474 tgt->channel);
1475 if (raid_volume) {
1476 spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1477 raid_volume->starget = NULL;
1478 raid_volume->sdev = NULL;
1479 spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1480 flags);
1481 leapraid_raid_volume_put(raid_volume);
1482 }
1483 }
1484
leapraid_target_destroy_sas(struct scsi_target * tgt)1485 static void leapraid_target_destroy_sas(struct scsi_target *tgt)
1486 {
1487 struct leapraid_adapter *adapter;
1488 struct leapraid_sas_dev *sas_dev;
1489 struct leapraid_starget_priv *starget_priv;
1490 struct Scsi_Host *shost;
1491 unsigned long flags;
1492
1493 shost = dev_to_shost(&tgt->dev);
1494 adapter = shost_priv(shost);
1495 starget_priv = tgt->hostdata;
1496
1497 spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1498 sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1499 starget_priv);
1500 if (sas_dev &&
1501 sas_dev->starget == tgt &&
1502 sas_dev->id == tgt->id &&
1503 sas_dev->channel == tgt->channel)
1504 sas_dev->starget = NULL;
1505
1506 if (sas_dev) {
1507 starget_priv->sas_dev = NULL;
1508 leapraid_sdev_put(sas_dev);
1509 leapraid_sdev_put(sas_dev);
1510 }
1511 spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1512 }
1513
leapraid_target_destroy(struct scsi_target * tgt)1514 static void leapraid_target_destroy(struct scsi_target *tgt)
1515 {
1516 struct leapraid_starget_priv *starget_priv;
1517
1518 starget_priv = tgt->hostdata;
1519 if (!starget_priv)
1520 return;
1521
1522 if (tgt->channel == RAID_CHANNEL) {
1523 leapraid_target_destroy_raid(tgt);
1524 goto out_free;
1525 }
1526
1527 leapraid_target_destroy_sas(tgt);
1528
1529 out_free:
1530 kfree(starget_priv);
1531 tgt->hostdata = NULL;
1532 }
1533
leapraid_scan_check_status(struct leapraid_adapter * adapter,bool * need_hard_reset)1534 static bool leapraid_scan_check_status(struct leapraid_adapter *adapter,
1535 bool *need_hard_reset)
1536 {
1537 u32 adapter_state;
1538
1539 if (adapter->scan_dev_desc.scan_start) {
1540 adapter_state = leapraid_get_adapter_state(adapter);
1541 if (adapter_state == LEAPRAID_DB_FAULT) {
1542 *need_hard_reset = true;
1543 return true;
1544 }
1545 return false;
1546 }
1547
1548 if (adapter->driver_cmds.scan_dev_cmd.status & LEAPRAID_CMD_RESET) {
1549 dev_err(&adapter->pdev->dev,
1550 "Device scan: Aborted due to reset\n");
1551 adapter->driver_cmds.scan_dev_cmd.status =
1552 LEAPRAID_CMD_NOT_USED;
1553 adapter->scan_dev_desc.driver_loading = 0;
1554 wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1555 return true;
1556 }
1557
1558 if (adapter->scan_dev_desc.scan_start_failed) {
1559 dev_err(&adapter->pdev->dev,
1560 "Device scan: Failed with adapter_status=0x%08x\n",
1561 adapter->scan_dev_desc.scan_start_failed);
1562 adapter->scan_dev_desc.driver_loading = 0;
1563 wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1564 adapter->scan_dev_desc.wait_scan_dev_done = 0;
1565 adapter->access_ctrl.host_removing = 1;
1566 return true;
1567 }
1568
1569 adapter->driver_cmds.scan_dev_cmd.status = LEAPRAID_CMD_NOT_USED;
1570 leapraid_scan_dev_done(adapter);
1571 return true;
1572 }
1573
leapraid_scan_finished(struct Scsi_Host * shost,unsigned long time)1574 static int leapraid_scan_finished(struct Scsi_Host *shost, unsigned long time)
1575 {
1576 struct leapraid_adapter *adapter = shost_priv(shost);
1577 bool need_hard_reset = false;
1578
1579 if (time >= (LEAPRAID_SCAN_DEV_CMD_TIMEOUT * HZ)) {
1580 adapter->driver_cmds.scan_dev_cmd.status =
1581 LEAPRAID_CMD_NOT_USED;
1582 dev_err(&adapter->pdev->dev,
1583 "Device scan: Failed with timeout 300s\n");
1584 adapter->scan_dev_desc.driver_loading = 0;
1585 wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1586 return 1;
1587 }
1588
1589 if (!leapraid_scan_check_status(adapter, &need_hard_reset))
1590 return 0;
1591
1592 if (need_hard_reset) {
1593 adapter->driver_cmds.scan_dev_cmd.status =
1594 LEAPRAID_CMD_NOT_USED;
1595 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
1596 __func__, __LINE__);
1597 if (leapraid_hard_reset_handler(adapter, PART_RESET)) {
1598 adapter->scan_dev_desc.driver_loading = 0;
1599 wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1600 }
1601 }
1602
1603 return 1;
1604 }
1605
leapraid_scan_start(struct Scsi_Host * shost)1606 static void leapraid_scan_start(struct Scsi_Host *shost)
1607 {
1608 struct leapraid_adapter *adapter = shost_priv(shost);
1609
1610 adapter->scan_dev_desc.scan_start = 1;
1611 leapraid_scan_dev(adapter, true);
1612 }
1613
leapraid_get_raid_qd(struct leapraid_adapter * adapter,const struct leapraid_raid_volume * raid_volume,bool default_qd)1614 static u32 leapraid_get_raid_qd(struct leapraid_adapter *adapter,
1615 const struct leapraid_raid_volume *raid_volume,
1616 bool default_qd)
1617 {
1618 const struct leapraid_adapter_attr *attr = &adapter->adapter_attr;
1619
1620 if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1621 return default_qd ? LEAPRAID_RAID_QUEUE_DEPTH :
1622 attr->raid_volume_max_queue_depth;
1623
1624 if (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT)
1625 return default_qd ? LEAPRAID_SAS_QUEUE_DEPTH :
1626 attr->narrowport_max_queue_depth;
1627
1628 return default_qd ? LEAPRAID_SATA_QUEUE_DEPTH :
1629 attr->sata_max_queue_depth;
1630 }
1631
leapraid_calc_max_queue_depth(struct scsi_device * sdev,int qdepth)1632 static int leapraid_calc_max_queue_depth(struct scsi_device *sdev, int qdepth)
1633 {
1634 struct Scsi_Host *shost;
1635 struct leapraid_adapter *adapter;
1636 struct leapraid_starget_priv *starget_priv;
1637 struct leapraid_sdev_priv *sdev_priv;
1638 struct leapraid_raid_volume *raid_volume;
1639 struct leapraid_sas_dev *sas_dev;
1640 int max_depth;
1641 u32 default_qdepth = 0;
1642 u32 fw_qdepth = 0;
1643
1644 shost = sdev->host;
1645 adapter = shost_priv(shost);
1646 max_depth = shost->can_queue;
1647
1648 sdev_priv = sdev->hostdata;
1649 if (!sdev_priv)
1650 goto out_tag_check;
1651
1652 starget_priv = sdev_priv->starget_priv;
1653 if (!starget_priv)
1654 goto out_tag_check;
1655
1656 if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1657 raid_volume = leapraid_raid_volume_find_by_hdl(
1658 adapter, starget_priv->hdl);
1659 if (raid_volume) {
1660 default_qdepth = leapraid_get_raid_qd(
1661 adapter, raid_volume, true);
1662 fw_qdepth = leapraid_get_raid_qd(
1663 adapter, raid_volume, false);
1664 leapraid_raid_volume_put(raid_volume);
1665 }
1666 goto out_limit_check;
1667 }
1668
1669 sas_dev = leapraid_get_sas_dev_from_tgt(adapter, starget_priv);
1670 if (sas_dev) {
1671 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1672 default_qdepth = LEAPRAID_SAS_QUEUE_DEPTH;
1673 fw_qdepth = (sas_dev->port_connection > 1) ?
1674 adapter->adapter_attr.wideport_max_queue_depth :
1675 adapter->adapter_attr.narrowport_max_queue_depth;
1676 }
1677 if (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV) {
1678 default_qdepth = LEAPRAID_SATA_QUEUE_DEPTH;
1679 fw_qdepth = adapter->adapter_attr.sata_max_queue_depth;
1680 }
1681 leapraid_sdev_put(sas_dev);
1682 }
1683
1684 out_limit_check:
1685 if (fw_qdepth > shost->can_queue && default_qdepth)
1686 fw_qdepth = default_qdepth;
1687
1688 if (fw_qdepth)
1689 max_depth = min_t(int, max_depth, fw_qdepth);
1690
1691 out_tag_check:
1692 if (!sdev->tagged_supported)
1693 max_depth = 1;
1694
1695 if (qdepth > max_depth)
1696 qdepth = max_depth;
1697
1698 return qdepth;
1699 }
1700
leapraid_change_queue_depth(struct scsi_device * sdev,int qdepth)1701 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
1702 {
1703 qdepth = leapraid_calc_max_queue_depth(sdev, qdepth);
1704 scsi_change_queue_depth(sdev, qdepth);
1705 return sdev->queue_depth;
1706 }
1707
leapraid_map_queues(struct Scsi_Host * shost)1708 static void leapraid_map_queues(struct Scsi_Host *shost)
1709 {
1710 struct leapraid_adapter *adapter;
1711 struct blk_mq_queue_map *queue_map;
1712 int msix_queue_count;
1713 int poll_queue_count;
1714 int queue_offset;
1715 int map_index;
1716
1717 adapter = (struct leapraid_adapter *)shost->hostdata;
1718 if (shost->nr_hw_queues == 1)
1719 return;
1720
1721 msix_queue_count = adapter->notification_desc.iopoll_qdex;
1722 poll_queue_count = adapter->adapter_attr.rq_cnt - msix_queue_count;
1723
1724 queue_offset = 0;
1725 for (map_index = 0; map_index < shost->nr_maps; map_index++) {
1726 queue_map = &shost->tag_set.map[map_index];
1727 queue_map->nr_queues = 0;
1728
1729 switch (map_index) {
1730 case HCTX_TYPE_DEFAULT:
1731 queue_map->nr_queues = msix_queue_count;
1732 queue_map->queue_offset = queue_offset;
1733 WARN_ON_ONCE(!queue_map->nr_queues);
1734 blk_mq_map_hw_queues(queue_map,
1735 &adapter->pdev->dev, 0);
1736 break;
1737 case HCTX_TYPE_POLL:
1738 queue_map->nr_queues = poll_queue_count;
1739 queue_map->queue_offset = queue_offset;
1740 blk_mq_map_queues(queue_map);
1741 break;
1742 default:
1743 queue_map->queue_offset = queue_offset;
1744 blk_mq_map_hw_queues(queue_map,
1745 &adapter->pdev->dev, 0);
1746 break;
1747 }
1748 queue_offset += queue_map->nr_queues;
1749 }
1750 }
1751
leapraid_blk_mq_poll(struct Scsi_Host * shost,unsigned int queue_num)1752 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
1753 {
1754 struct leapraid_adapter *adapter =
1755 (struct leapraid_adapter *)shost->hostdata;
1756 struct leapraid_blk_mq_poll_rq *blk_mq_poll_rq;
1757 int num_entries;
1758 int qid = queue_num - adapter->notification_desc.iopoll_qdex;
1759
1760 blk_mq_poll_rq = &adapter->notification_desc.blk_mq_poll_rqs[qid];
1761 if (atomic_read(&blk_mq_poll_rq->pause) ||
1762 !atomic_add_unless(&blk_mq_poll_rq->busy, 1, 1))
1763 return 0;
1764
1765 num_entries = leapraid_rep_queue_handler(&blk_mq_poll_rq->rq);
1766 atomic_dec(&blk_mq_poll_rq->busy);
1767 return num_entries;
1768 }
1769
leapraid_bios_param(struct scsi_device * sdev,struct gendisk * disk,sector_t capacity,int geom[])1770 static int leapraid_bios_param(struct scsi_device *sdev,
1771 struct gendisk *disk, sector_t capacity,
1772 int geom[])
1773 {
1774 int heads;
1775 int sectors;
1776 sector_t cylinders;
1777
1778 if (scsi_partsize(disk, capacity, geom))
1779 return 0;
1780
1781 if ((ulong)capacity >= LEAPRAID_LARGE_DISK_THRESHOLD) {
1782 heads = LEAPRAID_LARGE_DISK_HEADS;
1783 sectors = LEAPRAID_LARGE_DISK_SECTORS;
1784 } else {
1785 heads = LEAPRAID_SMALL_DISK_HEADS;
1786 sectors = LEAPRAID_SMALL_DISK_SECTORS;
1787 }
1788
1789 cylinders = capacity;
1790 sector_div(cylinders, heads * sectors);
1791
1792 geom[0] = heads;
1793 geom[1] = sectors;
1794 geom[2] = cylinders;
1795 return 0;
1796 }
1797
fw_queue_depth_show(struct device * cdev,struct device_attribute * attr,char * buf)1798 static ssize_t fw_queue_depth_show(struct device *cdev,
1799 struct device_attribute *attr,
1800 char *buf)
1801 {
1802 struct Scsi_Host *shost = class_to_shost(cdev);
1803 struct leapraid_adapter *adapter = shost_priv(shost);
1804
1805 return sysfs_emit(buf, "%02d\n",
1806 adapter->adapter_attr.features.req_slot);
1807 }
1808
host_sas_address_show(struct device * cdev,struct device_attribute * attr,char * buf)1809 static ssize_t host_sas_address_show(struct device *cdev,
1810 struct device_attribute *attr, char *buf)
1811 {
1812 struct Scsi_Host *shost = class_to_shost(cdev);
1813 struct leapraid_adapter *adapter = shost_priv(shost);
1814
1815 return sysfs_emit(buf, "0x%016llx\n",
1816 (unsigned long long)adapter->dev_topo.card.sas_address);
1817 }
1818
board_name_show(struct device * cdev,struct device_attribute * attr,char * buf)1819 static ssize_t board_name_show(struct device *cdev,
1820 struct device_attribute *attr, char *buf)
1821 {
1822 struct Scsi_Host *shost = class_to_shost(cdev);
1823 struct leapraid_adapter *adapter = shost_priv(shost);
1824
1825 return sysfs_emit(buf, "%s\n", adapter->adapter_attr.board_name);
1826 }
1827
1828 static DEVICE_ATTR_RO(fw_queue_depth);
1829 static DEVICE_ATTR_RO(host_sas_address);
1830 static DEVICE_ATTR_RO(board_name);
1831
1832 static struct attribute *leapraid_shost_attrs[] = {
1833 &dev_attr_fw_queue_depth.attr,
1834 &dev_attr_host_sas_address.attr,
1835 &dev_attr_board_name.attr,
1836 NULL,
1837 };
1838
1839 ATTRIBUTE_GROUPS(leapraid_shost);
1840
sas_device_handle_show(struct device * dev,struct device_attribute * attr,char * buf)1841 static ssize_t sas_device_handle_show(struct device *dev,
1842 struct device_attribute *attr, char *buf)
1843 {
1844 struct scsi_device *sdev = to_scsi_device(dev);
1845 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1846
1847 if (!sas_device_priv_data || !sas_device_priv_data->starget_priv) {
1848 dev_err(&sdev->sdev_gendev,
1849 "%s: Invalid sdev_priv or starget_priv\n", __func__);
1850 return -EINVAL;
1851 }
1852
1853 return sysfs_emit(buf, "0x%04x\n",
1854 sas_device_priv_data->starget_priv->hdl);
1855 }
1856
sas_ncq_prio_supported_show(struct device * dev,struct device_attribute * attr,char * buf)1857 static ssize_t sas_ncq_prio_supported_show(struct device *dev,
1858 struct device_attribute *attr,
1859 char *buf)
1860 {
1861 struct scsi_device *sdev = to_scsi_device(dev);
1862
1863 return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev));
1864 }
1865
sas_ncq_prio_enable_show(struct device * dev,struct device_attribute * attr,char * buf)1866 static ssize_t sas_ncq_prio_enable_show(struct device *dev,
1867 struct device_attribute *attr,
1868 char *buf)
1869 {
1870 struct scsi_device *sdev = to_scsi_device(dev);
1871 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1872
1873 if (!sas_device_priv_data) {
1874 dev_err(&sdev->sdev_gendev,
1875 "%s: Invalid sdev_priv\n", __func__);
1876 return -EINVAL;
1877 }
1878
1879 return sysfs_emit(buf, "%d\n", sas_device_priv_data->ncq_prio_enable);
1880 }
1881
sas_ncq_prio_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1882 static ssize_t sas_ncq_prio_enable_store(struct device *dev,
1883 struct device_attribute *attr,
1884 const char *buf, size_t count)
1885 {
1886 struct scsi_device *sdev = to_scsi_device(dev);
1887 struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1888 bool enable;
1889
1890 if (!sas_device_priv_data) {
1891 dev_err(&sdev->sdev_gendev,
1892 "%s: Invalid sdev_priv\n", __func__);
1893 return -EINVAL;
1894 }
1895
1896 if (kstrtobool(buf, &enable))
1897 return -EINVAL;
1898
1899 if (!sas_ata_ncq_prio_supported(sdev))
1900 return -EINVAL;
1901
1902 sas_device_priv_data->ncq_prio_enable = enable;
1903 return count;
1904 }
1905
1906 static DEVICE_ATTR_RO(sas_device_handle);
1907 static DEVICE_ATTR_RO(sas_ncq_prio_supported);
1908 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
1909
leapraid_sdev_is_sata(struct scsi_device * sdev)1910 static bool leapraid_sdev_is_sata(struct scsi_device *sdev)
1911 {
1912 struct scsi_target *starget = sdev->sdev_target;
1913 struct leapraid_starget_priv *starget_priv = starget->hostdata;
1914 struct leapraid_sas_dev *sas_dev;
1915
1916 if (!starget_priv)
1917 return false;
1918
1919 sas_dev = starget_priv->sas_dev;
1920 return sas_dev && (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV);
1921 }
1922
1923 static struct attribute *leapraid_sdev_attrs[] = {
1924 &dev_attr_sas_device_handle.attr,
1925 &dev_attr_sas_ncq_prio_supported.attr,
1926 &dev_attr_sas_ncq_prio_enable.attr,
1927 NULL,
1928 };
1929
leapraid_sdev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int i)1930 static umode_t leapraid_sdev_attr_is_visible(struct kobject *kobj,
1931 struct attribute *attr, int i)
1932 {
1933 struct device *dev = kobj_to_dev(kobj);
1934 struct scsi_device *sdev = to_scsi_device(dev);
1935
1936 if (attr == &dev_attr_sas_ncq_prio_supported.attr ||
1937 attr == &dev_attr_sas_ncq_prio_enable.attr)
1938 if (!leapraid_sdev_is_sata(sdev))
1939 return 0;
1940
1941 return attr->mode;
1942 }
1943
1944 static const struct attribute_group leapraid_sdev_attr_group = {
1945 .attrs = leapraid_sdev_attrs,
1946 .is_visible = leapraid_sdev_attr_is_visible,
1947 };
1948
1949 static const struct attribute_group *leapraid_sdev_groups[] = {
1950 &leapraid_sdev_attr_group,
1951 NULL,
1952 };
1953
1954 static struct scsi_host_template leapraid_driver_template = {
1955 .module = THIS_MODULE,
1956 .name = "LEAPIO RAID Host",
1957 .proc_name = LEAPRAID_DRIVER_NAME,
1958 .queuecommand = leapraid_queuecommand,
1959 .cmd_size = sizeof(struct leapraid_io_req_tracker),
1960 .init_cmd_priv = leapraid_init_cmd_priv,
1961 .exit_cmd_priv = leapraid_exit_cmd_priv,
1962 .eh_abort_handler = leapraid_eh_abort_handler,
1963 .eh_device_reset_handler = leapraid_eh_device_reset_handler,
1964 .eh_target_reset_handler = leapraid_eh_target_reset_handler,
1965 .eh_host_reset_handler = leapraid_eh_host_reset_handler,
1966 .sdev_init = leapraid_sdev_init,
1967 .sdev_destroy = leapraid_sdev_destroy,
1968 .sdev_configure = leapraid_sdev_configure,
1969 .target_alloc = leapraid_target_alloc,
1970 .target_destroy = leapraid_target_destroy,
1971 .scan_finished = leapraid_scan_finished,
1972 .scan_start = leapraid_scan_start,
1973 .change_queue_depth = leapraid_change_queue_depth,
1974 .map_queues = leapraid_map_queues,
1975 .mq_poll = leapraid_blk_mq_poll,
1976 .bios_param = leapraid_bios_param,
1977 .can_queue = LEAPRAID_CAN_QUEUE_MIN,
1978 .this_id = LEAPRAID_THIS_ID_NONE,
1979 .sg_tablesize = LEAPRAID_SG_DEPTH,
1980 .max_sectors = LEAPRAID_MAX_SECTORS,
1981 .max_segment_size = LEAPRAID_MAX_SEGMENT_SIZE,
1982 .cmd_per_lun = LEAPRAID_CMD_PER_LUN,
1983 .shost_groups = leapraid_shost_groups,
1984 .sdev_groups = leapraid_sdev_groups,
1985 .track_queue_depth = 1,
1986 };
1987
leapraid_lock_init(struct leapraid_adapter * adapter)1988 static void leapraid_lock_init(struct leapraid_adapter *adapter)
1989 {
1990 mutex_init(&adapter->reset_desc.adapter_reset_mutex);
1991 mutex_init(&adapter->reset_desc.host_diag_mutex);
1992 mutex_init(&adapter->access_ctrl.pci_access_lock);
1993
1994 spin_lock_init(&adapter->reset_desc.adapter_reset_lock);
1995 spin_lock_init(&adapter->dynamic_task_desc.task_lock);
1996 spin_lock_init(&adapter->dev_topo.sas_dev_lock);
1997 spin_lock_init(&adapter->dev_topo.topo_node_lock);
1998 spin_lock_init(&adapter->fw_evt_s.fw_evt_lock);
1999 spin_lock_init(&adapter->dev_topo.raid_volume_lock);
2000 spin_lock_init(&adapter->dev_topo.enc_lock);
2001 spin_lock_init(&adapter->boot_devs.lock);
2002 }
2003
leapraid_list_init(struct leapraid_adapter * adapter)2004 static void leapraid_list_init(struct leapraid_adapter *adapter)
2005 {
2006 INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_list);
2007 INIT_LIST_HEAD(&adapter->dev_topo.card_port_list);
2008 INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_init_list);
2009 INIT_LIST_HEAD(&adapter->dev_topo.exp_list);
2010 INIT_LIST_HEAD(&adapter->dev_topo.enc_list);
2011 INIT_LIST_HEAD(&adapter->fw_evt_s.fw_evt_list);
2012 INIT_LIST_HEAD(&adapter->dev_topo.raid_volume_list);
2013 INIT_LIST_HEAD(&adapter->dev_topo.card.sas_port_list);
2014 }
2015
leapraid_probe(struct pci_dev * pdev,const struct pci_device_id * id)2016 static int leapraid_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2017 {
2018 struct leapraid_adapter *adapter;
2019 struct Scsi_Host *shost;
2020 int iopoll_q_count;
2021 int rc;
2022
2023 shost = scsi_host_alloc(&leapraid_driver_template,
2024 sizeof(struct leapraid_adapter));
2025 if (!shost) {
2026 dev_err(&pdev->dev,
2027 "%s: SCSI host alloc failed\n", __func__);
2028 return -ENODEV;
2029 }
2030
2031 adapter = shost_priv(shost);
2032 memset(adapter, 0, sizeof(struct leapraid_adapter));
2033
2034 init_waitqueue_head(&adapter->access_ctrl.recovery_waitq);
2035 adapter->adapter_attr.id = atomic_inc_return(&leapraid_ids) - 1;
2036
2037 adapter->adapter_attr.enable_mp = enable_mp;
2038
2039 adapter = shost_priv(shost);
2040 INIT_LIST_HEAD(&adapter->list);
2041
2042 adapter->shost = shost;
2043 adapter->pdev = pdev;
2044 adapter->fw_log_desc.open_pcie_trace = open_pcie_trace;
2045 atomic_set(&adapter->fw_log_desc.mmap_refcnt, 0);
2046 init_waitqueue_head(&adapter->fw_log_desc.mmap_waitq);
2047 leapraid_lock_init(adapter);
2048 leapraid_list_init(adapter);
2049 snprintf(adapter->adapter_attr.name, LEAPRAID_NAME_LENGTH, "%s%d",
2050 LEAPRAID_DRIVER_NAME, adapter->adapter_attr.id);
2051
2052 shost->max_cmd_len = LEAPRAID_MAX_CDB_LEN;
2053 shost->max_lun = LEAPRAID_MAX_LUNS;
2054 shost->transportt = leapraid_transport_template;
2055 shost->unique_id = adapter->adapter_attr.id;
2056
2057 snprintf(adapter->fw_evt_s.fw_evt_name,
2058 sizeof(adapter->fw_evt_s.fw_evt_name),
2059 "fw_event_%s%d", LEAPRAID_DRIVER_NAME,
2060 adapter->adapter_attr.id);
2061 adapter->fw_evt_s.fw_evt_thread =
2062 alloc_ordered_workqueue(adapter->fw_evt_s.fw_evt_name, 0);
2063 if (!adapter->fw_evt_s.fw_evt_thread) {
2064 dev_err(&adapter->pdev->dev,
2065 "%s: Failed to create fw event workqueue\n", __func__);
2066 rc = -ENODEV;
2067 goto evt_wq_fail;
2068 }
2069
2070 shost->host_tagset = 1;
2071 init_waitqueue_head(&adapter->scan_dev_desc.wait_driver_loading);
2072 adapter->scan_dev_desc.driver_loading = 1;
2073 if (leapraid_ctrl_init(adapter)) {
2074 dev_err(&adapter->pdev->dev,
2075 "%s: Adapter init failed\n", __func__);
2076 rc = -ENODEV;
2077 goto ctrl_init_fail;
2078 }
2079
2080 shost->nr_hw_queues = 1;
2081 if (shost->host_tagset) {
2082 shost->nr_hw_queues = adapter->adapter_attr.rq_cnt;
2083 iopoll_q_count = adapter->adapter_attr.rq_cnt -
2084 adapter->notification_desc.iopoll_qdex;
2085 shost->nr_maps = iopoll_q_count ? 3 : 1;
2086 dev_info(&adapter->pdev->dev,
2087 "Max scsi I/O cmds %d shared with nr_hw_queues=%d\n",
2088 shost->can_queue, shost->nr_hw_queues);
2089 }
2090
2091 rc = scsi_add_host(shost, &pdev->dev);
2092 if (rc) {
2093 dev_err(&pdev->dev,
2094 "%s: SCSI host add failed\n", __func__);
2095 goto scsi_add_shost_fail;
2096 }
2097
2098 spin_lock(&leapraid_adapter_lock);
2099 list_add_tail(&adapter->list, &leapraid_adapter_list);
2100 spin_unlock(&leapraid_adapter_lock);
2101
2102 scsi_scan_host(shost);
2103 return 0;
2104
2105 scsi_add_shost_fail:
2106 leapraid_remove_ctrl(adapter);
2107 ctrl_init_fail:
2108 leapraid_overheat_cleanup(adapter);
2109 destroy_workqueue(adapter->fw_evt_s.fw_evt_thread);
2110 evt_wq_fail:
2111 scsi_host_put(shost);
2112 return rc;
2113 }
2114
leapraid_cleanup_lists(struct leapraid_adapter * adapter)2115 void leapraid_cleanup_lists(struct leapraid_adapter *adapter)
2116 {
2117 struct leapraid_raid_volume *raid_volume, *next_raid_volume;
2118 struct leapraid_starget_priv *starget_priv_data;
2119 struct leapraid_sas_port *leapraid_port, *next_port;
2120 struct leapraid_card_port *port, *port_next;
2121 struct leapraid_vphy *vphy, *vphy_next;
2122
2123 list_for_each_entry_safe(raid_volume, next_raid_volume,
2124 &adapter->dev_topo.raid_volume_list, list) {
2125 if (raid_volume->starget) {
2126 starget_priv_data = raid_volume->starget->hostdata;
2127 if (starget_priv_data)
2128 starget_priv_data->deleted = 1;
2129 scsi_remove_target(&raid_volume->starget->dev);
2130 }
2131 dev_info(&adapter->pdev->dev,
2132 "removing hdl=0x%04x, wwid=0x%016llx\n",
2133 raid_volume->hdl,
2134 (unsigned long long)raid_volume->wwid);
2135 leapraid_raid_volume_remove(adapter, raid_volume);
2136 }
2137
2138 list_for_each_entry_safe(leapraid_port, next_port,
2139 &adapter->dev_topo.card.sas_port_list,
2140 port_list) {
2141 if (leapraid_port->remote_identify.device_type ==
2142 SAS_END_DEVICE)
2143 leapraid_sas_dev_remove_by_sas_address(
2144 adapter,
2145 leapraid_port->remote_identify.sas_address,
2146 leapraid_port->card_port);
2147 else if (leapraid_port->remote_identify.device_type ==
2148 SAS_EDGE_EXPANDER_DEVICE ||
2149 leapraid_port->remote_identify.device_type ==
2150 SAS_FANOUT_EXPANDER_DEVICE)
2151 leapraid_exp_rm(
2152 adapter,
2153 leapraid_port->remote_identify.sas_address,
2154 leapraid_port->card_port);
2155 }
2156
2157 list_for_each_entry_safe(port, port_next,
2158 &adapter->dev_topo.card_port_list, list) {
2159 if (port->vphys_mask)
2160 list_for_each_entry_safe(vphy, vphy_next,
2161 &port->vphys_list, list) {
2162 list_del(&vphy->list);
2163 kfree(vphy);
2164 }
2165 list_del(&port->list);
2166 kfree(port);
2167 }
2168
2169 if (adapter->dev_topo.card.phys_num) {
2170 kfree(adapter->dev_topo.card.card_phy);
2171 adapter->dev_topo.card.card_phy = NULL;
2172 adapter->dev_topo.card.phys_num = 0;
2173 }
2174 }
2175
leapraid_remove(struct pci_dev * pdev)2176 static void leapraid_remove(struct pci_dev *pdev)
2177 {
2178 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2179 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2180 struct workqueue_struct *wq;
2181 unsigned long flags;
2182
2183 if (!shost || !adapter) {
2184 dev_err(&pdev->dev, "Unable to remove!\n");
2185 return;
2186 }
2187
2188 wait_event(adapter->scan_dev_desc.wait_driver_loading,
2189 !adapter->scan_dev_desc.driver_loading);
2190 wait_event(adapter->scan_dev_desc.wait_driver_loading,
2191 !atomic_read(&adapter->overheat_desc.thermal_alert));
2192
2193 WRITE_ONCE(adapter->access_ctrl.host_removing, 1);
2194 spin_lock(&leapraid_adapter_lock);
2195 list_del(&adapter->list);
2196 spin_unlock(&leapraid_adapter_lock);
2197
2198 leapraid_wait_cmds_done(adapter);
2199
2200 if (leapraid_pci_removed(adapter)) {
2201 leapraid_mq_polling_pause(adapter);
2202 leapraid_clean_active_scsi_cmds(adapter);
2203 }
2204 leapraid_clean_active_fw_evt(adapter);
2205
2206 spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2207 wq = adapter->fw_evt_s.fw_evt_thread;
2208 adapter->fw_evt_s.fw_evt_thread = NULL;
2209 spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2210 if (wq)
2211 destroy_workqueue(wq);
2212
2213 sas_remove_host(shost);
2214 leapraid_cleanup_lists(adapter);
2215 leapraid_remove_ctrl(adapter);
2216 scsi_host_put(shost);
2217 }
2218
leapraid_shutdown(struct pci_dev * pdev)2219 static void leapraid_shutdown(struct pci_dev *pdev)
2220 {
2221 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2222 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2223 struct workqueue_struct *wq;
2224 unsigned long flags;
2225
2226 if (!shost || !adapter) {
2227 dev_err(&pdev->dev, "Unable to shutdown!\n");
2228 return;
2229 }
2230
2231 adapter->access_ctrl.host_removing = 1;
2232 leapraid_wait_cmds_done(adapter);
2233 leapraid_clean_active_fw_evt(adapter);
2234 leapraid_overheat_cleanup(adapter);
2235 leapraid_fw_log_stop(adapter);
2236 spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2237 wq = adapter->fw_evt_s.fw_evt_thread;
2238 adapter->fw_evt_s.fw_evt_thread = NULL;
2239 spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2240 if (wq)
2241 destroy_workqueue(wq);
2242
2243 leapraid_disable_controller(adapter);
2244 }
2245
leapraid_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)2246 static pci_ers_result_t leapraid_pci_error_detected(struct pci_dev *pdev,
2247 pci_channel_state_t state)
2248 {
2249 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2250 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2251
2252 if (!shost || !adapter) {
2253 dev_err(&pdev->dev, "Failed to error detected for device\n");
2254 return PCI_ERS_RESULT_DISCONNECT;
2255 }
2256
2257 dev_err(&pdev->dev, "%s: PCI error detected, state=%d\n",
2258 adapter->adapter_attr.name, state);
2259
2260 switch (state) {
2261 case pci_channel_io_normal:
2262 return PCI_ERS_RESULT_CAN_RECOVER;
2263 case pci_channel_io_frozen:
2264 adapter->access_ctrl.pcie_recovering = 1;
2265 scsi_block_requests(adapter->shost);
2266 leapraid_overheat_cleanup(adapter);
2267 leapraid_check_scheduled_fault_stop(adapter);
2268 leapraid_fw_log_stop(adapter);
2269 leapraid_disable_controller(adapter);
2270 return PCI_ERS_RESULT_NEED_RESET;
2271 case pci_channel_io_perm_failure:
2272 adapter->access_ctrl.pcie_recovering = 1;
2273 leapraid_overheat_cleanup(adapter);
2274 leapraid_check_scheduled_fault_stop(adapter);
2275 leapraid_fw_log_stop(adapter);
2276 leapraid_mq_polling_pause(adapter);
2277 leapraid_clean_active_scsi_cmds(adapter);
2278 return PCI_ERS_RESULT_DISCONNECT;
2279 }
2280
2281 return PCI_ERS_RESULT_NEED_RESET;
2282 }
2283
leapraid_pci_mmio_enabled(struct pci_dev * pdev)2284 static pci_ers_result_t leapraid_pci_mmio_enabled(struct pci_dev *pdev)
2285 {
2286 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2287 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2288
2289 if (!shost || !adapter) {
2290 dev_err(&pdev->dev,
2291 "Failed to enable mmio for device\n");
2292 return PCI_ERS_RESULT_DISCONNECT;
2293 }
2294
2295 dev_info(&pdev->dev, "%s: PCI error mmio enabled\n",
2296 adapter->adapter_attr.name);
2297
2298 return PCI_ERS_RESULT_RECOVERED;
2299 }
2300
leapraid_pci_slot_reset(struct pci_dev * pdev)2301 static pci_ers_result_t leapraid_pci_slot_reset(struct pci_dev *pdev)
2302 {
2303 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2304 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2305 int rc;
2306
2307 if (!shost || !adapter) {
2308 dev_err(&pdev->dev,
2309 "Failed to slot reset for device\n");
2310 return PCI_ERS_RESULT_DISCONNECT;
2311 }
2312
2313 dev_err(&pdev->dev, "%s PCI error slot reset\n",
2314 adapter->adapter_attr.name);
2315
2316 adapter->pdev = pdev;
2317 pci_restore_state(pdev);
2318 if (leapraid_set_pcie_and_notification(adapter)) {
2319 dev_err(&pdev->dev,
2320 "%s: Failed to set PCIe state and notification\n",
2321 __func__);
2322 return PCI_ERS_RESULT_DISCONNECT;
2323 }
2324
2325 adapter->access_ctrl.pcie_recovering = 0;
2326 dev_info(&pdev->dev, "%s: Hard reset triggered by PCI slot reset\n",
2327 adapter->adapter_attr.name);
2328 dev_info(&adapter->pdev->dev, "%s: %d: call hard_reset\n",
2329 __func__, __LINE__);
2330 rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
2331 if (rc)
2332 dev_err(&pdev->dev, "%s hard reset: failed\n",
2333 adapter->adapter_attr.name);
2334
2335 return rc == 0 ? PCI_ERS_RESULT_RECOVERED :
2336 PCI_ERS_RESULT_DISCONNECT;
2337 }
2338
leapraid_pci_resume(struct pci_dev * pdev)2339 static void leapraid_pci_resume(struct pci_dev *pdev)
2340 {
2341 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2342 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2343
2344 if (!shost || !adapter) {
2345 dev_err(&pdev->dev, "Failed to resume\n");
2346 return;
2347 }
2348
2349 dev_err(&pdev->dev, "PCI error resume!\n");
2350
2351 pci_aer_clear_nonfatal_status(pdev);
2352 leapraid_check_scheduled_fault_start(adapter);
2353 leapraid_fw_log_start(adapter);
2354 scsi_unblock_requests(adapter->shost);
2355 }
2356
2357 MODULE_DEVICE_TABLE(pci, leapraid_pci_table);
2358 static struct pci_error_handlers leapraid_err_handler = {
2359 .error_detected = leapraid_pci_error_detected,
2360 .mmio_enabled = leapraid_pci_mmio_enabled,
2361 .slot_reset = leapraid_pci_slot_reset,
2362 .resume = leapraid_pci_resume,
2363 };
2364
2365 #ifdef CONFIG_PM
leapraid_suspend(struct pci_dev * pdev,pm_message_t state)2366 static int leapraid_suspend(struct pci_dev *pdev, pm_message_t state)
2367 {
2368 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2369 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2370 pci_power_t device_state;
2371
2372 if (!shost || !adapter) {
2373 dev_err(&pdev->dev,
2374 "Suspend failed, invalid host or adapter\n");
2375 return -ENXIO;
2376 }
2377
2378 leapraid_overheat_cleanup(adapter);
2379 leapraid_check_scheduled_fault_stop(adapter);
2380 leapraid_fw_log_stop(adapter);
2381 scsi_block_requests(shost);
2382 device_state = pci_choose_state(pdev, state);
2383
2384 dev_info(&pdev->dev, "Entering PCI power state D%d, (slot=%s)\n",
2385 device_state, pci_name(pdev));
2386
2387 pci_save_state(pdev);
2388 leapraid_disable_controller(adapter);
2389 pci_set_power_state(pdev, device_state);
2390 return 0;
2391 }
2392
leapraid_resume(struct pci_dev * pdev)2393 static int leapraid_resume(struct pci_dev *pdev)
2394 {
2395 struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2396 struct Scsi_Host *shost = pci_get_drvdata(pdev);
2397 pci_power_t device_state = pdev->current_state;
2398 int rc;
2399
2400 if (!shost || !adapter) {
2401 dev_err(&pdev->dev,
2402 "Resume failed, invalid host or adapter\n");
2403 return -ENXIO;
2404 }
2405
2406 dev_info(&pdev->dev,
2407 "Resuming device %s, previous state D%d\n",
2408 pci_name(pdev), device_state);
2409
2410 pci_set_power_state(pdev, PCI_D0);
2411 pci_enable_wake(pdev, PCI_D0, 0);
2412 pci_restore_state(pdev);
2413 adapter->pdev = pdev;
2414 rc = leapraid_set_pcie_and_notification(adapter);
2415 if (rc) {
2416 dev_err(&pdev->dev,
2417 "%s: Failed to set PCIe state and notification\n",
2418 __func__);
2419 return rc;
2420 }
2421
2422 dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
2423 __func__, __LINE__);
2424 rc = leapraid_hard_reset_handler(adapter, PART_RESET);
2425 if (rc) {
2426 dev_err(&adapter->pdev->dev,
2427 "%s: Hard reset failed during resume, rc=%d\n",
2428 __func__, rc);
2429 return rc;
2430 }
2431 scsi_unblock_requests(shost);
2432 leapraid_check_scheduled_fault_start(adapter);
2433 leapraid_fw_log_start(adapter);
2434 return 0;
2435 }
2436 #endif /* CONFIG_PM */
2437
2438 static struct pci_driver leapraid_driver = {
2439 .name = LEAPRAID_DRIVER_NAME,
2440 .id_table = leapraid_pci_table,
2441 .probe = leapraid_probe,
2442 .remove = leapraid_remove,
2443 .shutdown = leapraid_shutdown,
2444 .err_handler = &leapraid_err_handler,
2445 #ifdef CONFIG_PM
2446 .suspend = leapraid_suspend,
2447 .resume = leapraid_resume,
2448 #endif /* CONFIG_PM */
2449 };
2450
leapraid_init(void)2451 static int __init leapraid_init(void)
2452 {
2453 int error;
2454
2455 pr_info("%s initializing\n", LEAPRAID_DRIVER_NAME);
2456
2457 leapraid_transport_template =
2458 sas_attach_transport(&leapraid_transport_functions);
2459 if (!leapraid_transport_template) {
2460 pr_err("%s: Failed to attach SAS transport\n",
2461 LEAPRAID_DRIVER_NAME);
2462 return -ENODEV;
2463 }
2464
2465 error = pci_register_driver(&leapraid_driver);
2466 if (error) {
2467 pr_err("%s: PCI driver registration failed: %d\n",
2468 LEAPRAID_DRIVER_NAME, error);
2469 sas_release_transport(leapraid_transport_template);
2470 return error;
2471 }
2472
2473 error = leapraid_ctl_init();
2474 if (error) {
2475 pci_unregister_driver(&leapraid_driver);
2476 sas_release_transport(leapraid_transport_template);
2477 return error;
2478 }
2479
2480 return 0;
2481 }
2482
leapraid_exit(void)2483 static void __exit leapraid_exit(void)
2484 {
2485 pr_info("%s exiting\n", LEAPRAID_DRIVER_NAME);
2486
2487 leapraid_ctl_exit();
2488 pci_unregister_driver(&leapraid_driver);
2489 sas_release_transport(leapraid_transport_template);
2490 }
2491
2492 module_init(leapraid_init);
2493 module_exit(leapraid_exit);
2494