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