1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
5 */
6
7 #include "hisi_sas.h"
8 #define DRV_NAME "hisi_sas"
9
10 #define DEV_IS_GONE(dev) \
11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
12
13 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
14 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
15 void *funcdata);
16 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
17 struct domain_device *device);
18 static void hisi_sas_dev_gone(struct domain_device *device);
19
20 struct hisi_sas_internal_abort_data {
21 bool rst_ha_timeout; /* reset the HA for timeout */
22 };
23
hisi_sas_get_ata_protocol(struct host_to_dev_fis * fis,int direction)24 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
25 {
26 switch (fis->command) {
27 case ATA_CMD_FPDMA_WRITE:
28 case ATA_CMD_FPDMA_READ:
29 case ATA_CMD_FPDMA_RECV:
30 case ATA_CMD_FPDMA_SEND:
31 case ATA_CMD_NCQ_NON_DATA:
32 return HISI_SAS_SATA_PROTOCOL_FPDMA;
33
34 case ATA_CMD_DOWNLOAD_MICRO:
35 case ATA_CMD_ID_ATA:
36 case ATA_CMD_PMP_READ:
37 case ATA_CMD_READ_LOG_EXT:
38 case ATA_CMD_PIO_READ:
39 case ATA_CMD_PIO_READ_EXT:
40 case ATA_CMD_PMP_WRITE:
41 case ATA_CMD_WRITE_LOG_EXT:
42 case ATA_CMD_PIO_WRITE:
43 case ATA_CMD_PIO_WRITE_EXT:
44 return HISI_SAS_SATA_PROTOCOL_PIO;
45
46 case ATA_CMD_DSM:
47 case ATA_CMD_DOWNLOAD_MICRO_DMA:
48 case ATA_CMD_PMP_READ_DMA:
49 case ATA_CMD_PMP_WRITE_DMA:
50 case ATA_CMD_READ:
51 case ATA_CMD_READ_EXT:
52 case ATA_CMD_READ_LOG_DMA_EXT:
53 case ATA_CMD_READ_STREAM_DMA_EXT:
54 case ATA_CMD_TRUSTED_RCV_DMA:
55 case ATA_CMD_TRUSTED_SND_DMA:
56 case ATA_CMD_WRITE:
57 case ATA_CMD_WRITE_EXT:
58 case ATA_CMD_WRITE_FUA_EXT:
59 case ATA_CMD_WRITE_QUEUED:
60 case ATA_CMD_WRITE_LOG_DMA_EXT:
61 case ATA_CMD_WRITE_STREAM_DMA_EXT:
62 case ATA_CMD_ZAC_MGMT_IN:
63 return HISI_SAS_SATA_PROTOCOL_DMA;
64
65 case ATA_CMD_CHK_POWER:
66 case ATA_CMD_DEV_RESET:
67 case ATA_CMD_EDD:
68 case ATA_CMD_FLUSH:
69 case ATA_CMD_FLUSH_EXT:
70 case ATA_CMD_VERIFY:
71 case ATA_CMD_VERIFY_EXT:
72 case ATA_CMD_SET_FEATURES:
73 case ATA_CMD_STANDBY:
74 case ATA_CMD_STANDBYNOW1:
75 case ATA_CMD_ZAC_MGMT_OUT:
76 return HISI_SAS_SATA_PROTOCOL_NONDATA;
77
78 case ATA_CMD_SET_MAX:
79 switch (fis->features) {
80 case ATA_SET_MAX_PASSWD:
81 case ATA_SET_MAX_LOCK:
82 return HISI_SAS_SATA_PROTOCOL_PIO;
83
84 case ATA_SET_MAX_PASSWD_DMA:
85 case ATA_SET_MAX_UNLOCK_DMA:
86 return HISI_SAS_SATA_PROTOCOL_DMA;
87
88 default:
89 return HISI_SAS_SATA_PROTOCOL_NONDATA;
90 }
91
92 default:
93 {
94 if (direction == DMA_NONE)
95 return HISI_SAS_SATA_PROTOCOL_NONDATA;
96 return HISI_SAS_SATA_PROTOCOL_PIO;
97 }
98 }
99 }
100 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
101
hisi_sas_sata_done(struct sas_task * task,struct hisi_sas_slot * slot)102 void hisi_sas_sata_done(struct sas_task *task,
103 struct hisi_sas_slot *slot)
104 {
105 struct task_status_struct *ts = &task->task_status;
106 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
107 struct hisi_sas_status_buffer *status_buf =
108 hisi_sas_status_buf_addr_mem(slot);
109 u8 *iu = &status_buf->iu[0];
110 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
111
112 resp->frame_len = sizeof(struct dev_to_host_fis);
113 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
114
115 ts->buf_valid_size = sizeof(*resp);
116 }
117 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
118
119 /*
120 * This function assumes linkrate mask fits in 8 bits, which it
121 * does for all HW versions supported.
122 */
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)123 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
124 {
125 u8 rate = 0;
126 int i;
127
128 max -= SAS_LINK_RATE_1_5_GBPS;
129 for (i = 0; i <= max; i++)
130 rate |= 1 << (i * 2);
131 return rate;
132 }
133 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
134
dev_to_hisi_hba(struct domain_device * device)135 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
136 {
137 return device->port->ha->lldd_ha;
138 }
139
to_hisi_sas_port(struct asd_sas_port * sas_port)140 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
141 {
142 return container_of(sas_port, struct hisi_sas_port, sas_port);
143 }
144 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
145
hisi_sas_stop_phys(struct hisi_hba * hisi_hba)146 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
147 {
148 int phy_no;
149
150 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
151 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
152 }
153 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
154
hisi_sas_slot_index_clear(struct hisi_hba * hisi_hba,int slot_idx)155 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
156 {
157 void *bitmap = hisi_hba->slot_index_tags;
158
159 __clear_bit(slot_idx, bitmap);
160 }
161
hisi_sas_slot_index_free(struct hisi_hba * hisi_hba,int slot_idx)162 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
163 {
164 if (hisi_hba->hw->slot_index_alloc ||
165 slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
166 spin_lock(&hisi_hba->lock);
167 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
168 spin_unlock(&hisi_hba->lock);
169 }
170 }
171
hisi_sas_slot_index_set(struct hisi_hba * hisi_hba,int slot_idx)172 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
173 {
174 void *bitmap = hisi_hba->slot_index_tags;
175
176 __set_bit(slot_idx, bitmap);
177 }
178
hisi_sas_slot_index_alloc(struct hisi_hba * hisi_hba,struct scsi_cmnd * scsi_cmnd)179 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
180 struct scsi_cmnd *scsi_cmnd)
181 {
182 int index;
183 void *bitmap = hisi_hba->slot_index_tags;
184
185 if (scsi_cmnd)
186 return scsi_cmd_to_rq(scsi_cmnd)->tag;
187
188 spin_lock(&hisi_hba->lock);
189 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
190 hisi_hba->last_slot_index + 1);
191 if (index >= hisi_hba->slot_index_count) {
192 index = find_next_zero_bit(bitmap,
193 hisi_hba->slot_index_count,
194 HISI_SAS_UNRESERVED_IPTT);
195 if (index >= hisi_hba->slot_index_count) {
196 spin_unlock(&hisi_hba->lock);
197 return -SAS_QUEUE_FULL;
198 }
199 }
200 hisi_sas_slot_index_set(hisi_hba, index);
201 hisi_hba->last_slot_index = index;
202 spin_unlock(&hisi_hba->lock);
203
204 return index;
205 }
206
hisi_sas_slot_task_free(struct hisi_hba * hisi_hba,struct sas_task * task,struct hisi_sas_slot * slot)207 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
208 struct hisi_sas_slot *slot)
209 {
210 int device_id = slot->device_id;
211 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
212
213 if (task) {
214 struct device *dev = hisi_hba->dev;
215
216 if (!task->lldd_task)
217 return;
218
219 task->lldd_task = NULL;
220
221 if (!sas_protocol_ata(task->task_proto)) {
222 if (slot->n_elem) {
223 if (task->task_proto & SAS_PROTOCOL_SSP)
224 dma_unmap_sg(dev, task->scatter,
225 task->num_scatter,
226 task->data_dir);
227 else
228 dma_unmap_sg(dev, &task->smp_task.smp_req,
229 1, DMA_TO_DEVICE);
230 }
231 if (slot->n_elem_dif) {
232 struct sas_ssp_task *ssp_task = &task->ssp_task;
233 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
234
235 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
236 scsi_prot_sg_count(scsi_cmnd),
237 task->data_dir);
238 }
239 }
240 }
241
242 spin_lock(&sas_dev->lock);
243 list_del_init(&slot->entry);
244 spin_unlock(&sas_dev->lock);
245
246 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
247
248 hisi_sas_slot_index_free(hisi_hba, slot->idx);
249 }
250 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
251
hisi_sas_task_prep_smp(struct hisi_hba * hisi_hba,struct hisi_sas_slot * slot)252 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
253 struct hisi_sas_slot *slot)
254 {
255 hisi_hba->hw->prep_smp(hisi_hba, slot);
256 }
257
hisi_sas_task_prep_ssp(struct hisi_hba * hisi_hba,struct hisi_sas_slot * slot)258 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
259 struct hisi_sas_slot *slot)
260 {
261 hisi_hba->hw->prep_ssp(hisi_hba, slot);
262 }
263
hisi_sas_task_prep_ata(struct hisi_hba * hisi_hba,struct hisi_sas_slot * slot)264 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
265 struct hisi_sas_slot *slot)
266 {
267 hisi_hba->hw->prep_stp(hisi_hba, slot);
268 }
269
hisi_sas_task_prep_abort(struct hisi_hba * hisi_hba,struct hisi_sas_slot * slot)270 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
271 struct hisi_sas_slot *slot)
272 {
273 hisi_hba->hw->prep_abort(hisi_hba, slot);
274 }
275
hisi_sas_dma_unmap(struct hisi_hba * hisi_hba,struct sas_task * task,int n_elem)276 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
277 struct sas_task *task, int n_elem)
278 {
279 struct device *dev = hisi_hba->dev;
280
281 if (!sas_protocol_ata(task->task_proto) && n_elem) {
282 if (task->num_scatter) {
283 dma_unmap_sg(dev, task->scatter, task->num_scatter,
284 task->data_dir);
285 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
286 dma_unmap_sg(dev, &task->smp_task.smp_req,
287 1, DMA_TO_DEVICE);
288 }
289 }
290 }
291
hisi_sas_dma_map(struct hisi_hba * hisi_hba,struct sas_task * task,int * n_elem)292 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
293 struct sas_task *task, int *n_elem)
294 {
295 struct device *dev = hisi_hba->dev;
296 int rc;
297
298 if (sas_protocol_ata(task->task_proto)) {
299 *n_elem = task->num_scatter;
300 } else {
301 unsigned int req_len;
302
303 if (task->num_scatter) {
304 *n_elem = dma_map_sg(dev, task->scatter,
305 task->num_scatter, task->data_dir);
306 if (!*n_elem) {
307 rc = -ENOMEM;
308 goto prep_out;
309 }
310 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
311 *n_elem = dma_map_sg(dev, &task->smp_task.smp_req,
312 1, DMA_TO_DEVICE);
313 if (!*n_elem) {
314 rc = -ENOMEM;
315 goto prep_out;
316 }
317 req_len = sg_dma_len(&task->smp_task.smp_req);
318 if (req_len & 0x3) {
319 rc = -EINVAL;
320 goto err_out_dma_unmap;
321 }
322 }
323 }
324
325 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
326 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
327 *n_elem);
328 rc = -EINVAL;
329 goto err_out_dma_unmap;
330 }
331 return 0;
332
333 err_out_dma_unmap:
334 /* It would be better to call dma_unmap_sg() here, but it's messy */
335 hisi_sas_dma_unmap(hisi_hba, task, *n_elem);
336 prep_out:
337 return rc;
338 }
339
hisi_sas_dif_dma_unmap(struct hisi_hba * hisi_hba,struct sas_task * task,int n_elem_dif)340 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
341 struct sas_task *task, int n_elem_dif)
342 {
343 struct device *dev = hisi_hba->dev;
344
345 if (n_elem_dif) {
346 struct sas_ssp_task *ssp_task = &task->ssp_task;
347 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
348
349 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
350 scsi_prot_sg_count(scsi_cmnd),
351 task->data_dir);
352 }
353 }
354
hisi_sas_dif_dma_map(struct hisi_hba * hisi_hba,int * n_elem_dif,struct sas_task * task)355 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
356 int *n_elem_dif, struct sas_task *task)
357 {
358 struct device *dev = hisi_hba->dev;
359 struct sas_ssp_task *ssp_task;
360 struct scsi_cmnd *scsi_cmnd;
361 int rc;
362
363 if (task->num_scatter) {
364 ssp_task = &task->ssp_task;
365 scsi_cmnd = ssp_task->cmd;
366
367 if (scsi_prot_sg_count(scsi_cmnd)) {
368 *n_elem_dif = dma_map_sg(dev,
369 scsi_prot_sglist(scsi_cmnd),
370 scsi_prot_sg_count(scsi_cmnd),
371 task->data_dir);
372
373 if (!*n_elem_dif)
374 return -ENOMEM;
375
376 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
377 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
378 *n_elem_dif);
379 rc = -EINVAL;
380 goto err_out_dif_dma_unmap;
381 }
382 }
383 }
384
385 return 0;
386
387 err_out_dif_dma_unmap:
388 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
389 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
390 return rc;
391 }
392
393 static
hisi_sas_task_deliver(struct hisi_hba * hisi_hba,struct hisi_sas_slot * slot,struct hisi_sas_dq * dq,struct hisi_sas_device * sas_dev)394 void hisi_sas_task_deliver(struct hisi_hba *hisi_hba,
395 struct hisi_sas_slot *slot,
396 struct hisi_sas_dq *dq,
397 struct hisi_sas_device *sas_dev)
398 {
399 struct hisi_sas_cmd_hdr *cmd_hdr_base;
400 int dlvry_queue_slot, dlvry_queue;
401 struct sas_task *task = slot->task;
402 int wr_q_index;
403
404 spin_lock(&dq->lock);
405 wr_q_index = dq->wr_point;
406 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
407 list_add_tail(&slot->delivery, &dq->list);
408 spin_unlock(&dq->lock);
409 spin_lock(&sas_dev->lock);
410 list_add_tail(&slot->entry, &sas_dev->list);
411 spin_unlock(&sas_dev->lock);
412
413 dlvry_queue = dq->id;
414 dlvry_queue_slot = wr_q_index;
415
416 slot->device_id = sas_dev->device_id;
417 slot->dlvry_queue = dlvry_queue;
418 slot->dlvry_queue_slot = dlvry_queue_slot;
419 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
420 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
421
422 task->lldd_task = slot;
423
424 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
425 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
426 memset(hisi_sas_status_buf_addr_mem(slot), 0,
427 sizeof(struct hisi_sas_err_record));
428
429 switch (task->task_proto) {
430 case SAS_PROTOCOL_SMP:
431 hisi_sas_task_prep_smp(hisi_hba, slot);
432 break;
433 case SAS_PROTOCOL_SSP:
434 hisi_sas_task_prep_ssp(hisi_hba, slot);
435 break;
436 case SAS_PROTOCOL_SATA:
437 case SAS_PROTOCOL_STP:
438 case SAS_PROTOCOL_STP_ALL:
439 hisi_sas_task_prep_ata(hisi_hba, slot);
440 break;
441 case SAS_PROTOCOL_INTERNAL_ABORT:
442 hisi_sas_task_prep_abort(hisi_hba, slot);
443 break;
444 default:
445 return;
446 }
447
448 /* Make slot memories observable before marking as ready */
449 smp_wmb();
450 WRITE_ONCE(slot->ready, 1);
451
452 spin_lock(&dq->lock);
453 hisi_hba->hw->start_delivery(dq);
454 spin_unlock(&dq->lock);
455 }
456
hisi_sas_queue_command(struct sas_task * task,gfp_t gfp_flags)457 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
458 {
459 int n_elem = 0, n_elem_dif = 0;
460 struct domain_device *device = task->dev;
461 struct asd_sas_port *sas_port = device->port;
462 struct hisi_sas_device *sas_dev = device->lldd_dev;
463 bool internal_abort = sas_is_internal_abort(task);
464 struct scsi_cmnd *scmd = NULL;
465 struct hisi_sas_dq *dq = NULL;
466 struct hisi_sas_port *port;
467 struct hisi_hba *hisi_hba;
468 struct hisi_sas_slot *slot;
469 struct device *dev;
470 int rc;
471
472 if (!sas_port) {
473 struct task_status_struct *ts = &task->task_status;
474
475 ts->resp = SAS_TASK_UNDELIVERED;
476 ts->stat = SAS_PHY_DOWN;
477 /*
478 * libsas will use dev->port, should
479 * not call task_done for sata
480 */
481 if (device->dev_type != SAS_SATA_DEV && !internal_abort)
482 task->task_done(task);
483 return -ECOMM;
484 }
485
486 hisi_hba = dev_to_hisi_hba(device);
487 dev = hisi_hba->dev;
488
489 switch (task->task_proto) {
490 case SAS_PROTOCOL_SSP:
491 case SAS_PROTOCOL_SMP:
492 case SAS_PROTOCOL_SATA:
493 case SAS_PROTOCOL_STP:
494 case SAS_PROTOCOL_STP_ALL:
495 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
496 if (!gfpflags_allow_blocking(gfp_flags))
497 return -EINVAL;
498
499 down(&hisi_hba->sem);
500 up(&hisi_hba->sem);
501 }
502
503 if (DEV_IS_GONE(sas_dev)) {
504 if (sas_dev)
505 dev_info(dev, "task prep: device %d not ready\n",
506 sas_dev->device_id);
507 else
508 dev_info(dev, "task prep: device %016llx not ready\n",
509 SAS_ADDR(device->sas_addr));
510
511 return -ECOMM;
512 }
513
514 port = to_hisi_sas_port(sas_port);
515 if (!port->port_attached) {
516 dev_info(dev, "task prep: %s port%d not attach device\n",
517 dev_is_sata(device) ? "SATA/STP" : "SAS",
518 device->port->id);
519
520 return -ECOMM;
521 }
522
523 if (task->uldd_task) {
524 struct ata_queued_cmd *qc;
525
526 if (dev_is_sata(device)) {
527 qc = task->uldd_task;
528 scmd = qc->scsicmd;
529 } else {
530 scmd = task->uldd_task;
531 }
532 }
533
534 if (scmd) {
535 unsigned int dq_index;
536 u32 blk_tag;
537
538 blk_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
539 dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
540 dq = &hisi_hba->dq[dq_index];
541 } else {
542 struct Scsi_Host *shost = hisi_hba->shost;
543 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
544 int queue = qmap->mq_map[raw_smp_processor_id()];
545
546 dq = &hisi_hba->dq[queue];
547 }
548 break;
549 case SAS_PROTOCOL_INTERNAL_ABORT:
550 if (!hisi_hba->hw->prep_abort)
551 return TMF_RESP_FUNC_FAILED;
552
553 if (test_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags))
554 return -EIO;
555
556 hisi_hba = dev_to_hisi_hba(device);
557
558 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
559 return -EINVAL;
560
561 port = to_hisi_sas_port(sas_port);
562 dq = &hisi_hba->dq[task->abort_task.qid];
563 break;
564 default:
565 dev_err(hisi_hba->dev, "task prep: unknown/unsupported proto (0x%x)\n",
566 task->task_proto);
567 return -EINVAL;
568 }
569
570 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem);
571 if (rc < 0)
572 goto prep_out;
573
574 if (!sas_protocol_ata(task->task_proto)) {
575 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
576 if (rc < 0)
577 goto err_out_dma_unmap;
578 }
579
580 if (!internal_abort && hisi_hba->hw->slot_index_alloc)
581 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
582 else
583 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
584
585 if (rc < 0)
586 goto err_out_dif_dma_unmap;
587
588 slot = &hisi_hba->slot_info[rc];
589 slot->n_elem = n_elem;
590 slot->n_elem_dif = n_elem_dif;
591 slot->task = task;
592 slot->port = port;
593
594 slot->tmf = task->tmf;
595 slot->is_internal = !!task->tmf || internal_abort;
596
597 /* protect task_prep and start_delivery sequence */
598 hisi_sas_task_deliver(hisi_hba, slot, dq, sas_dev);
599
600 return 0;
601
602 err_out_dif_dma_unmap:
603 if (!sas_protocol_ata(task->task_proto))
604 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
605 err_out_dma_unmap:
606 hisi_sas_dma_unmap(hisi_hba, task, n_elem);
607 prep_out:
608 dev_err(dev, "task exec: failed[%d]!\n", rc);
609 return rc;
610 }
611
hisi_sas_bytes_dmaed(struct hisi_hba * hisi_hba,int phy_no,gfp_t gfp_flags)612 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no,
613 gfp_t gfp_flags)
614 {
615 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
616 struct asd_sas_phy *sas_phy = &phy->sas_phy;
617
618 if (!phy->phy_attached)
619 return;
620
621 sas_notify_phy_event(sas_phy, PHYE_OOB_DONE, gfp_flags);
622
623 if (sas_phy->phy) {
624 struct sas_phy *sphy = sas_phy->phy;
625
626 sphy->negotiated_linkrate = sas_phy->linkrate;
627 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
628 sphy->maximum_linkrate_hw =
629 hisi_hba->hw->phy_get_max_linkrate();
630 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
631 sphy->minimum_linkrate = phy->minimum_linkrate;
632
633 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
634 sphy->maximum_linkrate = phy->maximum_linkrate;
635 }
636
637 if (phy->phy_type & PORT_TYPE_SAS) {
638 struct sas_identify_frame *id;
639
640 id = (struct sas_identify_frame *)phy->frame_rcvd;
641 id->dev_type = phy->identify.device_type;
642 id->initiator_bits = SAS_PROTOCOL_ALL;
643 id->target_bits = phy->identify.target_port_protocols;
644 } else if (phy->phy_type & PORT_TYPE_SATA) {
645 /* Nothing */
646 }
647
648 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
649 sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED, gfp_flags);
650 }
651
hisi_sas_alloc_dev(struct domain_device * device)652 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
653 {
654 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
655 struct hisi_sas_device *sas_dev = NULL;
656 int last = hisi_hba->last_dev_id;
657 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
658 int i;
659
660 spin_lock(&hisi_hba->lock);
661 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
662 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
663 int queue = i % hisi_hba->queue_count;
664 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
665
666 hisi_hba->devices[i].device_id = i;
667 sas_dev = &hisi_hba->devices[i];
668 sas_dev->dev_status = HISI_SAS_DEV_INIT;
669 sas_dev->dev_type = device->dev_type;
670 sas_dev->hisi_hba = hisi_hba;
671 sas_dev->sas_device = device;
672 sas_dev->dq = dq;
673 spin_lock_init(&sas_dev->lock);
674 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
675 break;
676 }
677 i++;
678 }
679 hisi_hba->last_dev_id = i;
680 spin_unlock(&hisi_hba->lock);
681
682 return sas_dev;
683 }
684
hisi_sas_tmf_aborted(struct sas_task * task)685 static void hisi_sas_tmf_aborted(struct sas_task *task)
686 {
687 struct hisi_sas_slot *slot = task->lldd_task;
688 struct domain_device *device = task->dev;
689 struct hisi_sas_device *sas_dev = device->lldd_dev;
690 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
691
692 if (slot) {
693 struct hisi_sas_cq *cq =
694 &hisi_hba->cq[slot->dlvry_queue];
695 /*
696 * sync irq to avoid free'ing task
697 * before using task in IO completion
698 */
699 synchronize_irq(cq->irq_no);
700 slot->task = NULL;
701 }
702 }
703
704 #define HISI_SAS_DISK_RECOVER_CNT 3
hisi_sas_init_device(struct domain_device * device)705 static int hisi_sas_init_device(struct domain_device *device)
706 {
707 int rc = TMF_RESP_FUNC_COMPLETE;
708 struct scsi_lun lun;
709 int retry = HISI_SAS_DISK_RECOVER_CNT;
710 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
711
712 switch (device->dev_type) {
713 case SAS_END_DEVICE:
714 int_to_scsilun(0, &lun);
715
716 while (retry-- > 0) {
717 rc = sas_abort_task_set(device, lun.scsi_lun);
718 if (rc == TMF_RESP_FUNC_COMPLETE) {
719 hisi_sas_release_task(hisi_hba, device);
720 break;
721 }
722 }
723 break;
724 case SAS_SATA_DEV:
725 case SAS_SATA_PM:
726 case SAS_SATA_PM_PORT:
727 case SAS_SATA_PENDING:
728 /*
729 * If an expander is swapped when a SATA disk is attached then
730 * we should issue a hard reset to clear previous affiliation
731 * of STP target port, see SPL (chapter 6.19.4).
732 *
733 * However we don't need to issue a hard reset here for these
734 * reasons:
735 * a. When probing the device, libsas/libata already issues a
736 * hard reset in sas_probe_sata() -> ata_sas_async_probe().
737 * Note that in hisi_sas_debug_I_T_nexus_reset() we take care
738 * to issue a hard reset by checking the dev status (== INIT).
739 * b. When resetting the controller, this is simply unnecessary.
740 */
741 while (retry-- > 0) {
742 rc = hisi_sas_softreset_ata_disk(device);
743 if (!rc)
744 break;
745 }
746 break;
747 default:
748 break;
749 }
750
751 return rc;
752 }
753
hisi_sas_slave_alloc(struct scsi_device * sdev)754 int hisi_sas_slave_alloc(struct scsi_device *sdev)
755 {
756 struct domain_device *ddev = sdev_to_domain_dev(sdev);
757 struct hisi_sas_device *sas_dev = ddev->lldd_dev;
758 int rc;
759
760 rc = sas_slave_alloc(sdev);
761 if (rc)
762 return rc;
763
764 rc = hisi_sas_init_device(ddev);
765 if (rc)
766 return rc;
767 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
768 return 0;
769 }
770 EXPORT_SYMBOL_GPL(hisi_sas_slave_alloc);
771
hisi_sas_dev_found(struct domain_device * device)772 static int hisi_sas_dev_found(struct domain_device *device)
773 {
774 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
775 struct domain_device *parent_dev = device->parent;
776 struct hisi_sas_device *sas_dev;
777 struct device *dev = hisi_hba->dev;
778 int rc;
779
780 if (hisi_hba->hw->alloc_dev)
781 sas_dev = hisi_hba->hw->alloc_dev(device);
782 else
783 sas_dev = hisi_sas_alloc_dev(device);
784 if (!sas_dev) {
785 dev_err(dev, "fail alloc dev: max support %d devices\n",
786 HISI_SAS_MAX_DEVICES);
787 return -EINVAL;
788 }
789
790 device->lldd_dev = sas_dev;
791 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
792
793 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
794 int phy_no;
795 u8 phy_num = parent_dev->ex_dev.num_phys;
796 struct ex_phy *phy;
797
798 for (phy_no = 0; phy_no < phy_num; phy_no++) {
799 phy = &parent_dev->ex_dev.ex_phy[phy_no];
800 if (SAS_ADDR(phy->attached_sas_addr) ==
801 SAS_ADDR(device->sas_addr))
802 break;
803 }
804
805 if (phy_no == phy_num) {
806 dev_info(dev, "dev found: no attached "
807 "dev:%016llx at ex:%016llx\n",
808 SAS_ADDR(device->sas_addr),
809 SAS_ADDR(parent_dev->sas_addr));
810 rc = -EINVAL;
811 goto err_out;
812 }
813 }
814
815 dev_info(dev, "dev[%d:%x] found\n",
816 sas_dev->device_id, sas_dev->dev_type);
817
818 return 0;
819
820 err_out:
821 hisi_sas_dev_gone(device);
822 return rc;
823 }
824
hisi_sas_slave_configure(struct scsi_device * sdev)825 int hisi_sas_slave_configure(struct scsi_device *sdev)
826 {
827 struct domain_device *dev = sdev_to_domain_dev(sdev);
828 int ret = sas_slave_configure(sdev);
829
830 if (ret)
831 return ret;
832 if (!dev_is_sata(dev))
833 sas_change_queue_depth(sdev, 64);
834
835 return 0;
836 }
837 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
838
hisi_sas_scan_start(struct Scsi_Host * shost)839 void hisi_sas_scan_start(struct Scsi_Host *shost)
840 {
841 struct hisi_hba *hisi_hba = shost_priv(shost);
842
843 hisi_hba->hw->phys_init(hisi_hba);
844 }
845 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
846
hisi_sas_scan_finished(struct Scsi_Host * shost,unsigned long time)847 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
848 {
849 struct hisi_hba *hisi_hba = shost_priv(shost);
850 struct sas_ha_struct *sha = &hisi_hba->sha;
851
852 /* Wait for PHY up interrupt to occur */
853 if (time < HZ)
854 return 0;
855
856 sas_drain_work(sha);
857 return 1;
858 }
859 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
860
hisi_sas_phyup_work_common(struct work_struct * work,enum hisi_sas_phy_event event)861 static void hisi_sas_phyup_work_common(struct work_struct *work,
862 enum hisi_sas_phy_event event)
863 {
864 struct hisi_sas_phy *phy =
865 container_of(work, typeof(*phy), works[event]);
866 struct hisi_hba *hisi_hba = phy->hisi_hba;
867 struct asd_sas_phy *sas_phy = &phy->sas_phy;
868 int phy_no = sas_phy->id;
869
870 phy->wait_phyup_cnt = 0;
871 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
872 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
873 hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL);
874 }
875
hisi_sas_phyup_work(struct work_struct * work)876 static void hisi_sas_phyup_work(struct work_struct *work)
877 {
878 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP);
879 }
880
hisi_sas_linkreset_work(struct work_struct * work)881 static void hisi_sas_linkreset_work(struct work_struct *work)
882 {
883 struct hisi_sas_phy *phy =
884 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
885 struct asd_sas_phy *sas_phy = &phy->sas_phy;
886
887 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
888 }
889
hisi_sas_phyup_pm_work(struct work_struct * work)890 static void hisi_sas_phyup_pm_work(struct work_struct *work)
891 {
892 struct hisi_sas_phy *phy =
893 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP_PM]);
894 struct hisi_hba *hisi_hba = phy->hisi_hba;
895 struct device *dev = hisi_hba->dev;
896
897 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP_PM);
898 pm_runtime_put_sync(dev);
899 }
900
901 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
902 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
903 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
904 [HISI_PHYE_PHY_UP_PM] = hisi_sas_phyup_pm_work,
905 };
906
hisi_sas_notify_phy_event(struct hisi_sas_phy * phy,enum hisi_sas_phy_event event)907 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
908 enum hisi_sas_phy_event event)
909 {
910 struct hisi_hba *hisi_hba = phy->hisi_hba;
911
912 if (WARN_ON(event >= HISI_PHYES_NUM))
913 return false;
914
915 return queue_work(hisi_hba->wq, &phy->works[event]);
916 }
917 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
918
hisi_sas_wait_phyup_timedout(struct timer_list * t)919 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
920 {
921 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
922 struct hisi_hba *hisi_hba = phy->hisi_hba;
923 struct device *dev = hisi_hba->dev;
924 int phy_no = phy->sas_phy.id;
925
926 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
927 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
928 }
929
930 #define HISI_SAS_WAIT_PHYUP_RETRIES 10
931
hisi_sas_phy_oob_ready(struct hisi_hba * hisi_hba,int phy_no)932 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
933 {
934 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
935 struct device *dev = hisi_hba->dev;
936 unsigned long flags;
937
938 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
939 spin_lock_irqsave(&phy->lock, flags);
940 if (phy->phy_attached) {
941 spin_unlock_irqrestore(&phy->lock, flags);
942 return;
943 }
944
945 if (!timer_pending(&phy->timer)) {
946 if (phy->wait_phyup_cnt < HISI_SAS_WAIT_PHYUP_RETRIES) {
947 phy->wait_phyup_cnt++;
948 phy->timer.expires = jiffies +
949 HISI_SAS_WAIT_PHYUP_TIMEOUT;
950 add_timer(&phy->timer);
951 spin_unlock_irqrestore(&phy->lock, flags);
952 return;
953 }
954
955 dev_warn(dev, "phy%d failed to come up %d times, giving up\n",
956 phy_no, phy->wait_phyup_cnt);
957 phy->wait_phyup_cnt = 0;
958 }
959 spin_unlock_irqrestore(&phy->lock, flags);
960 }
961
962 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
963
hisi_sas_phy_init(struct hisi_hba * hisi_hba,int phy_no)964 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
965 {
966 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
967 struct asd_sas_phy *sas_phy = &phy->sas_phy;
968 int i;
969
970 phy->hisi_hba = hisi_hba;
971 phy->port = NULL;
972 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
973 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
974 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
975 sas_phy->class = SAS;
976 sas_phy->iproto = SAS_PROTOCOL_ALL;
977 sas_phy->tproto = 0;
978 sas_phy->type = PHY_TYPE_PHYSICAL;
979 sas_phy->role = PHY_ROLE_INITIATOR;
980 sas_phy->oob_mode = OOB_NOT_CONNECTED;
981 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
982 sas_phy->id = phy_no;
983 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
984 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
985 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
986 sas_phy->lldd_phy = phy;
987
988 for (i = 0; i < HISI_PHYES_NUM; i++)
989 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
990
991 spin_lock_init(&phy->lock);
992
993 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
994 }
995
996 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
hisi_sas_phy_enable(struct hisi_hba * hisi_hba,int phy_no,int enable)997 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
998 {
999 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1000 struct asd_sas_phy *aphy = &phy->sas_phy;
1001 struct sas_phy *sphy = aphy->phy;
1002 unsigned long flags;
1003
1004 spin_lock_irqsave(&phy->lock, flags);
1005
1006 if (enable) {
1007 /* We may have been enabled already; if so, don't touch */
1008 if (!phy->enable)
1009 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1010 hisi_hba->hw->phy_start(hisi_hba, phy_no);
1011 } else {
1012 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
1013 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1014 }
1015 phy->enable = enable;
1016 spin_unlock_irqrestore(&phy->lock, flags);
1017 }
1018 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
1019
hisi_sas_port_notify_formed(struct asd_sas_phy * sas_phy)1020 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
1021 {
1022 struct sas_ha_struct *sas_ha = sas_phy->ha;
1023 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1024 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
1025 struct asd_sas_port *sas_port = sas_phy->port;
1026 struct hisi_sas_port *port;
1027 unsigned long flags;
1028
1029 if (!sas_port)
1030 return;
1031
1032 port = to_hisi_sas_port(sas_port);
1033 spin_lock_irqsave(&hisi_hba->lock, flags);
1034 port->port_attached = 1;
1035 port->id = phy->port_id;
1036 phy->port = port;
1037 sas_port->lldd_port = port;
1038 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1039 }
1040
hisi_sas_do_release_task(struct hisi_hba * hisi_hba,struct sas_task * task,struct hisi_sas_slot * slot)1041 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1042 struct hisi_sas_slot *slot)
1043 {
1044 if (task) {
1045 unsigned long flags;
1046 struct task_status_struct *ts;
1047
1048 ts = &task->task_status;
1049
1050 ts->resp = SAS_TASK_COMPLETE;
1051 ts->stat = SAS_ABORTED_TASK;
1052 spin_lock_irqsave(&task->task_state_lock, flags);
1053 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
1054 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1055 task->task_state_flags |= SAS_TASK_STATE_DONE;
1056 spin_unlock_irqrestore(&task->task_state_lock, flags);
1057 }
1058
1059 hisi_sas_slot_task_free(hisi_hba, task, slot);
1060 }
1061
hisi_sas_release_task(struct hisi_hba * hisi_hba,struct domain_device * device)1062 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1063 struct domain_device *device)
1064 {
1065 struct hisi_sas_slot *slot, *slot2;
1066 struct hisi_sas_device *sas_dev = device->lldd_dev;
1067
1068 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1069 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1070 }
1071
hisi_sas_release_tasks(struct hisi_hba * hisi_hba)1072 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1073 {
1074 struct hisi_sas_device *sas_dev;
1075 struct domain_device *device;
1076 int i;
1077
1078 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1079 sas_dev = &hisi_hba->devices[i];
1080 device = sas_dev->sas_device;
1081
1082 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1083 !device)
1084 continue;
1085
1086 hisi_sas_release_task(hisi_hba, device);
1087 }
1088 }
1089 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1090
hisi_sas_dereg_device(struct hisi_hba * hisi_hba,struct domain_device * device)1091 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1092 struct domain_device *device)
1093 {
1094 if (hisi_hba->hw->dereg_device)
1095 hisi_hba->hw->dereg_device(hisi_hba, device);
1096 }
1097
1098 static int
hisi_sas_internal_task_abort_dev(struct hisi_sas_device * sas_dev,bool rst_ha_timeout)1099 hisi_sas_internal_task_abort_dev(struct hisi_sas_device *sas_dev,
1100 bool rst_ha_timeout)
1101 {
1102 struct hisi_sas_internal_abort_data data = { rst_ha_timeout };
1103 struct domain_device *device = sas_dev->sas_device;
1104 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1105 int i, rc;
1106
1107 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
1108 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1109 const struct cpumask *mask = cq->irq_mask;
1110
1111 if (mask && !cpumask_intersects(cpu_online_mask, mask))
1112 continue;
1113 rc = sas_execute_internal_abort_dev(device, i, &data);
1114 if (rc)
1115 return rc;
1116 }
1117
1118 return 0;
1119 }
1120
hisi_sas_dev_gone(struct domain_device * device)1121 static void hisi_sas_dev_gone(struct domain_device *device)
1122 {
1123 struct hisi_sas_device *sas_dev = device->lldd_dev;
1124 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1125 struct device *dev = hisi_hba->dev;
1126 int ret = 0;
1127
1128 dev_info(dev, "dev[%d:%x] is gone\n",
1129 sas_dev->device_id, sas_dev->dev_type);
1130
1131 down(&hisi_hba->sem);
1132 if (!test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1133 hisi_sas_internal_task_abort_dev(sas_dev, true);
1134
1135 hisi_sas_dereg_device(hisi_hba, device);
1136
1137 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1138 device->lldd_dev = NULL;
1139 }
1140
1141 if (hisi_hba->hw->free_device)
1142 hisi_hba->hw->free_device(sas_dev);
1143
1144 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1145 if (!ret)
1146 sas_dev->dev_type = SAS_PHY_UNUSED;
1147 sas_dev->sas_device = NULL;
1148 up(&hisi_hba->sem);
1149 }
1150
hisi_sas_phy_set_linkrate(struct hisi_hba * hisi_hba,int phy_no,struct sas_phy_linkrates * r)1151 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1152 struct sas_phy_linkrates *r)
1153 {
1154 struct sas_phy_linkrates _r;
1155
1156 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1157 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1158 enum sas_linkrate min, max;
1159
1160 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1161 return -EINVAL;
1162
1163 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1164 max = sas_phy->phy->maximum_linkrate;
1165 min = r->minimum_linkrate;
1166 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1167 max = r->maximum_linkrate;
1168 min = sas_phy->phy->minimum_linkrate;
1169 } else
1170 return -EINVAL;
1171
1172 _r.maximum_linkrate = max;
1173 _r.minimum_linkrate = min;
1174
1175 sas_phy->phy->maximum_linkrate = max;
1176 sas_phy->phy->minimum_linkrate = min;
1177
1178 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1179 msleep(100);
1180 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1181 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1182
1183 return 0;
1184 }
1185
hisi_sas_control_phy(struct asd_sas_phy * sas_phy,enum phy_func func,void * funcdata)1186 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1187 void *funcdata)
1188 {
1189 struct hisi_sas_phy *phy = container_of(sas_phy,
1190 struct hisi_sas_phy, sas_phy);
1191 struct sas_ha_struct *sas_ha = sas_phy->ha;
1192 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1193 struct device *dev = hisi_hba->dev;
1194 DECLARE_COMPLETION_ONSTACK(completion);
1195 int phy_no = sas_phy->id;
1196 u8 sts = phy->phy_attached;
1197 int ret = 0;
1198
1199 down(&hisi_hba->sem);
1200 phy->reset_completion = &completion;
1201
1202 switch (func) {
1203 case PHY_FUNC_HARD_RESET:
1204 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1205 break;
1206
1207 case PHY_FUNC_LINK_RESET:
1208 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1209 msleep(100);
1210 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1211 break;
1212
1213 case PHY_FUNC_DISABLE:
1214 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1215 goto out;
1216
1217 case PHY_FUNC_SET_LINK_RATE:
1218 ret = hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1219 break;
1220
1221 case PHY_FUNC_GET_EVENTS:
1222 if (hisi_hba->hw->get_events) {
1223 hisi_hba->hw->get_events(hisi_hba, phy_no);
1224 goto out;
1225 }
1226 fallthrough;
1227 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1228 default:
1229 ret = -EOPNOTSUPP;
1230 goto out;
1231 }
1232
1233 if (sts && !wait_for_completion_timeout(&completion,
1234 HISI_SAS_WAIT_PHYUP_TIMEOUT)) {
1235 dev_warn(dev, "phy%d wait phyup timed out for func %d\n",
1236 phy_no, func);
1237 if (phy->in_reset)
1238 ret = -ETIMEDOUT;
1239 }
1240
1241 out:
1242 phy->reset_completion = NULL;
1243
1244 up(&hisi_hba->sem);
1245 return ret;
1246 }
1247
hisi_sas_fill_ata_reset_cmd(struct ata_device * dev,bool reset,int pmp,u8 * fis)1248 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1249 bool reset, int pmp, u8 *fis)
1250 {
1251 struct ata_taskfile tf;
1252
1253 ata_tf_init(dev, &tf);
1254 if (reset)
1255 tf.ctl |= ATA_SRST;
1256 else
1257 tf.ctl &= ~ATA_SRST;
1258 tf.command = ATA_CMD_DEV_RESET;
1259 ata_tf_to_fis(&tf, pmp, 0, fis);
1260 }
1261
hisi_sas_softreset_ata_disk(struct domain_device * device)1262 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1263 {
1264 u8 fis[20] = {0};
1265 struct ata_port *ap = device->sata_dev.ap;
1266 struct ata_link *link;
1267 int rc = TMF_RESP_FUNC_FAILED;
1268 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1269 struct device *dev = hisi_hba->dev;
1270
1271 ata_for_each_link(link, ap, EDGE) {
1272 int pmp = sata_srst_pmp(link);
1273
1274 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1275 rc = sas_execute_ata_cmd(device, fis, -1);
1276 if (rc != TMF_RESP_FUNC_COMPLETE)
1277 break;
1278 }
1279
1280 if (rc == TMF_RESP_FUNC_COMPLETE) {
1281 ata_for_each_link(link, ap, EDGE) {
1282 int pmp = sata_srst_pmp(link);
1283
1284 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1285 rc = sas_execute_ata_cmd(device, fis, -1);
1286 if (rc != TMF_RESP_FUNC_COMPLETE)
1287 dev_err(dev, "ata disk %016llx de-reset failed\n",
1288 SAS_ADDR(device->sas_addr));
1289 }
1290 } else {
1291 dev_err(dev, "ata disk %016llx reset failed\n",
1292 SAS_ADDR(device->sas_addr));
1293 }
1294
1295 if (rc == TMF_RESP_FUNC_COMPLETE)
1296 hisi_sas_release_task(hisi_hba, device);
1297
1298 return rc;
1299 }
1300
hisi_sas_refresh_port_id(struct hisi_hba * hisi_hba)1301 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1302 {
1303 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1304 int i;
1305
1306 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1307 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1308 struct domain_device *device = sas_dev->sas_device;
1309 struct asd_sas_port *sas_port;
1310 struct hisi_sas_port *port;
1311 struct hisi_sas_phy *phy = NULL;
1312 struct asd_sas_phy *sas_phy;
1313
1314 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1315 || !device || !device->port)
1316 continue;
1317
1318 sas_port = device->port;
1319 port = to_hisi_sas_port(sas_port);
1320
1321 spin_lock(&sas_port->phy_list_lock);
1322 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1323 if (state & BIT(sas_phy->id)) {
1324 phy = sas_phy->lldd_phy;
1325 break;
1326 }
1327 spin_unlock(&sas_port->phy_list_lock);
1328
1329 if (phy) {
1330 port->id = phy->port_id;
1331
1332 /* Update linkrate of directly attached device. */
1333 if (!device->parent)
1334 device->linkrate = phy->sas_phy.linkrate;
1335
1336 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1337 } else if (!port->port_attached)
1338 port->id = 0xff;
1339 }
1340 }
1341
hisi_sas_rescan_topology(struct hisi_hba * hisi_hba,u32 state)1342 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1343 {
1344 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1345 struct asd_sas_port *_sas_port = NULL;
1346 int phy_no;
1347
1348 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1349 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1350 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1351 struct asd_sas_port *sas_port = sas_phy->port;
1352 bool do_port_check = _sas_port != sas_port;
1353
1354 if (!sas_phy->phy->enabled)
1355 continue;
1356
1357 /* Report PHY state change to libsas */
1358 if (state & BIT(phy_no)) {
1359 if (do_port_check && sas_port && sas_port->port_dev) {
1360 struct domain_device *dev = sas_port->port_dev;
1361
1362 _sas_port = sas_port;
1363
1364 if (dev_is_expander(dev->dev_type))
1365 sas_notify_port_event(sas_phy,
1366 PORTE_BROADCAST_RCVD,
1367 GFP_KERNEL);
1368 }
1369 } else {
1370 hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL);
1371 }
1372 }
1373 /*
1374 * Ensure any bcast events are processed prior to calling async nexus
1375 * reset calls from hisi_sas_clear_nexus_ha() ->
1376 * hisi_sas_async_I_T_nexus_reset()
1377 */
1378 sas_drain_work(sas_ha);
1379 }
1380
hisi_sas_reset_init_all_devices(struct hisi_hba * hisi_hba)1381 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1382 {
1383 struct hisi_sas_device *sas_dev;
1384 struct domain_device *device;
1385 int i;
1386
1387 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1388 sas_dev = &hisi_hba->devices[i];
1389 device = sas_dev->sas_device;
1390
1391 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1392 continue;
1393
1394 hisi_sas_init_device(device);
1395 }
1396 }
1397
hisi_sas_send_ata_reset_each_phy(struct hisi_hba * hisi_hba,struct asd_sas_port * sas_port,struct domain_device * device)1398 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1399 struct asd_sas_port *sas_port,
1400 struct domain_device *device)
1401 {
1402 struct ata_port *ap = device->sata_dev.ap;
1403 struct device *dev = hisi_hba->dev;
1404 int rc = TMF_RESP_FUNC_FAILED;
1405 struct ata_link *link;
1406 u8 fis[20] = {0};
1407 int i;
1408
1409 for (i = 0; i < hisi_hba->n_phy; i++) {
1410 if (!(sas_port->phy_mask & BIT(i)))
1411 continue;
1412
1413 ata_for_each_link(link, ap, EDGE) {
1414 int pmp = sata_srst_pmp(link);
1415
1416 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1417 rc = sas_execute_ata_cmd(device, fis, i);
1418 if (rc != TMF_RESP_FUNC_COMPLETE) {
1419 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1420 i, rc);
1421 break;
1422 }
1423 }
1424 }
1425 }
1426
hisi_sas_terminate_stp_reject(struct hisi_hba * hisi_hba)1427 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1428 {
1429 struct device *dev = hisi_hba->dev;
1430 int port_no, rc, i;
1431
1432 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1433 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1434 struct domain_device *device = sas_dev->sas_device;
1435
1436 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1437 continue;
1438
1439 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1440 if (rc < 0)
1441 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1442 }
1443
1444 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1445 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1446 struct asd_sas_port *sas_port = &port->sas_port;
1447 struct domain_device *port_dev = sas_port->port_dev;
1448 struct domain_device *device;
1449
1450 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1451 continue;
1452
1453 /* Try to find a SATA device */
1454 list_for_each_entry(device, &sas_port->dev_list,
1455 dev_list_node) {
1456 if (dev_is_sata(device)) {
1457 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1458 sas_port,
1459 device);
1460 break;
1461 }
1462 }
1463 }
1464 }
1465
hisi_sas_controller_reset_prepare(struct hisi_hba * hisi_hba)1466 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1467 {
1468 struct Scsi_Host *shost = hisi_hba->shost;
1469
1470 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1471
1472 scsi_block_requests(shost);
1473 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1474
1475 del_timer_sync(&hisi_hba->timer);
1476
1477 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1478 }
1479 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1480
hisi_sas_controller_reset_done(struct hisi_hba * hisi_hba)1481 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1482 {
1483 struct Scsi_Host *shost = hisi_hba->shost;
1484
1485 /* Init and wait for PHYs to come up and all libsas event finished. */
1486 hisi_hba->hw->phys_init(hisi_hba);
1487 msleep(1000);
1488 hisi_sas_refresh_port_id(hisi_hba);
1489 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1490
1491 if (hisi_hba->reject_stp_links_msk)
1492 hisi_sas_terminate_stp_reject(hisi_hba);
1493 hisi_sas_reset_init_all_devices(hisi_hba);
1494 scsi_unblock_requests(shost);
1495 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1496 up(&hisi_hba->sem);
1497
1498 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1499 }
1500 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1501
hisi_sas_controller_prereset(struct hisi_hba * hisi_hba)1502 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba)
1503 {
1504 if (!hisi_hba->hw->soft_reset)
1505 return -ENOENT;
1506
1507 down(&hisi_hba->sem);
1508 if (test_and_set_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1509 up(&hisi_hba->sem);
1510 return -EPERM;
1511 }
1512
1513 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1514 hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1515
1516 return 0;
1517 }
1518
hisi_sas_controller_reset(struct hisi_hba * hisi_hba)1519 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1520 {
1521 struct device *dev = hisi_hba->dev;
1522 struct Scsi_Host *shost = hisi_hba->shost;
1523 int rc;
1524
1525 dev_info(dev, "controller resetting...\n");
1526 hisi_sas_controller_reset_prepare(hisi_hba);
1527
1528 rc = hisi_hba->hw->soft_reset(hisi_hba);
1529 if (rc) {
1530 dev_warn(dev, "controller reset failed (%d)\n", rc);
1531 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1532 up(&hisi_hba->sem);
1533 scsi_unblock_requests(shost);
1534 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1535 return rc;
1536 }
1537 clear_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1538
1539 hisi_sas_controller_reset_done(hisi_hba);
1540 dev_info(dev, "controller reset complete\n");
1541
1542 return 0;
1543 }
1544
hisi_sas_abort_task(struct sas_task * task)1545 static int hisi_sas_abort_task(struct sas_task *task)
1546 {
1547 struct hisi_sas_internal_abort_data internal_abort_data = { false };
1548 struct domain_device *device = task->dev;
1549 struct hisi_sas_device *sas_dev = device->lldd_dev;
1550 struct hisi_hba *hisi_hba;
1551 struct device *dev;
1552 int rc = TMF_RESP_FUNC_FAILED;
1553 unsigned long flags;
1554
1555 if (!sas_dev)
1556 return TMF_RESP_FUNC_FAILED;
1557
1558 hisi_hba = dev_to_hisi_hba(task->dev);
1559 dev = hisi_hba->dev;
1560
1561 spin_lock_irqsave(&task->task_state_lock, flags);
1562 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1563 struct hisi_sas_slot *slot = task->lldd_task;
1564 struct hisi_sas_cq *cq;
1565
1566 if (slot) {
1567 /*
1568 * sync irq to avoid free'ing task
1569 * before using task in IO completion
1570 */
1571 cq = &hisi_hba->cq[slot->dlvry_queue];
1572 synchronize_irq(cq->irq_no);
1573 }
1574 spin_unlock_irqrestore(&task->task_state_lock, flags);
1575 rc = TMF_RESP_FUNC_COMPLETE;
1576 goto out;
1577 }
1578 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1579 spin_unlock_irqrestore(&task->task_state_lock, flags);
1580
1581 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1582 struct hisi_sas_slot *slot = task->lldd_task;
1583 u16 tag = slot->idx;
1584 int rc2;
1585
1586 rc = sas_abort_task(task, tag);
1587 rc2 = sas_execute_internal_abort_single(device, tag,
1588 slot->dlvry_queue, &internal_abort_data);
1589 if (rc2 < 0) {
1590 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1591 return TMF_RESP_FUNC_FAILED;
1592 }
1593
1594 /*
1595 * If the TMF finds that the IO is not in the device and also
1596 * the internal abort does not succeed, then it is safe to
1597 * free the slot.
1598 * Note: if the internal abort succeeds then the slot
1599 * will have already been completed
1600 */
1601 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1602 if (task->lldd_task)
1603 hisi_sas_do_release_task(hisi_hba, task, slot);
1604 }
1605 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1606 task->task_proto & SAS_PROTOCOL_STP) {
1607 if (task->dev->dev_type == SAS_SATA_DEV) {
1608 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1609 if (rc < 0) {
1610 dev_err(dev, "abort task: internal abort failed\n");
1611 goto out;
1612 }
1613 hisi_sas_dereg_device(hisi_hba, device);
1614 rc = hisi_sas_softreset_ata_disk(device);
1615 }
1616 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1617 /* SMP */
1618 struct hisi_sas_slot *slot = task->lldd_task;
1619 u32 tag = slot->idx;
1620 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1621
1622 rc = sas_execute_internal_abort_single(device,
1623 tag, slot->dlvry_queue,
1624 &internal_abort_data);
1625 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1626 task->lldd_task) {
1627 /*
1628 * sync irq to avoid free'ing task
1629 * before using task in IO completion
1630 */
1631 synchronize_irq(cq->irq_no);
1632 slot->task = NULL;
1633 }
1634 }
1635
1636 out:
1637 if (rc != TMF_RESP_FUNC_COMPLETE)
1638 dev_notice(dev, "abort task: rc=%d\n", rc);
1639 return rc;
1640 }
1641
hisi_sas_abort_task_set(struct domain_device * device,u8 * lun)1642 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1643 {
1644 struct hisi_sas_device *sas_dev = device->lldd_dev;
1645 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1646 struct device *dev = hisi_hba->dev;
1647 int rc;
1648
1649 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1650 if (rc < 0) {
1651 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1652 return TMF_RESP_FUNC_FAILED;
1653 }
1654 hisi_sas_dereg_device(hisi_hba, device);
1655
1656 rc = sas_abort_task_set(device, lun);
1657 if (rc == TMF_RESP_FUNC_COMPLETE)
1658 hisi_sas_release_task(hisi_hba, device);
1659
1660 return rc;
1661 }
1662
hisi_sas_debug_I_T_nexus_reset(struct domain_device * device)1663 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1664 {
1665 struct sas_phy *local_phy = sas_get_local_phy(device);
1666 struct hisi_sas_device *sas_dev = device->lldd_dev;
1667 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1668 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1669 int rc, reset_type;
1670
1671 if (!local_phy->enabled) {
1672 sas_put_local_phy(local_phy);
1673 return -ENODEV;
1674 }
1675
1676 if (scsi_is_sas_phy_local(local_phy)) {
1677 struct asd_sas_phy *sas_phy =
1678 sas_ha->sas_phy[local_phy->number];
1679 struct hisi_sas_phy *phy =
1680 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1681 unsigned long flags;
1682
1683 spin_lock_irqsave(&phy->lock, flags);
1684 phy->in_reset = 1;
1685 spin_unlock_irqrestore(&phy->lock, flags);
1686 }
1687
1688 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1689 !dev_is_sata(device)) ? true : false;
1690
1691 rc = sas_phy_reset(local_phy, reset_type);
1692 sas_put_local_phy(local_phy);
1693
1694 if (scsi_is_sas_phy_local(local_phy)) {
1695 struct asd_sas_phy *sas_phy =
1696 sas_ha->sas_phy[local_phy->number];
1697 struct hisi_sas_phy *phy =
1698 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1699 unsigned long flags;
1700
1701 spin_lock_irqsave(&phy->lock, flags);
1702 phy->in_reset = 0;
1703 spin_unlock_irqrestore(&phy->lock, flags);
1704
1705 /* report PHY down if timed out */
1706 if (rc == -ETIMEDOUT)
1707 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL);
1708 return rc;
1709 }
1710
1711 /* Remote phy */
1712 if (rc)
1713 return rc;
1714
1715 if (dev_is_sata(device)) {
1716 struct ata_link *link = &device->sata_dev.ap->link;
1717
1718 rc = ata_wait_after_reset(link, HISI_SAS_WAIT_PHYUP_TIMEOUT,
1719 smp_ata_check_ready_type);
1720 } else {
1721 msleep(2000);
1722 }
1723
1724 return rc;
1725 }
1726
hisi_sas_I_T_nexus_reset(struct domain_device * device)1727 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1728 {
1729 struct hisi_sas_device *sas_dev = device->lldd_dev;
1730 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1731 struct device *dev = hisi_hba->dev;
1732 int rc;
1733
1734 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1735 if (rc < 0) {
1736 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1737 return TMF_RESP_FUNC_FAILED;
1738 }
1739 hisi_sas_dereg_device(hisi_hba, device);
1740
1741 rc = hisi_sas_debug_I_T_nexus_reset(device);
1742 if (rc == TMF_RESP_FUNC_COMPLETE && dev_is_sata(device)) {
1743 struct sas_phy *local_phy;
1744
1745 rc = hisi_sas_softreset_ata_disk(device);
1746 switch (rc) {
1747 case -ECOMM:
1748 rc = -ENODEV;
1749 break;
1750 case TMF_RESP_FUNC_FAILED:
1751 case -EMSGSIZE:
1752 case -EIO:
1753 local_phy = sas_get_local_phy(device);
1754 rc = sas_phy_enable(local_phy, 0);
1755 if (!rc) {
1756 local_phy->enabled = 0;
1757 dev_err(dev, "Disabled local phy of ATA disk %016llx due to softreset fail (%d)\n",
1758 SAS_ADDR(device->sas_addr), rc);
1759 rc = -ENODEV;
1760 }
1761 sas_put_local_phy(local_phy);
1762 break;
1763 default:
1764 break;
1765 }
1766 }
1767
1768 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1769 hisi_sas_release_task(hisi_hba, device);
1770
1771 return rc;
1772 }
1773
hisi_sas_lu_reset(struct domain_device * device,u8 * lun)1774 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1775 {
1776 struct hisi_sas_device *sas_dev = device->lldd_dev;
1777 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1778 struct device *dev = hisi_hba->dev;
1779 int rc = TMF_RESP_FUNC_FAILED;
1780
1781 /* Clear internal IO and then lu reset */
1782 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1783 if (rc < 0) {
1784 dev_err(dev, "lu_reset: internal abort failed\n");
1785 goto out;
1786 }
1787 hisi_sas_dereg_device(hisi_hba, device);
1788
1789 if (dev_is_sata(device)) {
1790 struct sas_phy *phy;
1791
1792 phy = sas_get_local_phy(device);
1793
1794 rc = sas_phy_reset(phy, true);
1795
1796 if (rc == 0)
1797 hisi_sas_release_task(hisi_hba, device);
1798 sas_put_local_phy(phy);
1799 } else {
1800 rc = sas_lu_reset(device, lun);
1801 if (rc == TMF_RESP_FUNC_COMPLETE)
1802 hisi_sas_release_task(hisi_hba, device);
1803 }
1804 out:
1805 if (rc != TMF_RESP_FUNC_COMPLETE)
1806 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1807 sas_dev->device_id, rc);
1808 return rc;
1809 }
1810
hisi_sas_async_I_T_nexus_reset(void * data,async_cookie_t cookie)1811 static void hisi_sas_async_I_T_nexus_reset(void *data, async_cookie_t cookie)
1812 {
1813 struct domain_device *device = data;
1814 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1815 int rc;
1816
1817 rc = hisi_sas_debug_I_T_nexus_reset(device);
1818 if (rc != TMF_RESP_FUNC_COMPLETE)
1819 dev_info(hisi_hba->dev, "I_T_nexus reset fail for dev:%016llx rc=%d\n",
1820 SAS_ADDR(device->sas_addr), rc);
1821 }
1822
hisi_sas_clear_nexus_ha(struct sas_ha_struct * sas_ha)1823 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1824 {
1825 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1826 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1827 ASYNC_DOMAIN_EXCLUSIVE(async);
1828 int i, ret;
1829
1830 queue_work(hisi_hba->wq, &r.work);
1831 wait_for_completion(r.completion);
1832 if (!r.done) {
1833 ret = TMF_RESP_FUNC_FAILED;
1834 goto out;
1835 }
1836
1837 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1838 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1839 struct domain_device *device = sas_dev->sas_device;
1840
1841 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1842 dev_is_expander(device->dev_type))
1843 continue;
1844
1845 async_schedule_domain(hisi_sas_async_I_T_nexus_reset,
1846 device, &async);
1847 }
1848
1849 async_synchronize_full_domain(&async);
1850 hisi_sas_release_tasks(hisi_hba);
1851
1852 ret = TMF_RESP_FUNC_COMPLETE;
1853 out:
1854 return ret;
1855 }
1856
hisi_sas_query_task(struct sas_task * task)1857 static int hisi_sas_query_task(struct sas_task *task)
1858 {
1859 int rc = TMF_RESP_FUNC_FAILED;
1860
1861 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1862 struct hisi_sas_slot *slot = task->lldd_task;
1863 u32 tag = slot->idx;
1864
1865 rc = sas_query_task(task, tag);
1866 switch (rc) {
1867 /* The task is still in Lun, release it then */
1868 case TMF_RESP_FUNC_SUCC:
1869 /* The task is not in Lun or failed, reset the phy */
1870 case TMF_RESP_FUNC_FAILED:
1871 case TMF_RESP_FUNC_COMPLETE:
1872 break;
1873 default:
1874 rc = TMF_RESP_FUNC_FAILED;
1875 break;
1876 }
1877 }
1878 return rc;
1879 }
1880
hisi_sas_internal_abort_timeout(struct sas_task * task,void * data)1881 static bool hisi_sas_internal_abort_timeout(struct sas_task *task,
1882 void *data)
1883 {
1884 struct domain_device *device = task->dev;
1885 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1886 struct hisi_sas_internal_abort_data *timeout = data;
1887
1888 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1889 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1890
1891 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1892 pr_err("Internal abort: timeout %016llx\n",
1893 SAS_ADDR(device->sas_addr));
1894 } else {
1895 struct hisi_sas_slot *slot = task->lldd_task;
1896
1897 set_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1898
1899 if (slot) {
1900 struct hisi_sas_cq *cq =
1901 &hisi_hba->cq[slot->dlvry_queue];
1902 /*
1903 * sync irq to avoid free'ing task
1904 * before using task in IO completion
1905 */
1906 synchronize_irq(cq->irq_no);
1907 slot->task = NULL;
1908 }
1909
1910 if (timeout->rst_ha_timeout) {
1911 pr_err("Internal abort: timeout and not done %016llx. Queuing reset.\n",
1912 SAS_ADDR(device->sas_addr));
1913 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1914 } else {
1915 pr_err("Internal abort: timeout and not done %016llx.\n",
1916 SAS_ADDR(device->sas_addr));
1917 }
1918
1919 return true;
1920 }
1921
1922 return false;
1923 }
1924
hisi_sas_port_formed(struct asd_sas_phy * sas_phy)1925 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1926 {
1927 hisi_sas_port_notify_formed(sas_phy);
1928 }
1929
hisi_sas_write_gpio(struct sas_ha_struct * sha,u8 reg_type,u8 reg_index,u8 reg_count,u8 * write_data)1930 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1931 u8 reg_index, u8 reg_count, u8 *write_data)
1932 {
1933 struct hisi_hba *hisi_hba = sha->lldd_ha;
1934
1935 if (!hisi_hba->hw->write_gpio)
1936 return -EOPNOTSUPP;
1937
1938 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1939 reg_index, reg_count, write_data);
1940 }
1941
hisi_sas_phy_disconnected(struct hisi_sas_phy * phy)1942 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1943 {
1944 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1945 struct sas_phy *sphy = sas_phy->phy;
1946 unsigned long flags;
1947
1948 phy->phy_attached = 0;
1949 phy->phy_type = 0;
1950 phy->port = NULL;
1951
1952 spin_lock_irqsave(&phy->lock, flags);
1953 if (phy->enable)
1954 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1955 else
1956 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
1957 spin_unlock_irqrestore(&phy->lock, flags);
1958 }
1959
hisi_sas_phy_down(struct hisi_hba * hisi_hba,int phy_no,int rdy,gfp_t gfp_flags)1960 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy,
1961 gfp_t gfp_flags)
1962 {
1963 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1964 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1965 struct device *dev = hisi_hba->dev;
1966
1967 if (rdy) {
1968 /* Phy down but ready */
1969 hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags);
1970 hisi_sas_port_notify_formed(sas_phy);
1971 } else {
1972 struct hisi_sas_port *port = phy->port;
1973
1974 if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags) ||
1975 phy->in_reset) {
1976 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
1977 return;
1978 }
1979 /* Phy down and not ready */
1980 sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags);
1981 sas_phy_disconnected(sas_phy);
1982
1983 if (port) {
1984 if (phy->phy_type & PORT_TYPE_SAS) {
1985 int port_id = port->id;
1986
1987 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1988 port_id))
1989 port->port_attached = 0;
1990 } else if (phy->phy_type & PORT_TYPE_SATA)
1991 port->port_attached = 0;
1992 }
1993 hisi_sas_phy_disconnected(phy);
1994 }
1995 }
1996 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1997
hisi_sas_phy_bcast(struct hisi_sas_phy * phy)1998 void hisi_sas_phy_bcast(struct hisi_sas_phy *phy)
1999 {
2000 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2001 struct hisi_hba *hisi_hba = phy->hisi_hba;
2002 struct sas_ha_struct *sha = &hisi_hba->sha;
2003
2004 if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags))
2005 return;
2006
2007 if (test_bit(SAS_HA_FROZEN, &sha->state))
2008 return;
2009
2010 sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD, GFP_ATOMIC);
2011 }
2012 EXPORT_SYMBOL_GPL(hisi_sas_phy_bcast);
2013
hisi_sas_sync_irqs(struct hisi_hba * hisi_hba)2014 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2015 {
2016 int i;
2017
2018 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2019 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2020
2021 synchronize_irq(cq->irq_no);
2022 }
2023 }
2024 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2025
hisi_sas_host_reset(struct Scsi_Host * shost,int reset_type)2026 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2027 {
2028 struct hisi_hba *hisi_hba = shost_priv(shost);
2029
2030 if (reset_type != SCSI_ADAPTER_RESET)
2031 return -EOPNOTSUPP;
2032
2033 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2034
2035 return 0;
2036 }
2037 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2038
2039 struct scsi_transport_template *hisi_sas_stt;
2040 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2041
2042 static struct sas_domain_function_template hisi_sas_transport_ops = {
2043 .lldd_dev_found = hisi_sas_dev_found,
2044 .lldd_dev_gone = hisi_sas_dev_gone,
2045 .lldd_execute_task = hisi_sas_queue_command,
2046 .lldd_control_phy = hisi_sas_control_phy,
2047 .lldd_abort_task = hisi_sas_abort_task,
2048 .lldd_abort_task_set = hisi_sas_abort_task_set,
2049 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2050 .lldd_lu_reset = hisi_sas_lu_reset,
2051 .lldd_query_task = hisi_sas_query_task,
2052 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2053 .lldd_port_formed = hisi_sas_port_formed,
2054 .lldd_write_gpio = hisi_sas_write_gpio,
2055 .lldd_tmf_aborted = hisi_sas_tmf_aborted,
2056 .lldd_abort_timeout = hisi_sas_internal_abort_timeout,
2057 };
2058
hisi_sas_init_mem(struct hisi_hba * hisi_hba)2059 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2060 {
2061 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2062 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2063
2064 for (i = 0; i < hisi_hba->queue_count; i++) {
2065 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2066 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2067 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2068
2069 s = sizeof(struct hisi_sas_cmd_hdr);
2070 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2071 memset(&cmd_hdr[j], 0, s);
2072
2073 dq->wr_point = 0;
2074
2075 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2076 memset(hisi_hba->complete_hdr[i], 0, s);
2077 cq->rd_point = 0;
2078 }
2079
2080 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2081 memset(hisi_hba->initial_fis, 0, s);
2082
2083 s = max_command_entries * sizeof(struct hisi_sas_iost);
2084 memset(hisi_hba->iost, 0, s);
2085
2086 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2087 memset(hisi_hba->breakpoint, 0, s);
2088
2089 s = sizeof(struct hisi_sas_sata_breakpoint);
2090 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2091 memset(&sata_breakpoint[j], 0, s);
2092 }
2093 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2094
hisi_sas_alloc(struct hisi_hba * hisi_hba)2095 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2096 {
2097 struct device *dev = hisi_hba->dev;
2098 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2099 int max_command_entries_ru, sz_slot_buf_ru;
2100 int blk_cnt, slots_per_blk;
2101
2102 sema_init(&hisi_hba->sem, 1);
2103 spin_lock_init(&hisi_hba->lock);
2104 for (i = 0; i < hisi_hba->n_phy; i++) {
2105 hisi_sas_phy_init(hisi_hba, i);
2106 hisi_hba->port[i].port_attached = 0;
2107 hisi_hba->port[i].id = -1;
2108 }
2109
2110 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2111 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2112 hisi_hba->devices[i].device_id = i;
2113 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2114 }
2115
2116 for (i = 0; i < hisi_hba->queue_count; i++) {
2117 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2118 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2119
2120 /* Completion queue structure */
2121 cq->id = i;
2122 cq->hisi_hba = hisi_hba;
2123
2124 /* Delivery queue structure */
2125 spin_lock_init(&dq->lock);
2126 INIT_LIST_HEAD(&dq->list);
2127 dq->id = i;
2128 dq->hisi_hba = hisi_hba;
2129
2130 /* Delivery queue */
2131 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2132 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2133 &hisi_hba->cmd_hdr_dma[i],
2134 GFP_KERNEL);
2135 if (!hisi_hba->cmd_hdr[i])
2136 goto err_out;
2137
2138 /* Completion queue */
2139 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2140 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2141 &hisi_hba->complete_hdr_dma[i],
2142 GFP_KERNEL);
2143 if (!hisi_hba->complete_hdr[i])
2144 goto err_out;
2145 }
2146
2147 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2148 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2149 GFP_KERNEL);
2150 if (!hisi_hba->itct)
2151 goto err_out;
2152
2153 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2154 sizeof(struct hisi_sas_slot),
2155 GFP_KERNEL);
2156 if (!hisi_hba->slot_info)
2157 goto err_out;
2158
2159 /* roundup to avoid overly large block size */
2160 max_command_entries_ru = roundup(max_command_entries, 64);
2161 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2162 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2163 else
2164 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2165 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2166 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2167 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2168 slots_per_blk = s / sz_slot_buf_ru;
2169
2170 for (i = 0; i < blk_cnt; i++) {
2171 int slot_index = i * slots_per_blk;
2172 dma_addr_t buf_dma;
2173 void *buf;
2174
2175 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2176 GFP_KERNEL);
2177 if (!buf)
2178 goto err_out;
2179
2180 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2181 struct hisi_sas_slot *slot;
2182
2183 slot = &hisi_hba->slot_info[slot_index];
2184 slot->buf = buf;
2185 slot->buf_dma = buf_dma;
2186 slot->idx = slot_index;
2187
2188 buf += sz_slot_buf_ru;
2189 buf_dma += sz_slot_buf_ru;
2190 }
2191 }
2192
2193 s = max_command_entries * sizeof(struct hisi_sas_iost);
2194 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2195 GFP_KERNEL);
2196 if (!hisi_hba->iost)
2197 goto err_out;
2198
2199 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2200 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2201 &hisi_hba->breakpoint_dma,
2202 GFP_KERNEL);
2203 if (!hisi_hba->breakpoint)
2204 goto err_out;
2205
2206 s = hisi_hba->slot_index_count = max_command_entries;
2207 hisi_hba->slot_index_tags = devm_bitmap_zalloc(dev, s, GFP_KERNEL);
2208 if (!hisi_hba->slot_index_tags)
2209 goto err_out;
2210
2211 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2212 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2213 &hisi_hba->initial_fis_dma,
2214 GFP_KERNEL);
2215 if (!hisi_hba->initial_fis)
2216 goto err_out;
2217
2218 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2219 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2220 &hisi_hba->sata_breakpoint_dma,
2221 GFP_KERNEL);
2222 if (!hisi_hba->sata_breakpoint)
2223 goto err_out;
2224
2225 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2226
2227 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2228 if (!hisi_hba->wq) {
2229 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2230 goto err_out;
2231 }
2232
2233 return 0;
2234 err_out:
2235 return -ENOMEM;
2236 }
2237 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2238
hisi_sas_free(struct hisi_hba * hisi_hba)2239 void hisi_sas_free(struct hisi_hba *hisi_hba)
2240 {
2241 int i;
2242
2243 for (i = 0; i < hisi_hba->n_phy; i++) {
2244 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2245
2246 del_timer_sync(&phy->timer);
2247 }
2248
2249 if (hisi_hba->wq)
2250 destroy_workqueue(hisi_hba->wq);
2251 }
2252 EXPORT_SYMBOL_GPL(hisi_sas_free);
2253
hisi_sas_rst_work_handler(struct work_struct * work)2254 void hisi_sas_rst_work_handler(struct work_struct *work)
2255 {
2256 struct hisi_hba *hisi_hba =
2257 container_of(work, struct hisi_hba, rst_work);
2258
2259 if (hisi_sas_controller_prereset(hisi_hba))
2260 return;
2261
2262 hisi_sas_controller_reset(hisi_hba);
2263 }
2264 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2265
hisi_sas_sync_rst_work_handler(struct work_struct * work)2266 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2267 {
2268 struct hisi_sas_rst *rst =
2269 container_of(work, struct hisi_sas_rst, work);
2270
2271 if (hisi_sas_controller_prereset(rst->hisi_hba))
2272 goto rst_complete;
2273
2274 if (!hisi_sas_controller_reset(rst->hisi_hba))
2275 rst->done = true;
2276 rst_complete:
2277 complete(rst->completion);
2278 }
2279 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2280
hisi_sas_get_fw_info(struct hisi_hba * hisi_hba)2281 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2282 {
2283 struct device *dev = hisi_hba->dev;
2284 struct platform_device *pdev = hisi_hba->platform_dev;
2285 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2286 struct clk *refclk;
2287
2288 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2289 SAS_ADDR_SIZE)) {
2290 dev_err(dev, "could not get property sas-addr\n");
2291 return -ENOENT;
2292 }
2293
2294 if (np) {
2295 /*
2296 * These properties are only required for platform device-based
2297 * controller with DT firmware.
2298 */
2299 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2300 "hisilicon,sas-syscon");
2301 if (IS_ERR(hisi_hba->ctrl)) {
2302 dev_err(dev, "could not get syscon\n");
2303 return -ENOENT;
2304 }
2305
2306 if (device_property_read_u32(dev, "ctrl-reset-reg",
2307 &hisi_hba->ctrl_reset_reg)) {
2308 dev_err(dev, "could not get property ctrl-reset-reg\n");
2309 return -ENOENT;
2310 }
2311
2312 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2313 &hisi_hba->ctrl_reset_sts_reg)) {
2314 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2315 return -ENOENT;
2316 }
2317
2318 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2319 &hisi_hba->ctrl_clock_ena_reg)) {
2320 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2321 return -ENOENT;
2322 }
2323 }
2324
2325 refclk = devm_clk_get(dev, NULL);
2326 if (IS_ERR(refclk))
2327 dev_dbg(dev, "no ref clk property\n");
2328 else
2329 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2330
2331 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2332 dev_err(dev, "could not get property phy-count\n");
2333 return -ENOENT;
2334 }
2335
2336 if (device_property_read_u32(dev, "queue-count",
2337 &hisi_hba->queue_count)) {
2338 dev_err(dev, "could not get property queue-count\n");
2339 return -ENOENT;
2340 }
2341
2342 return 0;
2343 }
2344 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2345
hisi_sas_shost_alloc(struct platform_device * pdev,const struct hisi_sas_hw * hw)2346 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2347 const struct hisi_sas_hw *hw)
2348 {
2349 struct resource *res;
2350 struct Scsi_Host *shost;
2351 struct hisi_hba *hisi_hba;
2352 struct device *dev = &pdev->dev;
2353 int error;
2354
2355 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2356 if (!shost) {
2357 dev_err(dev, "scsi host alloc failed\n");
2358 return NULL;
2359 }
2360 hisi_hba = shost_priv(shost);
2361
2362 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2363 hisi_hba->hw = hw;
2364 hisi_hba->dev = dev;
2365 hisi_hba->platform_dev = pdev;
2366 hisi_hba->shost = shost;
2367 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2368
2369 timer_setup(&hisi_hba->timer, NULL, 0);
2370
2371 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2372 goto err_out;
2373
2374 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2375 if (error) {
2376 dev_err(dev, "No usable DMA addressing method\n");
2377 goto err_out;
2378 }
2379
2380 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2381 if (IS_ERR(hisi_hba->regs))
2382 goto err_out;
2383
2384 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2385 if (res) {
2386 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2387 if (IS_ERR(hisi_hba->sgpio_regs))
2388 goto err_out;
2389 }
2390
2391 if (hisi_sas_alloc(hisi_hba)) {
2392 hisi_sas_free(hisi_hba);
2393 goto err_out;
2394 }
2395
2396 return shost;
2397 err_out:
2398 scsi_host_put(shost);
2399 dev_err(dev, "shost alloc failed\n");
2400 return NULL;
2401 }
2402
hisi_sas_interrupt_preinit(struct hisi_hba * hisi_hba)2403 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba)
2404 {
2405 if (hisi_hba->hw->interrupt_preinit)
2406 return hisi_hba->hw->interrupt_preinit(hisi_hba);
2407 return 0;
2408 }
2409
hisi_sas_probe(struct platform_device * pdev,const struct hisi_sas_hw * hw)2410 int hisi_sas_probe(struct platform_device *pdev,
2411 const struct hisi_sas_hw *hw)
2412 {
2413 struct Scsi_Host *shost;
2414 struct hisi_hba *hisi_hba;
2415 struct device *dev = &pdev->dev;
2416 struct asd_sas_phy **arr_phy;
2417 struct asd_sas_port **arr_port;
2418 struct sas_ha_struct *sha;
2419 int rc, phy_nr, port_nr, i;
2420
2421 shost = hisi_sas_shost_alloc(pdev, hw);
2422 if (!shost)
2423 return -ENOMEM;
2424
2425 sha = SHOST_TO_SAS_HA(shost);
2426 hisi_hba = shost_priv(shost);
2427 platform_set_drvdata(pdev, sha);
2428
2429 phy_nr = port_nr = hisi_hba->n_phy;
2430
2431 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2432 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2433 if (!arr_phy || !arr_port) {
2434 rc = -ENOMEM;
2435 goto err_out_ha;
2436 }
2437
2438 sha->sas_phy = arr_phy;
2439 sha->sas_port = arr_port;
2440 sha->lldd_ha = hisi_hba;
2441
2442 shost->transportt = hisi_sas_stt;
2443 shost->max_id = HISI_SAS_MAX_DEVICES;
2444 shost->max_lun = ~0;
2445 shost->max_channel = 1;
2446 shost->max_cmd_len = 16;
2447 if (hisi_hba->hw->slot_index_alloc) {
2448 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2449 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2450 } else {
2451 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2452 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2453 }
2454
2455 sha->sas_ha_name = DRV_NAME;
2456 sha->dev = hisi_hba->dev;
2457 sha->lldd_module = THIS_MODULE;
2458 sha->sas_addr = &hisi_hba->sas_addr[0];
2459 sha->num_phys = hisi_hba->n_phy;
2460 sha->core.shost = hisi_hba->shost;
2461
2462 for (i = 0; i < hisi_hba->n_phy; i++) {
2463 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2464 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2465 }
2466
2467 rc = hisi_sas_interrupt_preinit(hisi_hba);
2468 if (rc)
2469 goto err_out_ha;
2470
2471 rc = scsi_add_host(shost, &pdev->dev);
2472 if (rc)
2473 goto err_out_ha;
2474
2475 rc = sas_register_ha(sha);
2476 if (rc)
2477 goto err_out_register_ha;
2478
2479 rc = hisi_hba->hw->hw_init(hisi_hba);
2480 if (rc)
2481 goto err_out_hw_init;
2482
2483 scsi_scan_host(shost);
2484
2485 return 0;
2486
2487 err_out_hw_init:
2488 sas_unregister_ha(sha);
2489 err_out_register_ha:
2490 scsi_remove_host(shost);
2491 err_out_ha:
2492 hisi_sas_free(hisi_hba);
2493 scsi_host_put(shost);
2494 return rc;
2495 }
2496 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2497
hisi_sas_remove(struct platform_device * pdev)2498 int hisi_sas_remove(struct platform_device *pdev)
2499 {
2500 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2501 struct hisi_hba *hisi_hba = sha->lldd_ha;
2502 struct Scsi_Host *shost = sha->core.shost;
2503
2504 del_timer_sync(&hisi_hba->timer);
2505
2506 sas_unregister_ha(sha);
2507 sas_remove_host(sha->core.shost);
2508
2509 hisi_sas_free(hisi_hba);
2510 scsi_host_put(shost);
2511 return 0;
2512 }
2513 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2514
2515 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2516 #define DEBUGFS_ENABLE_DEFAULT "enabled"
2517 bool hisi_sas_debugfs_enable = true;
2518 u32 hisi_sas_debugfs_dump_count = 50;
2519 #else
2520 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2521 bool hisi_sas_debugfs_enable;
2522 u32 hisi_sas_debugfs_dump_count = 1;
2523 #endif
2524
2525 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2526 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2527 MODULE_PARM_DESC(hisi_sas_debugfs_enable,
2528 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")");
2529
2530 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
2531 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
2532 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
2533
2534 struct dentry *hisi_sas_debugfs_dir;
2535 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir);
2536
hisi_sas_init(void)2537 static __init int hisi_sas_init(void)
2538 {
2539 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2540 if (!hisi_sas_stt)
2541 return -ENOMEM;
2542
2543 if (hisi_sas_debugfs_enable) {
2544 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2545 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
2546 pr_info("hisi_sas: Limiting debugfs dump count\n");
2547 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
2548 }
2549 }
2550
2551 return 0;
2552 }
2553
hisi_sas_exit(void)2554 static __exit void hisi_sas_exit(void)
2555 {
2556 sas_release_transport(hisi_sas_stt);
2557
2558 debugfs_remove(hisi_sas_debugfs_dir);
2559 }
2560
2561 module_init(hisi_sas_init);
2562 module_exit(hisi_sas_exit);
2563
2564 MODULE_LICENSE("GPL");
2565 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2566 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2567 MODULE_ALIAS("platform:" DRV_NAME);
2568