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