]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: hisi_sas: make return type of prep functions void
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
1 /*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12 #include "hisi_sas.h"
13 #define DRV_NAME "hisi_sas"
14
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
20 static int
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26 void *funcdata);
27
28 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
29 {
30 switch (fis->command) {
31 case ATA_CMD_FPDMA_WRITE:
32 case ATA_CMD_FPDMA_READ:
33 case ATA_CMD_FPDMA_RECV:
34 case ATA_CMD_FPDMA_SEND:
35 case ATA_CMD_NCQ_NON_DATA:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA;
37
38 case ATA_CMD_DOWNLOAD_MICRO:
39 case ATA_CMD_ID_ATA:
40 case ATA_CMD_PMP_READ:
41 case ATA_CMD_READ_LOG_EXT:
42 case ATA_CMD_PIO_READ:
43 case ATA_CMD_PIO_READ_EXT:
44 case ATA_CMD_PMP_WRITE:
45 case ATA_CMD_WRITE_LOG_EXT:
46 case ATA_CMD_PIO_WRITE:
47 case ATA_CMD_PIO_WRITE_EXT:
48 return HISI_SAS_SATA_PROTOCOL_PIO;
49
50 case ATA_CMD_DSM:
51 case ATA_CMD_DOWNLOAD_MICRO_DMA:
52 case ATA_CMD_PMP_READ_DMA:
53 case ATA_CMD_PMP_WRITE_DMA:
54 case ATA_CMD_READ:
55 case ATA_CMD_READ_EXT:
56 case ATA_CMD_READ_LOG_DMA_EXT:
57 case ATA_CMD_READ_STREAM_DMA_EXT:
58 case ATA_CMD_TRUSTED_RCV_DMA:
59 case ATA_CMD_TRUSTED_SND_DMA:
60 case ATA_CMD_WRITE:
61 case ATA_CMD_WRITE_EXT:
62 case ATA_CMD_WRITE_FUA_EXT:
63 case ATA_CMD_WRITE_QUEUED:
64 case ATA_CMD_WRITE_LOG_DMA_EXT:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT:
66 case ATA_CMD_ZAC_MGMT_IN:
67 return HISI_SAS_SATA_PROTOCOL_DMA;
68
69 case ATA_CMD_CHK_POWER:
70 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_EDD:
72 case ATA_CMD_FLUSH:
73 case ATA_CMD_FLUSH_EXT:
74 case ATA_CMD_VERIFY:
75 case ATA_CMD_VERIFY_EXT:
76 case ATA_CMD_SET_FEATURES:
77 case ATA_CMD_STANDBY:
78 case ATA_CMD_STANDBYNOW1:
79 case ATA_CMD_ZAC_MGMT_OUT:
80 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 default:
82 {
83 if (fis->command == ATA_CMD_SET_MAX) {
84 switch (fis->features) {
85 case ATA_SET_MAX_PASSWD:
86 case ATA_SET_MAX_LOCK:
87 return HISI_SAS_SATA_PROTOCOL_PIO;
88
89 case ATA_SET_MAX_PASSWD_DMA:
90 case ATA_SET_MAX_UNLOCK_DMA:
91 return HISI_SAS_SATA_PROTOCOL_DMA;
92
93 default:
94 return HISI_SAS_SATA_PROTOCOL_NONDATA;
95 }
96 }
97 if (direction == DMA_NONE)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA;
99 return HISI_SAS_SATA_PROTOCOL_PIO;
100 }
101 }
102 }
103 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104
105 void hisi_sas_sata_done(struct sas_task *task,
106 struct hisi_sas_slot *slot)
107 {
108 struct task_status_struct *ts = &task->task_status;
109 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
110 struct hisi_sas_status_buffer *status_buf =
111 hisi_sas_status_buf_addr_mem(slot);
112 u8 *iu = &status_buf->iu[0];
113 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
114
115 resp->frame_len = sizeof(struct dev_to_host_fis);
116 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117
118 ts->buf_valid_size = sizeof(*resp);
119 }
120 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
121
122 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
123 {
124 struct ata_queued_cmd *qc = task->uldd_task;
125
126 if (qc) {
127 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
128 qc->tf.command == ATA_CMD_FPDMA_READ) {
129 *tag = qc->tag;
130 return 1;
131 }
132 }
133 return 0;
134 }
135 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
136
137 /*
138 * This function assumes linkrate mask fits in 8 bits, which it
139 * does for all HW versions supported.
140 */
141 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
142 {
143 u16 rate = 0;
144 int i;
145
146 max -= SAS_LINK_RATE_1_5_GBPS;
147 for (i = 0; i <= max; i++)
148 rate |= 1 << (i * 2);
149 return rate;
150 }
151 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
152
153 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
154 {
155 return device->port->ha->lldd_ha;
156 }
157
158 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
159 {
160 return container_of(sas_port, struct hisi_sas_port, sas_port);
161 }
162 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
163
164 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
165 {
166 int phy_no;
167
168 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
169 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
170 }
171 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
172
173 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
174 {
175 void *bitmap = hisi_hba->slot_index_tags;
176
177 clear_bit(slot_idx, bitmap);
178 }
179
180 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
181 {
182 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
183 }
184
185 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
186 {
187 void *bitmap = hisi_hba->slot_index_tags;
188
189 set_bit(slot_idx, bitmap);
190 }
191
192 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
193 {
194 unsigned int index;
195 void *bitmap = hisi_hba->slot_index_tags;
196
197 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
198 if (index >= hisi_hba->slot_index_count)
199 return -SAS_QUEUE_FULL;
200 hisi_sas_slot_index_set(hisi_hba, index);
201 *slot_idx = index;
202 return 0;
203 }
204
205 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
206 {
207 int i;
208
209 for (i = 0; i < hisi_hba->slot_index_count; ++i)
210 hisi_sas_slot_index_clear(hisi_hba, i);
211 }
212
213 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
214 struct hisi_sas_slot *slot)
215 {
216
217 if (task) {
218 struct device *dev = hisi_hba->dev;
219
220 if (!task->lldd_task)
221 return;
222
223 task->lldd_task = NULL;
224
225 if (!sas_protocol_ata(task->task_proto))
226 if (slot->n_elem)
227 dma_unmap_sg(dev, task->scatter,
228 task->num_scatter,
229 task->data_dir);
230 }
231
232 if (slot->buf)
233 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
234
235 list_del_init(&slot->entry);
236 slot->buf = NULL;
237 slot->task = NULL;
238 slot->port = NULL;
239 hisi_sas_slot_index_free(hisi_hba, slot->idx);
240
241 /* slot memory is fully zeroed when it is reused */
242 }
243 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
244
245 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
246 struct hisi_sas_slot *slot)
247 {
248 hisi_hba->hw->prep_smp(hisi_hba, slot);
249 }
250
251 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
252 struct hisi_sas_slot *slot, int is_tmf,
253 struct hisi_sas_tmf_task *tmf)
254 {
255 hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
256 }
257
258 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
259 struct hisi_sas_slot *slot)
260 {
261 hisi_hba->hw->prep_stp(hisi_hba, slot);
262 }
263
264 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
265 struct hisi_sas_slot *slot,
266 int device_id, int abort_flag, int tag_to_abort)
267 {
268 hisi_hba->hw->prep_abort(hisi_hba, slot,
269 device_id, abort_flag, tag_to_abort);
270 }
271
272 /*
273 * This function will issue an abort TMF regardless of whether the
274 * task is in the sdev or not. Then it will do the task complete
275 * cleanup and callbacks.
276 */
277 static void hisi_sas_slot_abort(struct work_struct *work)
278 {
279 struct hisi_sas_slot *abort_slot =
280 container_of(work, struct hisi_sas_slot, abort_slot);
281 struct sas_task *task = abort_slot->task;
282 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
283 struct scsi_cmnd *cmnd = task->uldd_task;
284 struct hisi_sas_tmf_task tmf_task;
285 struct scsi_lun lun;
286 struct device *dev = hisi_hba->dev;
287 int tag = abort_slot->idx;
288 unsigned long flags;
289
290 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
291 dev_err(dev, "cannot abort slot for non-ssp task\n");
292 goto out;
293 }
294
295 int_to_scsilun(cmnd->device->lun, &lun);
296 tmf_task.tmf = TMF_ABORT_TASK;
297 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
298
299 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
300 out:
301 /* Do cleanup for this task */
302 spin_lock_irqsave(&hisi_hba->lock, flags);
303 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
304 spin_unlock_irqrestore(&hisi_hba->lock, flags);
305 if (task->task_done)
306 task->task_done(task);
307 }
308
309 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
310 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
311 int *pass)
312 {
313 struct hisi_hba *hisi_hba = dq->hisi_hba;
314 struct domain_device *device = task->dev;
315 struct hisi_sas_device *sas_dev = device->lldd_dev;
316 struct hisi_sas_port *port;
317 struct hisi_sas_slot *slot;
318 struct hisi_sas_cmd_hdr *cmd_hdr_base;
319 struct asd_sas_port *sas_port = device->port;
320 struct device *dev = hisi_hba->dev;
321 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
322 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
323 unsigned long flags;
324
325 if (!sas_port) {
326 struct task_status_struct *ts = &task->task_status;
327
328 ts->resp = SAS_TASK_UNDELIVERED;
329 ts->stat = SAS_PHY_DOWN;
330 /*
331 * libsas will use dev->port, should
332 * not call task_done for sata
333 */
334 if (device->dev_type != SAS_SATA_DEV)
335 task->task_done(task);
336 return -ECOMM;
337 }
338
339 if (DEV_IS_GONE(sas_dev)) {
340 if (sas_dev)
341 dev_info(dev, "task prep: device %d not ready\n",
342 sas_dev->device_id);
343 else
344 dev_info(dev, "task prep: device %016llx not ready\n",
345 SAS_ADDR(device->sas_addr));
346
347 return -ECOMM;
348 }
349
350 port = to_hisi_sas_port(sas_port);
351 if (port && !port->port_attached) {
352 dev_info(dev, "task prep: %s port%d not attach device\n",
353 (dev_is_sata(device)) ?
354 "SATA/STP" : "SAS",
355 device->port->id);
356
357 return -ECOMM;
358 }
359
360 if (!sas_protocol_ata(task->task_proto)) {
361 unsigned int req_len, resp_len;
362
363 if (task->num_scatter) {
364 n_elem = dma_map_sg(dev, task->scatter,
365 task->num_scatter, task->data_dir);
366 if (!n_elem) {
367 rc = -ENOMEM;
368 goto prep_out;
369 }
370 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
371 n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
372 1, DMA_TO_DEVICE);
373 if (!n_elem_req) {
374 rc = -ENOMEM;
375 goto prep_out;
376 }
377 req_len = sg_dma_len(&task->smp_task.smp_req);
378 if (req_len & 0x3) {
379 rc = -EINVAL;
380 goto err_out_dma_unmap;
381 }
382 n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
383 1, DMA_FROM_DEVICE);
384 if (!n_elem_req) {
385 rc = -ENOMEM;
386 goto err_out_dma_unmap;
387 }
388 resp_len = sg_dma_len(&task->smp_task.smp_resp);
389 if (resp_len & 0x3) {
390 rc = -EINVAL;
391 goto err_out_dma_unmap;
392 }
393 }
394 } else
395 n_elem = task->num_scatter;
396
397 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
398 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
399 n_elem);
400 rc = -EINVAL;
401 goto err_out_dma_unmap;
402 }
403
404 spin_lock_irqsave(&hisi_hba->lock, flags);
405 if (hisi_hba->hw->slot_index_alloc)
406 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
407 device);
408 else
409 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
410 spin_unlock_irqrestore(&hisi_hba->lock, flags);
411 if (rc)
412 goto err_out_dma_unmap;
413
414 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
415 if (rc)
416 goto err_out_tag;
417
418 dlvry_queue = dq->id;
419 dlvry_queue_slot = dq->wr_point;
420 slot = &hisi_hba->slot_info[slot_idx];
421 memset(slot, 0, sizeof(struct hisi_sas_slot));
422
423 slot->idx = slot_idx;
424 slot->n_elem = n_elem;
425 slot->dlvry_queue = dlvry_queue;
426 slot->dlvry_queue_slot = dlvry_queue_slot;
427 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
428 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
429 slot->task = task;
430 slot->port = port;
431 if (is_tmf)
432 slot->is_internal = true;
433 task->lldd_task = slot;
434 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
435
436 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
437 GFP_ATOMIC, &slot->buf_dma);
438 if (!slot->buf) {
439 rc = -ENOMEM;
440 goto err_out_slot_buf;
441 }
442 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
443 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
444 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
445
446 switch (task->task_proto) {
447 case SAS_PROTOCOL_SMP:
448 hisi_sas_task_prep_smp(hisi_hba, slot);
449 break;
450 case SAS_PROTOCOL_SSP:
451 hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
452 break;
453 case SAS_PROTOCOL_SATA:
454 case SAS_PROTOCOL_STP:
455 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
456 hisi_sas_task_prep_ata(hisi_hba, slot);
457 break;
458 default:
459 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
460 task->task_proto);
461 break;
462 }
463
464 spin_lock_irqsave(&hisi_hba->lock, flags);
465 list_add_tail(&slot->entry, &sas_dev->list);
466 spin_unlock_irqrestore(&hisi_hba->lock, flags);
467 spin_lock_irqsave(&task->task_state_lock, flags);
468 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
469 spin_unlock_irqrestore(&task->task_state_lock, flags);
470
471 dq->slot_prep = slot;
472 ++(*pass);
473
474 return 0;
475
476 err_out_slot_buf:
477 /* Nothing to be done */
478 err_out_tag:
479 spin_lock_irqsave(&hisi_hba->lock, flags);
480 hisi_sas_slot_index_free(hisi_hba, slot_idx);
481 spin_unlock_irqrestore(&hisi_hba->lock, flags);
482 err_out_dma_unmap:
483 if (!sas_protocol_ata(task->task_proto)) {
484 if (task->num_scatter) {
485 dma_unmap_sg(dev, task->scatter, task->num_scatter,
486 task->data_dir);
487 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
488 if (n_elem_req)
489 dma_unmap_sg(dev, &task->smp_task.smp_req,
490 1, DMA_TO_DEVICE);
491 if (n_elem_resp)
492 dma_unmap_sg(dev, &task->smp_task.smp_resp,
493 1, DMA_FROM_DEVICE);
494 }
495 }
496 prep_out:
497 dev_err(dev, "task prep: failed[%d]!\n", rc);
498 return rc;
499 }
500
501 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
502 int is_tmf, struct hisi_sas_tmf_task *tmf)
503 {
504 u32 rc;
505 u32 pass = 0;
506 unsigned long flags;
507 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
508 struct device *dev = hisi_hba->dev;
509 struct domain_device *device = task->dev;
510 struct hisi_sas_device *sas_dev = device->lldd_dev;
511 struct hisi_sas_dq *dq = sas_dev->dq;
512
513 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
514 return -EINVAL;
515
516 /* protect task_prep and start_delivery sequence */
517 spin_lock_irqsave(&dq->lock, flags);
518 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
519 if (rc)
520 dev_err(dev, "task exec: failed[%d]!\n", rc);
521
522 if (likely(pass))
523 hisi_hba->hw->start_delivery(dq);
524 spin_unlock_irqrestore(&dq->lock, flags);
525
526 return rc;
527 }
528
529 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
530 {
531 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
532 struct asd_sas_phy *sas_phy = &phy->sas_phy;
533 struct sas_ha_struct *sas_ha;
534
535 if (!phy->phy_attached)
536 return;
537
538 sas_ha = &hisi_hba->sha;
539 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
540
541 if (sas_phy->phy) {
542 struct sas_phy *sphy = sas_phy->phy;
543
544 sphy->negotiated_linkrate = sas_phy->linkrate;
545 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
546 sphy->maximum_linkrate_hw =
547 hisi_hba->hw->phy_get_max_linkrate();
548 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
549 sphy->minimum_linkrate = phy->minimum_linkrate;
550
551 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
552 sphy->maximum_linkrate = phy->maximum_linkrate;
553 }
554
555 if (phy->phy_type & PORT_TYPE_SAS) {
556 struct sas_identify_frame *id;
557
558 id = (struct sas_identify_frame *)phy->frame_rcvd;
559 id->dev_type = phy->identify.device_type;
560 id->initiator_bits = SAS_PROTOCOL_ALL;
561 id->target_bits = phy->identify.target_port_protocols;
562 } else if (phy->phy_type & PORT_TYPE_SATA) {
563 /*Nothing*/
564 }
565
566 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
567 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
568 }
569
570 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
571 {
572 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
573 struct hisi_sas_device *sas_dev = NULL;
574 unsigned long flags;
575 int i;
576
577 spin_lock_irqsave(&hisi_hba->lock, flags);
578 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
579 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
580 int queue = i % hisi_hba->queue_count;
581 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
582
583 hisi_hba->devices[i].device_id = i;
584 sas_dev = &hisi_hba->devices[i];
585 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
586 sas_dev->dev_type = device->dev_type;
587 sas_dev->hisi_hba = hisi_hba;
588 sas_dev->sas_device = device;
589 sas_dev->dq = dq;
590 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
591 break;
592 }
593 }
594 spin_unlock_irqrestore(&hisi_hba->lock, flags);
595
596 return sas_dev;
597 }
598
599 static int hisi_sas_dev_found(struct domain_device *device)
600 {
601 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
602 struct domain_device *parent_dev = device->parent;
603 struct hisi_sas_device *sas_dev;
604 struct device *dev = hisi_hba->dev;
605
606 if (hisi_hba->hw->alloc_dev)
607 sas_dev = hisi_hba->hw->alloc_dev(device);
608 else
609 sas_dev = hisi_sas_alloc_dev(device);
610 if (!sas_dev) {
611 dev_err(dev, "fail alloc dev: max support %d devices\n",
612 HISI_SAS_MAX_DEVICES);
613 return -EINVAL;
614 }
615
616 device->lldd_dev = sas_dev;
617 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
618
619 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
620 int phy_no;
621 u8 phy_num = parent_dev->ex_dev.num_phys;
622 struct ex_phy *phy;
623
624 for (phy_no = 0; phy_no < phy_num; phy_no++) {
625 phy = &parent_dev->ex_dev.ex_phy[phy_no];
626 if (SAS_ADDR(phy->attached_sas_addr) ==
627 SAS_ADDR(device->sas_addr)) {
628 sas_dev->attached_phy = phy_no;
629 break;
630 }
631 }
632
633 if (phy_no == phy_num) {
634 dev_info(dev, "dev found: no attached "
635 "dev:%016llx at ex:%016llx\n",
636 SAS_ADDR(device->sas_addr),
637 SAS_ADDR(parent_dev->sas_addr));
638 return -EINVAL;
639 }
640 }
641
642 dev_info(dev, "dev[%d:%x] found\n",
643 sas_dev->device_id, sas_dev->dev_type);
644
645 return 0;
646 }
647
648 static int hisi_sas_slave_configure(struct scsi_device *sdev)
649 {
650 struct domain_device *dev = sdev_to_domain_dev(sdev);
651 int ret = sas_slave_configure(sdev);
652
653 if (ret)
654 return ret;
655 if (!dev_is_sata(dev))
656 sas_change_queue_depth(sdev, 64);
657
658 return 0;
659 }
660
661 static void hisi_sas_scan_start(struct Scsi_Host *shost)
662 {
663 struct hisi_hba *hisi_hba = shost_priv(shost);
664
665 hisi_hba->hw->phys_init(hisi_hba);
666 }
667
668 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
669 {
670 struct hisi_hba *hisi_hba = shost_priv(shost);
671 struct sas_ha_struct *sha = &hisi_hba->sha;
672
673 /* Wait for PHY up interrupt to occur */
674 if (time < HZ)
675 return 0;
676
677 sas_drain_work(sha);
678 return 1;
679 }
680
681 static void hisi_sas_phyup_work(struct work_struct *work)
682 {
683 struct hisi_sas_phy *phy =
684 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
685 struct hisi_hba *hisi_hba = phy->hisi_hba;
686 struct asd_sas_phy *sas_phy = &phy->sas_phy;
687 int phy_no = sas_phy->id;
688
689 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
690 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
691 }
692
693 static void hisi_sas_linkreset_work(struct work_struct *work)
694 {
695 struct hisi_sas_phy *phy =
696 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
697 struct asd_sas_phy *sas_phy = &phy->sas_phy;
698
699 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
700 }
701
702 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
703 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
704 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
705 };
706
707 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
708 enum hisi_sas_phy_event event)
709 {
710 struct hisi_hba *hisi_hba = phy->hisi_hba;
711
712 if (WARN_ON(event >= HISI_PHYES_NUM))
713 return false;
714
715 return queue_work(hisi_hba->wq, &phy->works[event]);
716 }
717 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
718
719 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
720 {
721 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
722 struct asd_sas_phy *sas_phy = &phy->sas_phy;
723 int i;
724
725 phy->hisi_hba = hisi_hba;
726 phy->port = NULL;
727 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
728 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
729 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
730 sas_phy->class = SAS;
731 sas_phy->iproto = SAS_PROTOCOL_ALL;
732 sas_phy->tproto = 0;
733 sas_phy->type = PHY_TYPE_PHYSICAL;
734 sas_phy->role = PHY_ROLE_INITIATOR;
735 sas_phy->oob_mode = OOB_NOT_CONNECTED;
736 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
737 sas_phy->id = phy_no;
738 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
739 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
740 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
741 sas_phy->lldd_phy = phy;
742
743 for (i = 0; i < HISI_PHYES_NUM; i++)
744 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
745 }
746
747 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
748 {
749 struct sas_ha_struct *sas_ha = sas_phy->ha;
750 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
751 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
752 struct asd_sas_port *sas_port = sas_phy->port;
753 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
754 unsigned long flags;
755
756 if (!sas_port)
757 return;
758
759 spin_lock_irqsave(&hisi_hba->lock, flags);
760 port->port_attached = 1;
761 port->id = phy->port_id;
762 phy->port = port;
763 sas_port->lldd_port = port;
764 spin_unlock_irqrestore(&hisi_hba->lock, flags);
765 }
766
767 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
768 struct hisi_sas_slot *slot)
769 {
770 if (task) {
771 unsigned long flags;
772 struct task_status_struct *ts;
773
774 ts = &task->task_status;
775
776 ts->resp = SAS_TASK_COMPLETE;
777 ts->stat = SAS_ABORTED_TASK;
778 spin_lock_irqsave(&task->task_state_lock, flags);
779 task->task_state_flags &=
780 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
781 task->task_state_flags |= SAS_TASK_STATE_DONE;
782 spin_unlock_irqrestore(&task->task_state_lock, flags);
783 }
784
785 hisi_sas_slot_task_free(hisi_hba, task, slot);
786 }
787
788 /* hisi_hba.lock should be locked */
789 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
790 struct domain_device *device)
791 {
792 struct hisi_sas_slot *slot, *slot2;
793 struct hisi_sas_device *sas_dev = device->lldd_dev;
794
795 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
796 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
797 }
798
799 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
800 {
801 struct hisi_sas_device *sas_dev;
802 struct domain_device *device;
803 int i;
804
805 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
806 sas_dev = &hisi_hba->devices[i];
807 device = sas_dev->sas_device;
808
809 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
810 !device)
811 continue;
812
813 hisi_sas_release_task(hisi_hba, device);
814 }
815 }
816 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
817
818 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
819 struct domain_device *device)
820 {
821 if (hisi_hba->hw->dereg_device)
822 hisi_hba->hw->dereg_device(hisi_hba, device);
823 }
824
825 static void hisi_sas_dev_gone(struct domain_device *device)
826 {
827 struct hisi_sas_device *sas_dev = device->lldd_dev;
828 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
829 struct device *dev = hisi_hba->dev;
830
831 dev_info(dev, "dev[%d:%x] is gone\n",
832 sas_dev->device_id, sas_dev->dev_type);
833
834 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
835 hisi_sas_internal_task_abort(hisi_hba, device,
836 HISI_SAS_INT_ABT_DEV, 0);
837
838 hisi_sas_dereg_device(hisi_hba, device);
839
840 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
841 device->lldd_dev = NULL;
842 }
843
844 if (hisi_hba->hw->free_device)
845 hisi_hba->hw->free_device(sas_dev);
846 sas_dev->dev_type = SAS_PHY_UNUSED;
847 }
848
849 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
850 {
851 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
852 }
853
854 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
855 void *funcdata)
856 {
857 struct sas_ha_struct *sas_ha = sas_phy->ha;
858 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
859 int phy_no = sas_phy->id;
860
861 switch (func) {
862 case PHY_FUNC_HARD_RESET:
863 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
864 break;
865
866 case PHY_FUNC_LINK_RESET:
867 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
868 msleep(100);
869 hisi_hba->hw->phy_start(hisi_hba, phy_no);
870 break;
871
872 case PHY_FUNC_DISABLE:
873 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
874 break;
875
876 case PHY_FUNC_SET_LINK_RATE:
877 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
878 break;
879 case PHY_FUNC_GET_EVENTS:
880 if (hisi_hba->hw->get_events) {
881 hisi_hba->hw->get_events(hisi_hba, phy_no);
882 break;
883 }
884 /* fallthru */
885 case PHY_FUNC_RELEASE_SPINUP_HOLD:
886 default:
887 return -EOPNOTSUPP;
888 }
889 return 0;
890 }
891
892 static void hisi_sas_task_done(struct sas_task *task)
893 {
894 if (!del_timer(&task->slow_task->timer))
895 return;
896 complete(&task->slow_task->completion);
897 }
898
899 static void hisi_sas_tmf_timedout(struct timer_list *t)
900 {
901 struct sas_task_slow *slow = from_timer(slow, t, timer);
902 struct sas_task *task = slow->task;
903 unsigned long flags;
904
905 spin_lock_irqsave(&task->task_state_lock, flags);
906 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
907 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
908 spin_unlock_irqrestore(&task->task_state_lock, flags);
909
910 complete(&task->slow_task->completion);
911 }
912
913 #define TASK_TIMEOUT 20
914 #define TASK_RETRY 3
915 #define INTERNAL_ABORT_TIMEOUT 6
916 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
917 void *parameter, u32 para_len,
918 struct hisi_sas_tmf_task *tmf)
919 {
920 struct hisi_sas_device *sas_dev = device->lldd_dev;
921 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
922 struct device *dev = hisi_hba->dev;
923 struct sas_task *task;
924 int res, retry;
925
926 for (retry = 0; retry < TASK_RETRY; retry++) {
927 task = sas_alloc_slow_task(GFP_KERNEL);
928 if (!task)
929 return -ENOMEM;
930
931 task->dev = device;
932 task->task_proto = device->tproto;
933
934 if (dev_is_sata(device)) {
935 task->ata_task.device_control_reg_update = 1;
936 memcpy(&task->ata_task.fis, parameter, para_len);
937 } else {
938 memcpy(&task->ssp_task, parameter, para_len);
939 }
940 task->task_done = hisi_sas_task_done;
941
942 task->slow_task->timer.function = hisi_sas_tmf_timedout;
943 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
944 add_timer(&task->slow_task->timer);
945
946 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
947
948 if (res) {
949 del_timer(&task->slow_task->timer);
950 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
951 res);
952 goto ex_err;
953 }
954
955 wait_for_completion(&task->slow_task->completion);
956 res = TMF_RESP_FUNC_FAILED;
957 /* Even TMF timed out, return direct. */
958 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
959 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
960 struct hisi_sas_slot *slot = task->lldd_task;
961
962 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
963 if (slot)
964 slot->task = NULL;
965
966 goto ex_err;
967 } else
968 dev_err(dev, "abort tmf: TMF task timeout\n");
969 }
970
971 if (task->task_status.resp == SAS_TASK_COMPLETE &&
972 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
973 res = TMF_RESP_FUNC_COMPLETE;
974 break;
975 }
976
977 if (task->task_status.resp == SAS_TASK_COMPLETE &&
978 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
979 res = TMF_RESP_FUNC_SUCC;
980 break;
981 }
982
983 if (task->task_status.resp == SAS_TASK_COMPLETE &&
984 task->task_status.stat == SAS_DATA_UNDERRUN) {
985 /* no error, but return the number of bytes of
986 * underrun
987 */
988 dev_warn(dev, "abort tmf: task to dev %016llx "
989 "resp: 0x%x sts 0x%x underrun\n",
990 SAS_ADDR(device->sas_addr),
991 task->task_status.resp,
992 task->task_status.stat);
993 res = task->task_status.residual;
994 break;
995 }
996
997 if (task->task_status.resp == SAS_TASK_COMPLETE &&
998 task->task_status.stat == SAS_DATA_OVERRUN) {
999 dev_warn(dev, "abort tmf: blocked task error\n");
1000 res = -EMSGSIZE;
1001 break;
1002 }
1003
1004 dev_warn(dev, "abort tmf: task to dev "
1005 "%016llx resp: 0x%x status 0x%x\n",
1006 SAS_ADDR(device->sas_addr), task->task_status.resp,
1007 task->task_status.stat);
1008 sas_free_task(task);
1009 task = NULL;
1010 }
1011 ex_err:
1012 if (retry == TASK_RETRY)
1013 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1014 sas_free_task(task);
1015 return res;
1016 }
1017
1018 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1019 bool reset, int pmp, u8 *fis)
1020 {
1021 struct ata_taskfile tf;
1022
1023 ata_tf_init(dev, &tf);
1024 if (reset)
1025 tf.ctl |= ATA_SRST;
1026 else
1027 tf.ctl &= ~ATA_SRST;
1028 tf.command = ATA_CMD_DEV_RESET;
1029 ata_tf_to_fis(&tf, pmp, 0, fis);
1030 }
1031
1032 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1033 {
1034 u8 fis[20] = {0};
1035 struct ata_port *ap = device->sata_dev.ap;
1036 struct ata_link *link;
1037 int rc = TMF_RESP_FUNC_FAILED;
1038 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1039 struct device *dev = hisi_hba->dev;
1040 int s = sizeof(struct host_to_dev_fis);
1041 unsigned long flags;
1042
1043 ata_for_each_link(link, ap, EDGE) {
1044 int pmp = sata_srst_pmp(link);
1045
1046 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1047 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1048 if (rc != TMF_RESP_FUNC_COMPLETE)
1049 break;
1050 }
1051
1052 if (rc == TMF_RESP_FUNC_COMPLETE) {
1053 ata_for_each_link(link, ap, EDGE) {
1054 int pmp = sata_srst_pmp(link);
1055
1056 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1057 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1058 s, NULL);
1059 if (rc != TMF_RESP_FUNC_COMPLETE)
1060 dev_err(dev, "ata disk de-reset failed\n");
1061 }
1062 } else {
1063 dev_err(dev, "ata disk reset failed\n");
1064 }
1065
1066 if (rc == TMF_RESP_FUNC_COMPLETE) {
1067 spin_lock_irqsave(&hisi_hba->lock, flags);
1068 hisi_sas_release_task(hisi_hba, device);
1069 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1070 }
1071
1072 return rc;
1073 }
1074
1075 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1076 u8 *lun, struct hisi_sas_tmf_task *tmf)
1077 {
1078 struct sas_ssp_task ssp_task;
1079
1080 if (!(device->tproto & SAS_PROTOCOL_SSP))
1081 return TMF_RESP_FUNC_ESUPP;
1082
1083 memcpy(ssp_task.LUN, lun, 8);
1084
1085 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1086 sizeof(ssp_task), tmf);
1087 }
1088
1089 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1090 {
1091 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1092 int i;
1093
1094 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1095 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1096 struct domain_device *device = sas_dev->sas_device;
1097 struct asd_sas_port *sas_port;
1098 struct hisi_sas_port *port;
1099 struct hisi_sas_phy *phy = NULL;
1100 struct asd_sas_phy *sas_phy;
1101
1102 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1103 || !device || !device->port)
1104 continue;
1105
1106 sas_port = device->port;
1107 port = to_hisi_sas_port(sas_port);
1108
1109 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1110 if (state & BIT(sas_phy->id)) {
1111 phy = sas_phy->lldd_phy;
1112 break;
1113 }
1114
1115 if (phy) {
1116 port->id = phy->port_id;
1117
1118 /* Update linkrate of directly attached device. */
1119 if (!device->parent)
1120 device->linkrate = phy->sas_phy.linkrate;
1121
1122 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1123 } else
1124 port->id = 0xff;
1125 }
1126 }
1127
1128 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1129 u32 state)
1130 {
1131 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1132 struct asd_sas_port *_sas_port = NULL;
1133 int phy_no;
1134
1135 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1136 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1137 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1138 struct asd_sas_port *sas_port = sas_phy->port;
1139 bool do_port_check = !!(_sas_port != sas_port);
1140
1141 if (!sas_phy->phy->enabled)
1142 continue;
1143
1144 /* Report PHY state change to libsas */
1145 if (state & BIT(phy_no)) {
1146 if (do_port_check && sas_port && sas_port->port_dev) {
1147 struct domain_device *dev = sas_port->port_dev;
1148
1149 _sas_port = sas_port;
1150
1151 if (DEV_IS_EXPANDER(dev->dev_type))
1152 sas_ha->notify_port_event(sas_phy,
1153 PORTE_BROADCAST_RCVD);
1154 }
1155 } else if (old_state & (1 << phy_no))
1156 /* PHY down but was up before */
1157 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1158
1159 }
1160 }
1161
1162 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1163 {
1164 struct device *dev = hisi_hba->dev;
1165 struct Scsi_Host *shost = hisi_hba->shost;
1166 u32 old_state, state;
1167 unsigned long flags;
1168 int rc;
1169
1170 if (!hisi_hba->hw->soft_reset)
1171 return -1;
1172
1173 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1174 return -1;
1175
1176 dev_info(dev, "controller resetting...\n");
1177 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1178
1179 scsi_block_requests(shost);
1180 if (timer_pending(&hisi_hba->timer))
1181 del_timer_sync(&hisi_hba->timer);
1182
1183 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1184 rc = hisi_hba->hw->soft_reset(hisi_hba);
1185 if (rc) {
1186 dev_warn(dev, "controller reset failed (%d)\n", rc);
1187 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1188 scsi_unblock_requests(shost);
1189 goto out;
1190 }
1191 spin_lock_irqsave(&hisi_hba->lock, flags);
1192 hisi_sas_release_tasks(hisi_hba);
1193 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1194
1195 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1196
1197 /* Init and wait for PHYs to come up and all libsas event finished. */
1198 hisi_hba->hw->phys_init(hisi_hba);
1199 msleep(1000);
1200 hisi_sas_refresh_port_id(hisi_hba);
1201 scsi_unblock_requests(shost);
1202
1203 state = hisi_hba->hw->get_phys_state(hisi_hba);
1204 hisi_sas_rescan_topology(hisi_hba, old_state, state);
1205 dev_info(dev, "controller reset complete\n");
1206
1207 out:
1208 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1209
1210 return rc;
1211 }
1212
1213 static int hisi_sas_abort_task(struct sas_task *task)
1214 {
1215 struct scsi_lun lun;
1216 struct hisi_sas_tmf_task tmf_task;
1217 struct domain_device *device = task->dev;
1218 struct hisi_sas_device *sas_dev = device->lldd_dev;
1219 struct hisi_hba *hisi_hba;
1220 struct device *dev;
1221 int rc = TMF_RESP_FUNC_FAILED;
1222 unsigned long flags;
1223
1224 if (!sas_dev)
1225 return TMF_RESP_FUNC_FAILED;
1226
1227 hisi_hba = dev_to_hisi_hba(task->dev);
1228 dev = hisi_hba->dev;
1229
1230 spin_lock_irqsave(&task->task_state_lock, flags);
1231 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1232 spin_unlock_irqrestore(&task->task_state_lock, flags);
1233 rc = TMF_RESP_FUNC_COMPLETE;
1234 goto out;
1235 }
1236 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1237 spin_unlock_irqrestore(&task->task_state_lock, flags);
1238
1239 sas_dev->dev_status = HISI_SAS_DEV_EH;
1240 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1241 struct scsi_cmnd *cmnd = task->uldd_task;
1242 struct hisi_sas_slot *slot = task->lldd_task;
1243 u32 tag = slot->idx;
1244 int rc2;
1245
1246 int_to_scsilun(cmnd->device->lun, &lun);
1247 tmf_task.tmf = TMF_ABORT_TASK;
1248 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1249
1250 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1251 &tmf_task);
1252
1253 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1254 HISI_SAS_INT_ABT_CMD, tag);
1255 if (rc2 < 0) {
1256 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1257 return TMF_RESP_FUNC_FAILED;
1258 }
1259
1260 /*
1261 * If the TMF finds that the IO is not in the device and also
1262 * the internal abort does not succeed, then it is safe to
1263 * free the slot.
1264 * Note: if the internal abort succeeds then the slot
1265 * will have already been completed
1266 */
1267 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1268 if (task->lldd_task) {
1269 spin_lock_irqsave(&hisi_hba->lock, flags);
1270 hisi_sas_do_release_task(hisi_hba, task, slot);
1271 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1272 }
1273 }
1274 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1275 task->task_proto & SAS_PROTOCOL_STP) {
1276 if (task->dev->dev_type == SAS_SATA_DEV) {
1277 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1278 HISI_SAS_INT_ABT_DEV, 0);
1279 if (rc < 0) {
1280 dev_err(dev, "abort task: internal abort failed\n");
1281 goto out;
1282 }
1283 hisi_sas_dereg_device(hisi_hba, device);
1284 rc = hisi_sas_softreset_ata_disk(device);
1285 }
1286 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1287 /* SMP */
1288 struct hisi_sas_slot *slot = task->lldd_task;
1289 u32 tag = slot->idx;
1290
1291 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1292 HISI_SAS_INT_ABT_CMD, tag);
1293 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1294 task->lldd_task) {
1295 spin_lock_irqsave(&hisi_hba->lock, flags);
1296 hisi_sas_do_release_task(hisi_hba, task, slot);
1297 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1298 }
1299 }
1300
1301 out:
1302 if (rc != TMF_RESP_FUNC_COMPLETE)
1303 dev_notice(dev, "abort task: rc=%d\n", rc);
1304 return rc;
1305 }
1306
1307 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1308 {
1309 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1310 struct device *dev = hisi_hba->dev;
1311 struct hisi_sas_tmf_task tmf_task;
1312 int rc = TMF_RESP_FUNC_FAILED;
1313 unsigned long flags;
1314
1315 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1316 HISI_SAS_INT_ABT_DEV, 0);
1317 if (rc < 0) {
1318 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1319 return TMF_RESP_FUNC_FAILED;
1320 }
1321 hisi_sas_dereg_device(hisi_hba, device);
1322
1323 tmf_task.tmf = TMF_ABORT_TASK_SET;
1324 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1325
1326 if (rc == TMF_RESP_FUNC_COMPLETE) {
1327 spin_lock_irqsave(&hisi_hba->lock, flags);
1328 hisi_sas_release_task(hisi_hba, device);
1329 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1330 }
1331
1332 return rc;
1333 }
1334
1335 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1336 {
1337 int rc = TMF_RESP_FUNC_FAILED;
1338 struct hisi_sas_tmf_task tmf_task;
1339
1340 tmf_task.tmf = TMF_CLEAR_ACA;
1341 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1342
1343 return rc;
1344 }
1345
1346 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1347 {
1348 struct sas_phy *phy = sas_get_local_phy(device);
1349 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1350 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1351 rc = sas_phy_reset(phy, reset_type);
1352 sas_put_local_phy(phy);
1353 msleep(2000);
1354 return rc;
1355 }
1356
1357 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1358 {
1359 struct hisi_sas_device *sas_dev = device->lldd_dev;
1360 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1361 struct device *dev = hisi_hba->dev;
1362 int rc = TMF_RESP_FUNC_FAILED;
1363 unsigned long flags;
1364
1365 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1366 return TMF_RESP_FUNC_FAILED;
1367 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1368
1369 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1370 HISI_SAS_INT_ABT_DEV, 0);
1371 if (rc < 0) {
1372 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1373 return TMF_RESP_FUNC_FAILED;
1374 }
1375 hisi_sas_dereg_device(hisi_hba, device);
1376
1377 rc = hisi_sas_debug_I_T_nexus_reset(device);
1378
1379 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) {
1380 spin_lock_irqsave(&hisi_hba->lock, flags);
1381 hisi_sas_release_task(hisi_hba, device);
1382 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1383 }
1384 return rc;
1385 }
1386
1387 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1388 {
1389 struct hisi_sas_device *sas_dev = device->lldd_dev;
1390 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1391 struct device *dev = hisi_hba->dev;
1392 unsigned long flags;
1393 int rc = TMF_RESP_FUNC_FAILED;
1394
1395 sas_dev->dev_status = HISI_SAS_DEV_EH;
1396 if (dev_is_sata(device)) {
1397 struct sas_phy *phy;
1398
1399 /* Clear internal IO and then hardreset */
1400 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1401 HISI_SAS_INT_ABT_DEV, 0);
1402 if (rc < 0) {
1403 dev_err(dev, "lu_reset: internal abort failed\n");
1404 goto out;
1405 }
1406 hisi_sas_dereg_device(hisi_hba, device);
1407
1408 phy = sas_get_local_phy(device);
1409
1410 rc = sas_phy_reset(phy, 1);
1411
1412 if (rc == 0) {
1413 spin_lock_irqsave(&hisi_hba->lock, flags);
1414 hisi_sas_release_task(hisi_hba, device);
1415 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1416 }
1417 sas_put_local_phy(phy);
1418 } else {
1419 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1420
1421 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1422 HISI_SAS_INT_ABT_DEV, 0);
1423 if (rc < 0) {
1424 dev_err(dev, "lu_reset: internal abort failed\n");
1425 goto out;
1426 }
1427 hisi_sas_dereg_device(hisi_hba, device);
1428
1429 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1430 if (rc == TMF_RESP_FUNC_COMPLETE) {
1431 spin_lock_irqsave(&hisi_hba->lock, flags);
1432 hisi_sas_release_task(hisi_hba, device);
1433 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1434 }
1435 }
1436 out:
1437 if (rc != TMF_RESP_FUNC_COMPLETE)
1438 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1439 sas_dev->device_id, rc);
1440 return rc;
1441 }
1442
1443 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1444 {
1445 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1446 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1447
1448 queue_work(hisi_hba->wq, &r.work);
1449 wait_for_completion(r.completion);
1450 if (r.done)
1451 return TMF_RESP_FUNC_COMPLETE;
1452
1453 return TMF_RESP_FUNC_FAILED;
1454 }
1455
1456 static int hisi_sas_query_task(struct sas_task *task)
1457 {
1458 struct scsi_lun lun;
1459 struct hisi_sas_tmf_task tmf_task;
1460 int rc = TMF_RESP_FUNC_FAILED;
1461
1462 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1463 struct scsi_cmnd *cmnd = task->uldd_task;
1464 struct domain_device *device = task->dev;
1465 struct hisi_sas_slot *slot = task->lldd_task;
1466 u32 tag = slot->idx;
1467
1468 int_to_scsilun(cmnd->device->lun, &lun);
1469 tmf_task.tmf = TMF_QUERY_TASK;
1470 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1471
1472 rc = hisi_sas_debug_issue_ssp_tmf(device,
1473 lun.scsi_lun,
1474 &tmf_task);
1475 switch (rc) {
1476 /* The task is still in Lun, release it then */
1477 case TMF_RESP_FUNC_SUCC:
1478 /* The task is not in Lun or failed, reset the phy */
1479 case TMF_RESP_FUNC_FAILED:
1480 case TMF_RESP_FUNC_COMPLETE:
1481 break;
1482 default:
1483 rc = TMF_RESP_FUNC_FAILED;
1484 break;
1485 }
1486 }
1487 return rc;
1488 }
1489
1490 static int
1491 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1492 struct sas_task *task, int abort_flag,
1493 int task_tag)
1494 {
1495 struct domain_device *device = task->dev;
1496 struct hisi_sas_device *sas_dev = device->lldd_dev;
1497 struct device *dev = hisi_hba->dev;
1498 struct hisi_sas_port *port;
1499 struct hisi_sas_slot *slot;
1500 struct asd_sas_port *sas_port = device->port;
1501 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1502 struct hisi_sas_dq *dq = sas_dev->dq;
1503 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1504 unsigned long flags, flags_dq;
1505
1506 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1507 return -EINVAL;
1508
1509 if (!device->port)
1510 return -1;
1511
1512 port = to_hisi_sas_port(sas_port);
1513
1514 /* simply get a slot and send abort command */
1515 spin_lock_irqsave(&hisi_hba->lock, flags);
1516 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1517 if (rc) {
1518 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1519 goto err_out;
1520 }
1521 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1522
1523 spin_lock_irqsave(&dq->lock, flags_dq);
1524 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1525 if (rc)
1526 goto err_out_tag;
1527
1528 dlvry_queue = dq->id;
1529 dlvry_queue_slot = dq->wr_point;
1530
1531 slot = &hisi_hba->slot_info[slot_idx];
1532 memset(slot, 0, sizeof(struct hisi_sas_slot));
1533
1534 slot->idx = slot_idx;
1535 slot->n_elem = n_elem;
1536 slot->dlvry_queue = dlvry_queue;
1537 slot->dlvry_queue_slot = dlvry_queue_slot;
1538 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1539 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1540 slot->task = task;
1541 slot->port = port;
1542 slot->is_internal = true;
1543 task->lldd_task = slot;
1544
1545 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1546 GFP_ATOMIC, &slot->buf_dma);
1547 if (!slot->buf) {
1548 rc = -ENOMEM;
1549 goto err_out_tag;
1550 }
1551
1552 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1553 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1554 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1555
1556 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1557 abort_flag, task_tag);
1558
1559 spin_lock_irqsave(&hisi_hba->lock, flags);
1560 list_add_tail(&slot->entry, &sas_dev->list);
1561 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1562 spin_lock_irqsave(&task->task_state_lock, flags);
1563 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1564 spin_unlock_irqrestore(&task->task_state_lock, flags);
1565
1566 dq->slot_prep = slot;
1567
1568 /* send abort command to the chip */
1569 hisi_hba->hw->start_delivery(dq);
1570 spin_unlock_irqrestore(&dq->lock, flags_dq);
1571
1572 return 0;
1573
1574 err_out_tag:
1575 spin_lock_irqsave(&hisi_hba->lock, flags);
1576 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1577 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1578 spin_unlock_irqrestore(&dq->lock, flags_dq);
1579 err_out:
1580 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1581
1582 return rc;
1583 }
1584
1585 /**
1586 * hisi_sas_internal_task_abort -- execute an internal
1587 * abort command for single IO command or a device
1588 * @hisi_hba: host controller struct
1589 * @device: domain device
1590 * @abort_flag: mode of operation, device or single IO
1591 * @tag: tag of IO to be aborted (only relevant to single
1592 * IO mode)
1593 */
1594 static int
1595 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1596 struct domain_device *device,
1597 int abort_flag, int tag)
1598 {
1599 struct sas_task *task;
1600 struct hisi_sas_device *sas_dev = device->lldd_dev;
1601 struct device *dev = hisi_hba->dev;
1602 int res;
1603
1604 /*
1605 * The interface is not realized means this HW don't support internal
1606 * abort, or don't need to do internal abort. Then here, we return
1607 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1608 * the internal abort has been executed and returned CQ.
1609 */
1610 if (!hisi_hba->hw->prep_abort)
1611 return TMF_RESP_FUNC_FAILED;
1612
1613 task = sas_alloc_slow_task(GFP_KERNEL);
1614 if (!task)
1615 return -ENOMEM;
1616
1617 task->dev = device;
1618 task->task_proto = device->tproto;
1619 task->task_done = hisi_sas_task_done;
1620 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1621 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1622 add_timer(&task->slow_task->timer);
1623
1624 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1625 task, abort_flag, tag);
1626 if (res) {
1627 del_timer(&task->slow_task->timer);
1628 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1629 res);
1630 goto exit;
1631 }
1632 wait_for_completion(&task->slow_task->completion);
1633 res = TMF_RESP_FUNC_FAILED;
1634
1635 /* Internal abort timed out */
1636 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1637 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1638 struct hisi_sas_slot *slot = task->lldd_task;
1639
1640 if (slot)
1641 slot->task = NULL;
1642 dev_err(dev, "internal task abort: timeout and not done.\n");
1643 res = -EIO;
1644 goto exit;
1645 } else
1646 dev_err(dev, "internal task abort: timeout.\n");
1647 }
1648
1649 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1650 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1651 res = TMF_RESP_FUNC_COMPLETE;
1652 goto exit;
1653 }
1654
1655 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1656 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1657 res = TMF_RESP_FUNC_SUCC;
1658 goto exit;
1659 }
1660
1661 exit:
1662 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1663 "resp: 0x%x sts 0x%x\n",
1664 SAS_ADDR(device->sas_addr),
1665 task,
1666 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1667 task->task_status.stat);
1668 sas_free_task(task);
1669
1670 return res;
1671 }
1672
1673 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1674 {
1675 hisi_sas_port_notify_formed(sas_phy);
1676 }
1677
1678 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1679 {
1680 }
1681
1682 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1683 u8 reg_index, u8 reg_count, u8 *write_data)
1684 {
1685 struct hisi_hba *hisi_hba = sha->lldd_ha;
1686
1687 if (!hisi_hba->hw->write_gpio)
1688 return -EOPNOTSUPP;
1689
1690 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1691 reg_index, reg_count, write_data);
1692 }
1693
1694 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1695 {
1696 phy->phy_attached = 0;
1697 phy->phy_type = 0;
1698 phy->port = NULL;
1699 }
1700
1701 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1702 {
1703 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1704 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1705 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1706
1707 if (rdy) {
1708 /* Phy down but ready */
1709 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1710 hisi_sas_port_notify_formed(sas_phy);
1711 } else {
1712 struct hisi_sas_port *port = phy->port;
1713
1714 /* Phy down and not ready */
1715 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1716 sas_phy_disconnected(sas_phy);
1717
1718 if (port) {
1719 if (phy->phy_type & PORT_TYPE_SAS) {
1720 int port_id = port->id;
1721
1722 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1723 port_id))
1724 port->port_attached = 0;
1725 } else if (phy->phy_type & PORT_TYPE_SATA)
1726 port->port_attached = 0;
1727 }
1728 hisi_sas_phy_disconnected(phy);
1729 }
1730 }
1731 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1732
1733 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1734 {
1735 int i;
1736
1737 for (i = 0; i < hisi_hba->queue_count; i++) {
1738 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1739
1740 tasklet_kill(&cq->tasklet);
1741 }
1742 }
1743 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1744
1745 struct scsi_transport_template *hisi_sas_stt;
1746 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1747
1748 static struct device_attribute *host_attrs[] = {
1749 &dev_attr_phy_event_threshold,
1750 NULL,
1751 };
1752
1753 static struct scsi_host_template _hisi_sas_sht = {
1754 .module = THIS_MODULE,
1755 .name = DRV_NAME,
1756 .queuecommand = sas_queuecommand,
1757 .target_alloc = sas_target_alloc,
1758 .slave_configure = hisi_sas_slave_configure,
1759 .scan_finished = hisi_sas_scan_finished,
1760 .scan_start = hisi_sas_scan_start,
1761 .change_queue_depth = sas_change_queue_depth,
1762 .bios_param = sas_bios_param,
1763 .can_queue = 1,
1764 .this_id = -1,
1765 .sg_tablesize = SG_ALL,
1766 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1767 .use_clustering = ENABLE_CLUSTERING,
1768 .eh_device_reset_handler = sas_eh_device_reset_handler,
1769 .eh_target_reset_handler = sas_eh_target_reset_handler,
1770 .target_destroy = sas_target_destroy,
1771 .ioctl = sas_ioctl,
1772 .shost_attrs = host_attrs,
1773 };
1774 struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1775 EXPORT_SYMBOL_GPL(hisi_sas_sht);
1776
1777 static struct sas_domain_function_template hisi_sas_transport_ops = {
1778 .lldd_dev_found = hisi_sas_dev_found,
1779 .lldd_dev_gone = hisi_sas_dev_gone,
1780 .lldd_execute_task = hisi_sas_queue_command,
1781 .lldd_control_phy = hisi_sas_control_phy,
1782 .lldd_abort_task = hisi_sas_abort_task,
1783 .lldd_abort_task_set = hisi_sas_abort_task_set,
1784 .lldd_clear_aca = hisi_sas_clear_aca,
1785 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1786 .lldd_lu_reset = hisi_sas_lu_reset,
1787 .lldd_query_task = hisi_sas_query_task,
1788 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1789 .lldd_port_formed = hisi_sas_port_formed,
1790 .lldd_port_deformed = hisi_sas_port_deformed,
1791 .lldd_write_gpio = hisi_sas_write_gpio,
1792 };
1793
1794 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1795 {
1796 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1797
1798 for (i = 0; i < hisi_hba->queue_count; i++) {
1799 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1800 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1801
1802 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1803 memset(hisi_hba->cmd_hdr[i], 0, s);
1804 dq->wr_point = 0;
1805
1806 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1807 memset(hisi_hba->complete_hdr[i], 0, s);
1808 cq->rd_point = 0;
1809 }
1810
1811 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1812 memset(hisi_hba->initial_fis, 0, s);
1813
1814 s = max_command_entries * sizeof(struct hisi_sas_iost);
1815 memset(hisi_hba->iost, 0, s);
1816
1817 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1818 memset(hisi_hba->breakpoint, 0, s);
1819
1820 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1821 memset(hisi_hba->sata_breakpoint, 0, s);
1822 }
1823 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1824
1825 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1826 {
1827 struct device *dev = hisi_hba->dev;
1828 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1829
1830 spin_lock_init(&hisi_hba->lock);
1831 for (i = 0; i < hisi_hba->n_phy; i++) {
1832 hisi_sas_phy_init(hisi_hba, i);
1833 hisi_hba->port[i].port_attached = 0;
1834 hisi_hba->port[i].id = -1;
1835 }
1836
1837 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1838 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1839 hisi_hba->devices[i].device_id = i;
1840 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1841 }
1842
1843 for (i = 0; i < hisi_hba->queue_count; i++) {
1844 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1845 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1846
1847 /* Completion queue structure */
1848 cq->id = i;
1849 cq->hisi_hba = hisi_hba;
1850
1851 /* Delivery queue structure */
1852 spin_lock_init(&dq->lock);
1853 dq->id = i;
1854 dq->hisi_hba = hisi_hba;
1855
1856 /* Delivery queue */
1857 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1858 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1859 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1860 if (!hisi_hba->cmd_hdr[i])
1861 goto err_out;
1862
1863 /* Completion queue */
1864 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1865 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1866 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1867 if (!hisi_hba->complete_hdr[i])
1868 goto err_out;
1869 }
1870
1871 s = sizeof(struct hisi_sas_slot_buf_table);
1872 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1873 if (!hisi_hba->buffer_pool)
1874 goto err_out;
1875
1876 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1877 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1878 GFP_KERNEL);
1879 if (!hisi_hba->itct)
1880 goto err_out;
1881
1882 memset(hisi_hba->itct, 0, s);
1883
1884 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1885 sizeof(struct hisi_sas_slot),
1886 GFP_KERNEL);
1887 if (!hisi_hba->slot_info)
1888 goto err_out;
1889
1890 s = max_command_entries * sizeof(struct hisi_sas_iost);
1891 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1892 GFP_KERNEL);
1893 if (!hisi_hba->iost)
1894 goto err_out;
1895
1896 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1897 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1898 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1899 if (!hisi_hba->breakpoint)
1900 goto err_out;
1901
1902 hisi_hba->slot_index_count = max_command_entries;
1903 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1904 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1905 if (!hisi_hba->slot_index_tags)
1906 goto err_out;
1907
1908 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1909 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1910 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1911 if (!hisi_hba->initial_fis)
1912 goto err_out;
1913
1914 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1915 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1916 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1917 if (!hisi_hba->sata_breakpoint)
1918 goto err_out;
1919 hisi_sas_init_mem(hisi_hba);
1920
1921 hisi_sas_slot_index_init(hisi_hba);
1922
1923 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1924 if (!hisi_hba->wq) {
1925 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1926 goto err_out;
1927 }
1928
1929 return 0;
1930 err_out:
1931 return -ENOMEM;
1932 }
1933 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
1934
1935 void hisi_sas_free(struct hisi_hba *hisi_hba)
1936 {
1937 struct device *dev = hisi_hba->dev;
1938 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1939
1940 for (i = 0; i < hisi_hba->queue_count; i++) {
1941 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1942 if (hisi_hba->cmd_hdr[i])
1943 dma_free_coherent(dev, s,
1944 hisi_hba->cmd_hdr[i],
1945 hisi_hba->cmd_hdr_dma[i]);
1946
1947 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1948 if (hisi_hba->complete_hdr[i])
1949 dma_free_coherent(dev, s,
1950 hisi_hba->complete_hdr[i],
1951 hisi_hba->complete_hdr_dma[i]);
1952 }
1953
1954 dma_pool_destroy(hisi_hba->buffer_pool);
1955
1956 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1957 if (hisi_hba->itct)
1958 dma_free_coherent(dev, s,
1959 hisi_hba->itct, hisi_hba->itct_dma);
1960
1961 s = max_command_entries * sizeof(struct hisi_sas_iost);
1962 if (hisi_hba->iost)
1963 dma_free_coherent(dev, s,
1964 hisi_hba->iost, hisi_hba->iost_dma);
1965
1966 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1967 if (hisi_hba->breakpoint)
1968 dma_free_coherent(dev, s,
1969 hisi_hba->breakpoint,
1970 hisi_hba->breakpoint_dma);
1971
1972
1973 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1974 if (hisi_hba->initial_fis)
1975 dma_free_coherent(dev, s,
1976 hisi_hba->initial_fis,
1977 hisi_hba->initial_fis_dma);
1978
1979 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1980 if (hisi_hba->sata_breakpoint)
1981 dma_free_coherent(dev, s,
1982 hisi_hba->sata_breakpoint,
1983 hisi_hba->sata_breakpoint_dma);
1984
1985 if (hisi_hba->wq)
1986 destroy_workqueue(hisi_hba->wq);
1987 }
1988 EXPORT_SYMBOL_GPL(hisi_sas_free);
1989
1990 void hisi_sas_rst_work_handler(struct work_struct *work)
1991 {
1992 struct hisi_hba *hisi_hba =
1993 container_of(work, struct hisi_hba, rst_work);
1994
1995 hisi_sas_controller_reset(hisi_hba);
1996 }
1997 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
1998
1999 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2000 {
2001 struct hisi_sas_rst *rst =
2002 container_of(work, struct hisi_sas_rst, work);
2003
2004 if (!hisi_sas_controller_reset(rst->hisi_hba))
2005 rst->done = true;
2006 complete(rst->completion);
2007 }
2008 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2009
2010 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2011 {
2012 struct device *dev = hisi_hba->dev;
2013 struct platform_device *pdev = hisi_hba->platform_dev;
2014 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2015 struct clk *refclk;
2016
2017 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2018 SAS_ADDR_SIZE)) {
2019 dev_err(dev, "could not get property sas-addr\n");
2020 return -ENOENT;
2021 }
2022
2023 if (np) {
2024 /*
2025 * These properties are only required for platform device-based
2026 * controller with DT firmware.
2027 */
2028 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2029 "hisilicon,sas-syscon");
2030 if (IS_ERR(hisi_hba->ctrl)) {
2031 dev_err(dev, "could not get syscon\n");
2032 return -ENOENT;
2033 }
2034
2035 if (device_property_read_u32(dev, "ctrl-reset-reg",
2036 &hisi_hba->ctrl_reset_reg)) {
2037 dev_err(dev,
2038 "could not get property ctrl-reset-reg\n");
2039 return -ENOENT;
2040 }
2041
2042 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2043 &hisi_hba->ctrl_reset_sts_reg)) {
2044 dev_err(dev,
2045 "could not get property ctrl-reset-sts-reg\n");
2046 return -ENOENT;
2047 }
2048
2049 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2050 &hisi_hba->ctrl_clock_ena_reg)) {
2051 dev_err(dev,
2052 "could not get property ctrl-clock-ena-reg\n");
2053 return -ENOENT;
2054 }
2055 }
2056
2057 refclk = devm_clk_get(dev, NULL);
2058 if (IS_ERR(refclk))
2059 dev_dbg(dev, "no ref clk property\n");
2060 else
2061 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2062
2063 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2064 dev_err(dev, "could not get property phy-count\n");
2065 return -ENOENT;
2066 }
2067
2068 if (device_property_read_u32(dev, "queue-count",
2069 &hisi_hba->queue_count)) {
2070 dev_err(dev, "could not get property queue-count\n");
2071 return -ENOENT;
2072 }
2073
2074 return 0;
2075 }
2076 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2077
2078 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2079 const struct hisi_sas_hw *hw)
2080 {
2081 struct resource *res;
2082 struct Scsi_Host *shost;
2083 struct hisi_hba *hisi_hba;
2084 struct device *dev = &pdev->dev;
2085
2086 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
2087 if (!shost) {
2088 dev_err(dev, "scsi host alloc failed\n");
2089 return NULL;
2090 }
2091 hisi_hba = shost_priv(shost);
2092
2093 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2094 hisi_hba->hw = hw;
2095 hisi_hba->dev = dev;
2096 hisi_hba->platform_dev = pdev;
2097 hisi_hba->shost = shost;
2098 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2099
2100 timer_setup(&hisi_hba->timer, NULL, 0);
2101
2102 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2103 goto err_out;
2104
2105 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2106 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2107 dev_err(dev, "No usable DMA addressing method\n");
2108 goto err_out;
2109 }
2110
2111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2112 hisi_hba->regs = devm_ioremap_resource(dev, res);
2113 if (IS_ERR(hisi_hba->regs))
2114 goto err_out;
2115
2116 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2117 if (res) {
2118 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2119 if (IS_ERR(hisi_hba->sgpio_regs))
2120 goto err_out;
2121 }
2122
2123 if (hisi_sas_alloc(hisi_hba, shost)) {
2124 hisi_sas_free(hisi_hba);
2125 goto err_out;
2126 }
2127
2128 return shost;
2129 err_out:
2130 scsi_host_put(shost);
2131 dev_err(dev, "shost alloc failed\n");
2132 return NULL;
2133 }
2134
2135 void hisi_sas_init_add(struct hisi_hba *hisi_hba)
2136 {
2137 int i;
2138
2139 for (i = 0; i < hisi_hba->n_phy; i++)
2140 memcpy(&hisi_hba->phy[i].dev_sas_addr,
2141 hisi_hba->sas_addr,
2142 SAS_ADDR_SIZE);
2143 }
2144 EXPORT_SYMBOL_GPL(hisi_sas_init_add);
2145
2146 int hisi_sas_probe(struct platform_device *pdev,
2147 const struct hisi_sas_hw *hw)
2148 {
2149 struct Scsi_Host *shost;
2150 struct hisi_hba *hisi_hba;
2151 struct device *dev = &pdev->dev;
2152 struct asd_sas_phy **arr_phy;
2153 struct asd_sas_port **arr_port;
2154 struct sas_ha_struct *sha;
2155 int rc, phy_nr, port_nr, i;
2156
2157 shost = hisi_sas_shost_alloc(pdev, hw);
2158 if (!shost)
2159 return -ENOMEM;
2160
2161 sha = SHOST_TO_SAS_HA(shost);
2162 hisi_hba = shost_priv(shost);
2163 platform_set_drvdata(pdev, sha);
2164
2165 phy_nr = port_nr = hisi_hba->n_phy;
2166
2167 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2168 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2169 if (!arr_phy || !arr_port) {
2170 rc = -ENOMEM;
2171 goto err_out_ha;
2172 }
2173
2174 sha->sas_phy = arr_phy;
2175 sha->sas_port = arr_port;
2176 sha->lldd_ha = hisi_hba;
2177
2178 shost->transportt = hisi_sas_stt;
2179 shost->max_id = HISI_SAS_MAX_DEVICES;
2180 shost->max_lun = ~0;
2181 shost->max_channel = 1;
2182 shost->max_cmd_len = 16;
2183 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2184 shost->can_queue = hisi_hba->hw->max_command_entries;
2185 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2186
2187 sha->sas_ha_name = DRV_NAME;
2188 sha->dev = hisi_hba->dev;
2189 sha->lldd_module = THIS_MODULE;
2190 sha->sas_addr = &hisi_hba->sas_addr[0];
2191 sha->num_phys = hisi_hba->n_phy;
2192 sha->core.shost = hisi_hba->shost;
2193
2194 for (i = 0; i < hisi_hba->n_phy; i++) {
2195 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2196 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2197 }
2198
2199 hisi_sas_init_add(hisi_hba);
2200
2201 rc = scsi_add_host(shost, &pdev->dev);
2202 if (rc)
2203 goto err_out_ha;
2204
2205 rc = sas_register_ha(sha);
2206 if (rc)
2207 goto err_out_register_ha;
2208
2209 rc = hisi_hba->hw->hw_init(hisi_hba);
2210 if (rc)
2211 goto err_out_register_ha;
2212
2213 scsi_scan_host(shost);
2214
2215 return 0;
2216
2217 err_out_register_ha:
2218 scsi_remove_host(shost);
2219 err_out_ha:
2220 hisi_sas_free(hisi_hba);
2221 scsi_host_put(shost);
2222 return rc;
2223 }
2224 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2225
2226 int hisi_sas_remove(struct platform_device *pdev)
2227 {
2228 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2229 struct hisi_hba *hisi_hba = sha->lldd_ha;
2230 struct Scsi_Host *shost = sha->core.shost;
2231
2232 if (timer_pending(&hisi_hba->timer))
2233 del_timer(&hisi_hba->timer);
2234
2235 sas_unregister_ha(sha);
2236 sas_remove_host(sha->core.shost);
2237
2238 hisi_sas_free(hisi_hba);
2239 scsi_host_put(shost);
2240 return 0;
2241 }
2242 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2243
2244 static __init int hisi_sas_init(void)
2245 {
2246 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2247 if (!hisi_sas_stt)
2248 return -ENOMEM;
2249
2250 return 0;
2251 }
2252
2253 static __exit void hisi_sas_exit(void)
2254 {
2255 sas_release_transport(hisi_sas_stt);
2256 }
2257
2258 module_init(hisi_sas_init);
2259 module_exit(hisi_sas_exit);
2260
2261 MODULE_LICENSE("GPL");
2262 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2263 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2264 MODULE_ALIAS("platform:" DRV_NAME);