hisi_sas_main.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  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. #include "hisi_sas.h"
  12. #define DRV_NAME "hisi_sas"
  13. #define DEV_IS_GONE(dev) \
  14. ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
  15. static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
  16. u8 *lun, struct hisi_sas_tmf_task *tmf);
  17. static int
  18. hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  19. struct domain_device *device,
  20. int abort_flag, int tag);
  21. static int hisi_sas_softreset_ata_disk(struct domain_device *device);
  22. u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
  23. {
  24. switch (cmd) {
  25. case ATA_CMD_FPDMA_WRITE:
  26. case ATA_CMD_FPDMA_READ:
  27. case ATA_CMD_FPDMA_RECV:
  28. case ATA_CMD_FPDMA_SEND:
  29. case ATA_CMD_NCQ_NON_DATA:
  30. return HISI_SAS_SATA_PROTOCOL_FPDMA;
  31. case ATA_CMD_DOWNLOAD_MICRO:
  32. case ATA_CMD_ID_ATA:
  33. case ATA_CMD_PMP_READ:
  34. case ATA_CMD_READ_LOG_EXT:
  35. case ATA_CMD_PIO_READ:
  36. case ATA_CMD_PIO_READ_EXT:
  37. case ATA_CMD_PMP_WRITE:
  38. case ATA_CMD_WRITE_LOG_EXT:
  39. case ATA_CMD_PIO_WRITE:
  40. case ATA_CMD_PIO_WRITE_EXT:
  41. return HISI_SAS_SATA_PROTOCOL_PIO;
  42. case ATA_CMD_DSM:
  43. case ATA_CMD_DOWNLOAD_MICRO_DMA:
  44. case ATA_CMD_PMP_READ_DMA:
  45. case ATA_CMD_PMP_WRITE_DMA:
  46. case ATA_CMD_READ:
  47. case ATA_CMD_READ_EXT:
  48. case ATA_CMD_READ_LOG_DMA_EXT:
  49. case ATA_CMD_READ_STREAM_DMA_EXT:
  50. case ATA_CMD_TRUSTED_RCV_DMA:
  51. case ATA_CMD_TRUSTED_SND_DMA:
  52. case ATA_CMD_WRITE:
  53. case ATA_CMD_WRITE_EXT:
  54. case ATA_CMD_WRITE_FUA_EXT:
  55. case ATA_CMD_WRITE_QUEUED:
  56. case ATA_CMD_WRITE_LOG_DMA_EXT:
  57. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  58. case ATA_CMD_ZAC_MGMT_IN:
  59. return HISI_SAS_SATA_PROTOCOL_DMA;
  60. case ATA_CMD_CHK_POWER:
  61. case ATA_CMD_DEV_RESET:
  62. case ATA_CMD_EDD:
  63. case ATA_CMD_FLUSH:
  64. case ATA_CMD_FLUSH_EXT:
  65. case ATA_CMD_VERIFY:
  66. case ATA_CMD_VERIFY_EXT:
  67. case ATA_CMD_SET_FEATURES:
  68. case ATA_CMD_STANDBY:
  69. case ATA_CMD_STANDBYNOW1:
  70. case ATA_CMD_ZAC_MGMT_OUT:
  71. return HISI_SAS_SATA_PROTOCOL_NONDATA;
  72. default:
  73. if (direction == DMA_NONE)
  74. return HISI_SAS_SATA_PROTOCOL_NONDATA;
  75. return HISI_SAS_SATA_PROTOCOL_PIO;
  76. }
  77. }
  78. EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
  79. void hisi_sas_sata_done(struct sas_task *task,
  80. struct hisi_sas_slot *slot)
  81. {
  82. struct task_status_struct *ts = &task->task_status;
  83. struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
  84. struct hisi_sas_status_buffer *status_buf =
  85. hisi_sas_status_buf_addr_mem(slot);
  86. u8 *iu = &status_buf->iu[0];
  87. struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
  88. resp->frame_len = sizeof(struct dev_to_host_fis);
  89. memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
  90. ts->buf_valid_size = sizeof(*resp);
  91. }
  92. EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
  93. int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
  94. {
  95. struct ata_queued_cmd *qc = task->uldd_task;
  96. if (qc) {
  97. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  98. qc->tf.command == ATA_CMD_FPDMA_READ) {
  99. *tag = qc->tag;
  100. return 1;
  101. }
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
  106. static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
  107. {
  108. return device->port->ha->lldd_ha;
  109. }
  110. struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
  111. {
  112. return container_of(sas_port, struct hisi_sas_port, sas_port);
  113. }
  114. EXPORT_SYMBOL_GPL(to_hisi_sas_port);
  115. void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
  116. {
  117. int phy_no;
  118. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
  119. hisi_hba->hw->phy_disable(hisi_hba, phy_no);
  120. }
  121. EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
  122. static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
  123. {
  124. void *bitmap = hisi_hba->slot_index_tags;
  125. clear_bit(slot_idx, bitmap);
  126. }
  127. static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
  128. {
  129. hisi_sas_slot_index_clear(hisi_hba, slot_idx);
  130. }
  131. static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
  132. {
  133. void *bitmap = hisi_hba->slot_index_tags;
  134. set_bit(slot_idx, bitmap);
  135. }
  136. static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
  137. {
  138. unsigned int index;
  139. void *bitmap = hisi_hba->slot_index_tags;
  140. index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
  141. if (index >= hisi_hba->slot_index_count)
  142. return -SAS_QUEUE_FULL;
  143. hisi_sas_slot_index_set(hisi_hba, index);
  144. *slot_idx = index;
  145. return 0;
  146. }
  147. static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
  148. {
  149. int i;
  150. for (i = 0; i < hisi_hba->slot_index_count; ++i)
  151. hisi_sas_slot_index_clear(hisi_hba, i);
  152. }
  153. void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
  154. struct hisi_sas_slot *slot)
  155. {
  156. if (task) {
  157. struct device *dev = hisi_hba->dev;
  158. struct domain_device *device = task->dev;
  159. struct hisi_sas_device *sas_dev = device->lldd_dev;
  160. if (!task->lldd_task)
  161. return;
  162. task->lldd_task = NULL;
  163. if (!sas_protocol_ata(task->task_proto))
  164. if (slot->n_elem)
  165. dma_unmap_sg(dev, task->scatter, slot->n_elem,
  166. task->data_dir);
  167. if (sas_dev)
  168. atomic64_dec(&sas_dev->running_req);
  169. }
  170. if (slot->buf)
  171. dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
  172. list_del_init(&slot->entry);
  173. slot->buf = NULL;
  174. slot->task = NULL;
  175. slot->port = NULL;
  176. hisi_sas_slot_index_free(hisi_hba, slot->idx);
  177. /* slot memory is fully zeroed when it is reused */
  178. }
  179. EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
  180. static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
  181. struct hisi_sas_slot *slot)
  182. {
  183. return hisi_hba->hw->prep_smp(hisi_hba, slot);
  184. }
  185. static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
  186. struct hisi_sas_slot *slot, int is_tmf,
  187. struct hisi_sas_tmf_task *tmf)
  188. {
  189. return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
  190. }
  191. static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
  192. struct hisi_sas_slot *slot)
  193. {
  194. return hisi_hba->hw->prep_stp(hisi_hba, slot);
  195. }
  196. static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
  197. struct hisi_sas_slot *slot,
  198. int device_id, int abort_flag, int tag_to_abort)
  199. {
  200. return hisi_hba->hw->prep_abort(hisi_hba, slot,
  201. device_id, abort_flag, tag_to_abort);
  202. }
  203. /*
  204. * This function will issue an abort TMF regardless of whether the
  205. * task is in the sdev or not. Then it will do the task complete
  206. * cleanup and callbacks.
  207. */
  208. static void hisi_sas_slot_abort(struct work_struct *work)
  209. {
  210. struct hisi_sas_slot *abort_slot =
  211. container_of(work, struct hisi_sas_slot, abort_slot);
  212. struct sas_task *task = abort_slot->task;
  213. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  214. struct scsi_cmnd *cmnd = task->uldd_task;
  215. struct hisi_sas_tmf_task tmf_task;
  216. struct scsi_lun lun;
  217. struct device *dev = hisi_hba->dev;
  218. int tag = abort_slot->idx;
  219. unsigned long flags;
  220. if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
  221. dev_err(dev, "cannot abort slot for non-ssp task\n");
  222. goto out;
  223. }
  224. int_to_scsilun(cmnd->device->lun, &lun);
  225. tmf_task.tmf = TMF_ABORT_TASK;
  226. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  227. hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
  228. out:
  229. /* Do cleanup for this task */
  230. spin_lock_irqsave(&hisi_hba->lock, flags);
  231. hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
  232. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  233. if (task->task_done)
  234. task->task_done(task);
  235. }
  236. static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
  237. *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
  238. int *pass)
  239. {
  240. struct hisi_hba *hisi_hba = dq->hisi_hba;
  241. struct domain_device *device = task->dev;
  242. struct hisi_sas_device *sas_dev = device->lldd_dev;
  243. struct hisi_sas_port *port;
  244. struct hisi_sas_slot *slot;
  245. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  246. struct asd_sas_port *sas_port = device->port;
  247. struct device *dev = hisi_hba->dev;
  248. int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
  249. unsigned long flags;
  250. if (!sas_port) {
  251. struct task_status_struct *ts = &task->task_status;
  252. ts->resp = SAS_TASK_UNDELIVERED;
  253. ts->stat = SAS_PHY_DOWN;
  254. /*
  255. * libsas will use dev->port, should
  256. * not call task_done for sata
  257. */
  258. if (device->dev_type != SAS_SATA_DEV)
  259. task->task_done(task);
  260. return SAS_PHY_DOWN;
  261. }
  262. if (DEV_IS_GONE(sas_dev)) {
  263. if (sas_dev)
  264. dev_info(dev, "task prep: device %d not ready\n",
  265. sas_dev->device_id);
  266. else
  267. dev_info(dev, "task prep: device %016llx not ready\n",
  268. SAS_ADDR(device->sas_addr));
  269. return SAS_PHY_DOWN;
  270. }
  271. port = to_hisi_sas_port(sas_port);
  272. if (port && !port->port_attached) {
  273. dev_info(dev, "task prep: %s port%d not attach device\n",
  274. (dev_is_sata(device)) ?
  275. "SATA/STP" : "SAS",
  276. device->port->id);
  277. return SAS_PHY_DOWN;
  278. }
  279. if (!sas_protocol_ata(task->task_proto)) {
  280. if (task->num_scatter) {
  281. n_elem = dma_map_sg(dev, task->scatter,
  282. task->num_scatter, task->data_dir);
  283. if (!n_elem) {
  284. rc = -ENOMEM;
  285. goto prep_out;
  286. }
  287. }
  288. } else
  289. n_elem = task->num_scatter;
  290. spin_lock_irqsave(&hisi_hba->lock, flags);
  291. if (hisi_hba->hw->slot_index_alloc)
  292. rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
  293. device);
  294. else
  295. rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
  296. if (rc) {
  297. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  298. goto err_out;
  299. }
  300. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  301. rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
  302. if (rc)
  303. goto err_out_tag;
  304. dlvry_queue = dq->id;
  305. dlvry_queue_slot = dq->wr_point;
  306. slot = &hisi_hba->slot_info[slot_idx];
  307. memset(slot, 0, sizeof(struct hisi_sas_slot));
  308. slot->idx = slot_idx;
  309. slot->n_elem = n_elem;
  310. slot->dlvry_queue = dlvry_queue;
  311. slot->dlvry_queue_slot = dlvry_queue_slot;
  312. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  313. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  314. slot->task = task;
  315. slot->port = port;
  316. task->lldd_task = slot;
  317. INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
  318. slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
  319. GFP_ATOMIC, &slot->buf_dma);
  320. if (!slot->buf) {
  321. rc = -ENOMEM;
  322. goto err_out_slot_buf;
  323. }
  324. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  325. memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
  326. memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
  327. switch (task->task_proto) {
  328. case SAS_PROTOCOL_SMP:
  329. rc = hisi_sas_task_prep_smp(hisi_hba, slot);
  330. break;
  331. case SAS_PROTOCOL_SSP:
  332. rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
  333. break;
  334. case SAS_PROTOCOL_SATA:
  335. case SAS_PROTOCOL_STP:
  336. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  337. rc = hisi_sas_task_prep_ata(hisi_hba, slot);
  338. break;
  339. default:
  340. dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
  341. task->task_proto);
  342. rc = -EINVAL;
  343. break;
  344. }
  345. if (rc) {
  346. dev_err(dev, "task prep: rc = 0x%x\n", rc);
  347. goto err_out_buf;
  348. }
  349. spin_lock_irqsave(&hisi_hba->lock, flags);
  350. list_add_tail(&slot->entry, &sas_dev->list);
  351. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  352. spin_lock_irqsave(&task->task_state_lock, flags);
  353. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  354. spin_unlock_irqrestore(&task->task_state_lock, flags);
  355. dq->slot_prep = slot;
  356. atomic64_inc(&sas_dev->running_req);
  357. ++(*pass);
  358. return 0;
  359. err_out_buf:
  360. dma_pool_free(hisi_hba->buffer_pool, slot->buf,
  361. slot->buf_dma);
  362. err_out_slot_buf:
  363. /* Nothing to be done */
  364. err_out_tag:
  365. spin_lock_irqsave(&hisi_hba->lock, flags);
  366. hisi_sas_slot_index_free(hisi_hba, slot_idx);
  367. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  368. err_out:
  369. dev_err(dev, "task prep: failed[%d]!\n", rc);
  370. if (!sas_protocol_ata(task->task_proto))
  371. if (n_elem)
  372. dma_unmap_sg(dev, task->scatter, n_elem,
  373. task->data_dir);
  374. prep_out:
  375. return rc;
  376. }
  377. static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
  378. int is_tmf, struct hisi_sas_tmf_task *tmf)
  379. {
  380. u32 rc;
  381. u32 pass = 0;
  382. unsigned long flags;
  383. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  384. struct device *dev = hisi_hba->dev;
  385. struct domain_device *device = task->dev;
  386. struct hisi_sas_device *sas_dev = device->lldd_dev;
  387. struct hisi_sas_dq *dq = sas_dev->dq;
  388. if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
  389. return -EINVAL;
  390. /* protect task_prep and start_delivery sequence */
  391. spin_lock_irqsave(&dq->lock, flags);
  392. rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
  393. if (rc)
  394. dev_err(dev, "task exec: failed[%d]!\n", rc);
  395. if (likely(pass))
  396. hisi_hba->hw->start_delivery(dq);
  397. spin_unlock_irqrestore(&dq->lock, flags);
  398. return rc;
  399. }
  400. static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
  401. {
  402. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  403. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  404. struct sas_ha_struct *sas_ha;
  405. if (!phy->phy_attached)
  406. return;
  407. sas_ha = &hisi_hba->sha;
  408. sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
  409. if (sas_phy->phy) {
  410. struct sas_phy *sphy = sas_phy->phy;
  411. sphy->negotiated_linkrate = sas_phy->linkrate;
  412. sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  413. sphy->maximum_linkrate_hw =
  414. hisi_hba->hw->phy_get_max_linkrate();
  415. if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
  416. sphy->minimum_linkrate = phy->minimum_linkrate;
  417. if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
  418. sphy->maximum_linkrate = phy->maximum_linkrate;
  419. }
  420. if (phy->phy_type & PORT_TYPE_SAS) {
  421. struct sas_identify_frame *id;
  422. id = (struct sas_identify_frame *)phy->frame_rcvd;
  423. id->dev_type = phy->identify.device_type;
  424. id->initiator_bits = SAS_PROTOCOL_ALL;
  425. id->target_bits = phy->identify.target_port_protocols;
  426. } else if (phy->phy_type & PORT_TYPE_SATA) {
  427. /*Nothing*/
  428. }
  429. sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
  430. sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
  431. }
  432. static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
  433. {
  434. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  435. struct hisi_sas_device *sas_dev = NULL;
  436. unsigned long flags;
  437. int i;
  438. spin_lock_irqsave(&hisi_hba->lock, flags);
  439. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  440. if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
  441. int queue = i % hisi_hba->queue_count;
  442. struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
  443. hisi_hba->devices[i].device_id = i;
  444. sas_dev = &hisi_hba->devices[i];
  445. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  446. sas_dev->dev_type = device->dev_type;
  447. sas_dev->hisi_hba = hisi_hba;
  448. sas_dev->sas_device = device;
  449. sas_dev->dq = dq;
  450. INIT_LIST_HEAD(&hisi_hba->devices[i].list);
  451. break;
  452. }
  453. }
  454. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  455. return sas_dev;
  456. }
  457. static int hisi_sas_dev_found(struct domain_device *device)
  458. {
  459. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  460. struct domain_device *parent_dev = device->parent;
  461. struct hisi_sas_device *sas_dev;
  462. struct device *dev = hisi_hba->dev;
  463. if (hisi_hba->hw->alloc_dev)
  464. sas_dev = hisi_hba->hw->alloc_dev(device);
  465. else
  466. sas_dev = hisi_sas_alloc_dev(device);
  467. if (!sas_dev) {
  468. dev_err(dev, "fail alloc dev: max support %d devices\n",
  469. HISI_SAS_MAX_DEVICES);
  470. return -EINVAL;
  471. }
  472. device->lldd_dev = sas_dev;
  473. hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
  474. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
  475. int phy_no;
  476. u8 phy_num = parent_dev->ex_dev.num_phys;
  477. struct ex_phy *phy;
  478. for (phy_no = 0; phy_no < phy_num; phy_no++) {
  479. phy = &parent_dev->ex_dev.ex_phy[phy_no];
  480. if (SAS_ADDR(phy->attached_sas_addr) ==
  481. SAS_ADDR(device->sas_addr)) {
  482. sas_dev->attached_phy = phy_no;
  483. break;
  484. }
  485. }
  486. if (phy_no == phy_num) {
  487. dev_info(dev, "dev found: no attached "
  488. "dev:%016llx at ex:%016llx\n",
  489. SAS_ADDR(device->sas_addr),
  490. SAS_ADDR(parent_dev->sas_addr));
  491. return -EINVAL;
  492. }
  493. }
  494. return 0;
  495. }
  496. static int hisi_sas_slave_configure(struct scsi_device *sdev)
  497. {
  498. struct domain_device *dev = sdev_to_domain_dev(sdev);
  499. int ret = sas_slave_configure(sdev);
  500. if (ret)
  501. return ret;
  502. if (!dev_is_sata(dev))
  503. sas_change_queue_depth(sdev, 64);
  504. return 0;
  505. }
  506. static void hisi_sas_scan_start(struct Scsi_Host *shost)
  507. {
  508. struct hisi_hba *hisi_hba = shost_priv(shost);
  509. hisi_hba->hw->phys_init(hisi_hba);
  510. }
  511. static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
  512. {
  513. struct hisi_hba *hisi_hba = shost_priv(shost);
  514. struct sas_ha_struct *sha = &hisi_hba->sha;
  515. /* Wait for PHY up interrupt to occur */
  516. if (time < HZ)
  517. return 0;
  518. sas_drain_work(sha);
  519. return 1;
  520. }
  521. static void hisi_sas_phyup_work(struct work_struct *work)
  522. {
  523. struct hisi_sas_phy *phy =
  524. container_of(work, struct hisi_sas_phy, phyup_ws);
  525. struct hisi_hba *hisi_hba = phy->hisi_hba;
  526. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  527. int phy_no = sas_phy->id;
  528. hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
  529. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  530. }
  531. static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
  532. {
  533. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  534. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  535. phy->hisi_hba = hisi_hba;
  536. phy->port = NULL;
  537. sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
  538. sas_phy->class = SAS;
  539. sas_phy->iproto = SAS_PROTOCOL_ALL;
  540. sas_phy->tproto = 0;
  541. sas_phy->type = PHY_TYPE_PHYSICAL;
  542. sas_phy->role = PHY_ROLE_INITIATOR;
  543. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  544. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  545. sas_phy->id = phy_no;
  546. sas_phy->sas_addr = &hisi_hba->sas_addr[0];
  547. sas_phy->frame_rcvd = &phy->frame_rcvd[0];
  548. sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
  549. sas_phy->lldd_phy = phy;
  550. INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
  551. }
  552. static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
  553. {
  554. struct sas_ha_struct *sas_ha = sas_phy->ha;
  555. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  556. struct hisi_sas_phy *phy = sas_phy->lldd_phy;
  557. struct asd_sas_port *sas_port = sas_phy->port;
  558. struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
  559. unsigned long flags;
  560. if (!sas_port)
  561. return;
  562. spin_lock_irqsave(&hisi_hba->lock, flags);
  563. port->port_attached = 1;
  564. port->id = phy->port_id;
  565. phy->port = port;
  566. sas_port->lldd_port = port;
  567. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  568. }
  569. static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
  570. struct hisi_sas_slot *slot)
  571. {
  572. if (task) {
  573. unsigned long flags;
  574. struct task_status_struct *ts;
  575. ts = &task->task_status;
  576. ts->resp = SAS_TASK_COMPLETE;
  577. ts->stat = SAS_ABORTED_TASK;
  578. spin_lock_irqsave(&task->task_state_lock, flags);
  579. task->task_state_flags &=
  580. ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
  581. task->task_state_flags |= SAS_TASK_STATE_DONE;
  582. spin_unlock_irqrestore(&task->task_state_lock, flags);
  583. }
  584. hisi_sas_slot_task_free(hisi_hba, task, slot);
  585. }
  586. /* hisi_hba.lock should be locked */
  587. static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
  588. struct domain_device *device)
  589. {
  590. struct hisi_sas_slot *slot, *slot2;
  591. struct hisi_sas_device *sas_dev = device->lldd_dev;
  592. list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
  593. hisi_sas_do_release_task(hisi_hba, slot->task, slot);
  594. }
  595. static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
  596. {
  597. struct hisi_sas_device *sas_dev;
  598. struct domain_device *device;
  599. int i;
  600. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  601. sas_dev = &hisi_hba->devices[i];
  602. device = sas_dev->sas_device;
  603. if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
  604. !device)
  605. continue;
  606. hisi_sas_release_task(hisi_hba, device);
  607. }
  608. }
  609. static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
  610. struct domain_device *device)
  611. {
  612. if (hisi_hba->hw->dereg_device)
  613. hisi_hba->hw->dereg_device(hisi_hba, device);
  614. }
  615. static void hisi_sas_dev_gone(struct domain_device *device)
  616. {
  617. struct hisi_sas_device *sas_dev = device->lldd_dev;
  618. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  619. struct device *dev = hisi_hba->dev;
  620. dev_info(dev, "found dev[%d:%x] is gone\n",
  621. sas_dev->device_id, sas_dev->dev_type);
  622. hisi_sas_internal_task_abort(hisi_hba, device,
  623. HISI_SAS_INT_ABT_DEV, 0);
  624. hisi_sas_dereg_device(hisi_hba, device);
  625. hisi_hba->hw->free_device(hisi_hba, sas_dev);
  626. device->lldd_dev = NULL;
  627. memset(sas_dev, 0, sizeof(*sas_dev));
  628. sas_dev->dev_type = SAS_PHY_UNUSED;
  629. }
  630. static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
  631. {
  632. return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
  633. }
  634. static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
  635. void *funcdata)
  636. {
  637. struct sas_ha_struct *sas_ha = sas_phy->ha;
  638. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  639. int phy_no = sas_phy->id;
  640. switch (func) {
  641. case PHY_FUNC_HARD_RESET:
  642. hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
  643. break;
  644. case PHY_FUNC_LINK_RESET:
  645. hisi_hba->hw->phy_disable(hisi_hba, phy_no);
  646. msleep(100);
  647. hisi_hba->hw->phy_start(hisi_hba, phy_no);
  648. break;
  649. case PHY_FUNC_DISABLE:
  650. hisi_hba->hw->phy_disable(hisi_hba, phy_no);
  651. break;
  652. case PHY_FUNC_SET_LINK_RATE:
  653. hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
  654. break;
  655. case PHY_FUNC_GET_EVENTS:
  656. if (hisi_hba->hw->get_events) {
  657. hisi_hba->hw->get_events(hisi_hba, phy_no);
  658. break;
  659. }
  660. /* fallthru */
  661. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  662. default:
  663. return -EOPNOTSUPP;
  664. }
  665. return 0;
  666. }
  667. static void hisi_sas_task_done(struct sas_task *task)
  668. {
  669. if (!del_timer(&task->slow_task->timer))
  670. return;
  671. complete(&task->slow_task->completion);
  672. }
  673. static void hisi_sas_tmf_timedout(struct timer_list *t)
  674. {
  675. struct sas_task_slow *slow = from_timer(slow, t, timer);
  676. struct sas_task *task = slow->task;
  677. unsigned long flags;
  678. spin_lock_irqsave(&task->task_state_lock, flags);
  679. if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
  680. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  681. spin_unlock_irqrestore(&task->task_state_lock, flags);
  682. complete(&task->slow_task->completion);
  683. }
  684. #define TASK_TIMEOUT 20
  685. #define TASK_RETRY 3
  686. static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
  687. void *parameter, u32 para_len,
  688. struct hisi_sas_tmf_task *tmf)
  689. {
  690. struct hisi_sas_device *sas_dev = device->lldd_dev;
  691. struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
  692. struct device *dev = hisi_hba->dev;
  693. struct sas_task *task;
  694. int res, retry;
  695. for (retry = 0; retry < TASK_RETRY; retry++) {
  696. task = sas_alloc_slow_task(GFP_KERNEL);
  697. if (!task)
  698. return -ENOMEM;
  699. task->dev = device;
  700. task->task_proto = device->tproto;
  701. if (dev_is_sata(device)) {
  702. task->ata_task.device_control_reg_update = 1;
  703. memcpy(&task->ata_task.fis, parameter, para_len);
  704. } else {
  705. memcpy(&task->ssp_task, parameter, para_len);
  706. }
  707. task->task_done = hisi_sas_task_done;
  708. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  709. task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
  710. add_timer(&task->slow_task->timer);
  711. res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
  712. if (res) {
  713. del_timer(&task->slow_task->timer);
  714. dev_err(dev, "abort tmf: executing internal task failed: %d\n",
  715. res);
  716. goto ex_err;
  717. }
  718. wait_for_completion(&task->slow_task->completion);
  719. res = TMF_RESP_FUNC_FAILED;
  720. /* Even TMF timed out, return direct. */
  721. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  722. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  723. struct hisi_sas_slot *slot = task->lldd_task;
  724. dev_err(dev, "abort tmf: TMF task timeout\n");
  725. if (slot)
  726. slot->task = NULL;
  727. goto ex_err;
  728. }
  729. }
  730. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  731. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  732. res = TMF_RESP_FUNC_COMPLETE;
  733. break;
  734. }
  735. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  736. task->task_status.stat == TMF_RESP_FUNC_SUCC) {
  737. res = TMF_RESP_FUNC_SUCC;
  738. break;
  739. }
  740. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  741. task->task_status.stat == SAS_DATA_UNDERRUN) {
  742. /* no error, but return the number of bytes of
  743. * underrun
  744. */
  745. dev_warn(dev, "abort tmf: task to dev %016llx "
  746. "resp: 0x%x sts 0x%x underrun\n",
  747. SAS_ADDR(device->sas_addr),
  748. task->task_status.resp,
  749. task->task_status.stat);
  750. res = task->task_status.residual;
  751. break;
  752. }
  753. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  754. task->task_status.stat == SAS_DATA_OVERRUN) {
  755. dev_warn(dev, "abort tmf: blocked task error\n");
  756. res = -EMSGSIZE;
  757. break;
  758. }
  759. dev_warn(dev, "abort tmf: task to dev "
  760. "%016llx resp: 0x%x status 0x%x\n",
  761. SAS_ADDR(device->sas_addr), task->task_status.resp,
  762. task->task_status.stat);
  763. sas_free_task(task);
  764. task = NULL;
  765. }
  766. ex_err:
  767. if (retry == TASK_RETRY)
  768. dev_warn(dev, "abort tmf: executing internal task failed!\n");
  769. sas_free_task(task);
  770. return res;
  771. }
  772. static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
  773. bool reset, int pmp, u8 *fis)
  774. {
  775. struct ata_taskfile tf;
  776. ata_tf_init(dev, &tf);
  777. if (reset)
  778. tf.ctl |= ATA_SRST;
  779. else
  780. tf.ctl &= ~ATA_SRST;
  781. tf.command = ATA_CMD_DEV_RESET;
  782. ata_tf_to_fis(&tf, pmp, 0, fis);
  783. }
  784. static int hisi_sas_softreset_ata_disk(struct domain_device *device)
  785. {
  786. u8 fis[20] = {0};
  787. struct ata_port *ap = device->sata_dev.ap;
  788. struct ata_link *link;
  789. int rc = TMF_RESP_FUNC_FAILED;
  790. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  791. struct device *dev = hisi_hba->dev;
  792. int s = sizeof(struct host_to_dev_fis);
  793. unsigned long flags;
  794. ata_for_each_link(link, ap, EDGE) {
  795. int pmp = sata_srst_pmp(link);
  796. hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
  797. rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
  798. if (rc != TMF_RESP_FUNC_COMPLETE)
  799. break;
  800. }
  801. if (rc == TMF_RESP_FUNC_COMPLETE) {
  802. ata_for_each_link(link, ap, EDGE) {
  803. int pmp = sata_srst_pmp(link);
  804. hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
  805. rc = hisi_sas_exec_internal_tmf_task(device, fis,
  806. s, NULL);
  807. if (rc != TMF_RESP_FUNC_COMPLETE)
  808. dev_err(dev, "ata disk de-reset failed\n");
  809. }
  810. } else {
  811. dev_err(dev, "ata disk reset failed\n");
  812. }
  813. if (rc == TMF_RESP_FUNC_COMPLETE) {
  814. spin_lock_irqsave(&hisi_hba->lock, flags);
  815. hisi_sas_release_task(hisi_hba, device);
  816. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  817. }
  818. return rc;
  819. }
  820. static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
  821. u8 *lun, struct hisi_sas_tmf_task *tmf)
  822. {
  823. struct sas_ssp_task ssp_task;
  824. if (!(device->tproto & SAS_PROTOCOL_SSP))
  825. return TMF_RESP_FUNC_ESUPP;
  826. memcpy(ssp_task.LUN, lun, 8);
  827. return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
  828. sizeof(ssp_task), tmf);
  829. }
  830. static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba,
  831. struct asd_sas_port *sas_port, enum sas_linkrate linkrate)
  832. {
  833. struct hisi_sas_device *sas_dev;
  834. struct domain_device *device;
  835. int i;
  836. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  837. sas_dev = &hisi_hba->devices[i];
  838. device = sas_dev->sas_device;
  839. if ((sas_dev->dev_type == SAS_PHY_UNUSED)
  840. || !device || (device->port != sas_port))
  841. continue;
  842. hisi_hba->hw->free_device(hisi_hba, sas_dev);
  843. /* Update linkrate of directly attached device. */
  844. if (!device->parent)
  845. device->linkrate = linkrate;
  846. hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
  847. }
  848. }
  849. static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
  850. u32 state)
  851. {
  852. struct sas_ha_struct *sas_ha = &hisi_hba->sha;
  853. struct asd_sas_port *_sas_port = NULL;
  854. int phy_no;
  855. for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
  856. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  857. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  858. struct asd_sas_port *sas_port = sas_phy->port;
  859. struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
  860. bool do_port_check = !!(_sas_port != sas_port);
  861. if (!sas_phy->phy->enabled)
  862. continue;
  863. /* Report PHY state change to libsas */
  864. if (state & (1 << phy_no)) {
  865. if (do_port_check && sas_port) {
  866. struct domain_device *dev = sas_port->port_dev;
  867. _sas_port = sas_port;
  868. port->id = phy->port_id;
  869. hisi_sas_refresh_port_id(hisi_hba,
  870. sas_port, sas_phy->linkrate);
  871. if (DEV_IS_EXPANDER(dev->dev_type))
  872. sas_ha->notify_port_event(sas_phy,
  873. PORTE_BROADCAST_RCVD);
  874. }
  875. } else if (old_state & (1 << phy_no))
  876. /* PHY down but was up before */
  877. hisi_sas_phy_down(hisi_hba, phy_no, 0);
  878. }
  879. drain_workqueue(hisi_hba->shost->work_q);
  880. }
  881. static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
  882. {
  883. struct device *dev = hisi_hba->dev;
  884. struct Scsi_Host *shost = hisi_hba->shost;
  885. u32 old_state, state;
  886. unsigned long flags;
  887. int rc;
  888. if (!hisi_hba->hw->soft_reset)
  889. return -1;
  890. if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
  891. return -1;
  892. dev_dbg(dev, "controller resetting...\n");
  893. old_state = hisi_hba->hw->get_phys_state(hisi_hba);
  894. scsi_block_requests(shost);
  895. set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  896. rc = hisi_hba->hw->soft_reset(hisi_hba);
  897. if (rc) {
  898. dev_warn(dev, "controller reset failed (%d)\n", rc);
  899. clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  900. goto out;
  901. }
  902. spin_lock_irqsave(&hisi_hba->lock, flags);
  903. hisi_sas_release_tasks(hisi_hba);
  904. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  905. clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
  906. /* Init and wait for PHYs to come up and all libsas event finished. */
  907. hisi_hba->hw->phys_init(hisi_hba);
  908. msleep(1000);
  909. drain_workqueue(hisi_hba->wq);
  910. drain_workqueue(shost->work_q);
  911. state = hisi_hba->hw->get_phys_state(hisi_hba);
  912. hisi_sas_rescan_topology(hisi_hba, old_state, state);
  913. dev_dbg(dev, "controller reset complete\n");
  914. out:
  915. scsi_unblock_requests(shost);
  916. clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
  917. return rc;
  918. }
  919. static int hisi_sas_abort_task(struct sas_task *task)
  920. {
  921. struct scsi_lun lun;
  922. struct hisi_sas_tmf_task tmf_task;
  923. struct domain_device *device = task->dev;
  924. struct hisi_sas_device *sas_dev = device->lldd_dev;
  925. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  926. struct device *dev = hisi_hba->dev;
  927. int rc = TMF_RESP_FUNC_FAILED;
  928. unsigned long flags;
  929. if (!sas_dev) {
  930. dev_warn(dev, "Device has been removed\n");
  931. return TMF_RESP_FUNC_FAILED;
  932. }
  933. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  934. rc = TMF_RESP_FUNC_COMPLETE;
  935. goto out;
  936. }
  937. sas_dev->dev_status = HISI_SAS_DEV_EH;
  938. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  939. struct scsi_cmnd *cmnd = task->uldd_task;
  940. struct hisi_sas_slot *slot = task->lldd_task;
  941. u32 tag = slot->idx;
  942. int rc2;
  943. int_to_scsilun(cmnd->device->lun, &lun);
  944. tmf_task.tmf = TMF_ABORT_TASK;
  945. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  946. rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
  947. &tmf_task);
  948. rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
  949. HISI_SAS_INT_ABT_CMD, tag);
  950. /*
  951. * If the TMF finds that the IO is not in the device and also
  952. * the internal abort does not succeed, then it is safe to
  953. * free the slot.
  954. * Note: if the internal abort succeeds then the slot
  955. * will have already been completed
  956. */
  957. if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
  958. if (task->lldd_task) {
  959. spin_lock_irqsave(&hisi_hba->lock, flags);
  960. hisi_sas_do_release_task(hisi_hba, task, slot);
  961. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  962. }
  963. }
  964. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  965. task->task_proto & SAS_PROTOCOL_STP) {
  966. if (task->dev->dev_type == SAS_SATA_DEV) {
  967. hisi_sas_internal_task_abort(hisi_hba, device,
  968. HISI_SAS_INT_ABT_DEV, 0);
  969. hisi_sas_dereg_device(hisi_hba, device);
  970. rc = hisi_sas_softreset_ata_disk(device);
  971. }
  972. } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
  973. /* SMP */
  974. struct hisi_sas_slot *slot = task->lldd_task;
  975. u32 tag = slot->idx;
  976. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  977. HISI_SAS_INT_ABT_CMD, tag);
  978. if (rc == TMF_RESP_FUNC_FAILED && task->lldd_task) {
  979. spin_lock_irqsave(&hisi_hba->lock, flags);
  980. hisi_sas_do_release_task(hisi_hba, task, slot);
  981. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  982. }
  983. }
  984. out:
  985. if (rc != TMF_RESP_FUNC_COMPLETE)
  986. dev_notice(dev, "abort task: rc=%d\n", rc);
  987. return rc;
  988. }
  989. static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
  990. {
  991. struct hisi_sas_tmf_task tmf_task;
  992. int rc = TMF_RESP_FUNC_FAILED;
  993. tmf_task.tmf = TMF_ABORT_TASK_SET;
  994. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  995. return rc;
  996. }
  997. static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
  998. {
  999. int rc = TMF_RESP_FUNC_FAILED;
  1000. struct hisi_sas_tmf_task tmf_task;
  1001. tmf_task.tmf = TMF_CLEAR_ACA;
  1002. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  1003. return rc;
  1004. }
  1005. static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
  1006. {
  1007. struct sas_phy *phy = sas_get_local_phy(device);
  1008. int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
  1009. (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
  1010. rc = sas_phy_reset(phy, reset_type);
  1011. sas_put_local_phy(phy);
  1012. msleep(2000);
  1013. return rc;
  1014. }
  1015. static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
  1016. {
  1017. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1018. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1019. unsigned long flags;
  1020. int rc = TMF_RESP_FUNC_FAILED;
  1021. if (sas_dev->dev_status != HISI_SAS_DEV_EH)
  1022. return TMF_RESP_FUNC_FAILED;
  1023. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  1024. hisi_sas_internal_task_abort(hisi_hba, device,
  1025. HISI_SAS_INT_ABT_DEV, 0);
  1026. hisi_sas_dereg_device(hisi_hba, device);
  1027. rc = hisi_sas_debug_I_T_nexus_reset(device);
  1028. if (rc == TMF_RESP_FUNC_COMPLETE) {
  1029. spin_lock_irqsave(&hisi_hba->lock, flags);
  1030. hisi_sas_release_task(hisi_hba, device);
  1031. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1032. }
  1033. return rc;
  1034. }
  1035. static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
  1036. {
  1037. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1038. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  1039. struct device *dev = hisi_hba->dev;
  1040. unsigned long flags;
  1041. int rc = TMF_RESP_FUNC_FAILED;
  1042. sas_dev->dev_status = HISI_SAS_DEV_EH;
  1043. if (dev_is_sata(device)) {
  1044. struct sas_phy *phy;
  1045. /* Clear internal IO and then hardreset */
  1046. rc = hisi_sas_internal_task_abort(hisi_hba, device,
  1047. HISI_SAS_INT_ABT_DEV, 0);
  1048. if (rc == TMF_RESP_FUNC_FAILED)
  1049. goto out;
  1050. hisi_sas_dereg_device(hisi_hba, device);
  1051. phy = sas_get_local_phy(device);
  1052. rc = sas_phy_reset(phy, 1);
  1053. if (rc == 0) {
  1054. spin_lock_irqsave(&hisi_hba->lock, flags);
  1055. hisi_sas_release_task(hisi_hba, device);
  1056. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1057. }
  1058. sas_put_local_phy(phy);
  1059. } else {
  1060. struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
  1061. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  1062. if (rc == TMF_RESP_FUNC_COMPLETE) {
  1063. spin_lock_irqsave(&hisi_hba->lock, flags);
  1064. hisi_sas_release_task(hisi_hba, device);
  1065. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1066. }
  1067. }
  1068. out:
  1069. if (rc != TMF_RESP_FUNC_COMPLETE)
  1070. dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
  1071. sas_dev->device_id, rc);
  1072. return rc;
  1073. }
  1074. static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
  1075. {
  1076. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  1077. return hisi_sas_controller_reset(hisi_hba);
  1078. }
  1079. static int hisi_sas_query_task(struct sas_task *task)
  1080. {
  1081. struct scsi_lun lun;
  1082. struct hisi_sas_tmf_task tmf_task;
  1083. int rc = TMF_RESP_FUNC_FAILED;
  1084. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  1085. struct scsi_cmnd *cmnd = task->uldd_task;
  1086. struct domain_device *device = task->dev;
  1087. struct hisi_sas_slot *slot = task->lldd_task;
  1088. u32 tag = slot->idx;
  1089. int_to_scsilun(cmnd->device->lun, &lun);
  1090. tmf_task.tmf = TMF_QUERY_TASK;
  1091. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  1092. rc = hisi_sas_debug_issue_ssp_tmf(device,
  1093. lun.scsi_lun,
  1094. &tmf_task);
  1095. switch (rc) {
  1096. /* The task is still in Lun, release it then */
  1097. case TMF_RESP_FUNC_SUCC:
  1098. /* The task is not in Lun or failed, reset the phy */
  1099. case TMF_RESP_FUNC_FAILED:
  1100. case TMF_RESP_FUNC_COMPLETE:
  1101. break;
  1102. default:
  1103. rc = TMF_RESP_FUNC_FAILED;
  1104. break;
  1105. }
  1106. }
  1107. return rc;
  1108. }
  1109. static int
  1110. hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
  1111. struct sas_task *task, int abort_flag,
  1112. int task_tag)
  1113. {
  1114. struct domain_device *device = task->dev;
  1115. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1116. struct device *dev = hisi_hba->dev;
  1117. struct hisi_sas_port *port;
  1118. struct hisi_sas_slot *slot;
  1119. struct asd_sas_port *sas_port = device->port;
  1120. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  1121. struct hisi_sas_dq *dq = sas_dev->dq;
  1122. int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
  1123. unsigned long flags, flags_dq;
  1124. if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
  1125. return -EINVAL;
  1126. if (!device->port)
  1127. return -1;
  1128. port = to_hisi_sas_port(sas_port);
  1129. /* simply get a slot and send abort command */
  1130. spin_lock_irqsave(&hisi_hba->lock, flags);
  1131. rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
  1132. if (rc) {
  1133. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1134. goto err_out;
  1135. }
  1136. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1137. spin_lock_irqsave(&dq->lock, flags_dq);
  1138. rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
  1139. if (rc)
  1140. goto err_out_tag;
  1141. dlvry_queue = dq->id;
  1142. dlvry_queue_slot = dq->wr_point;
  1143. slot = &hisi_hba->slot_info[slot_idx];
  1144. memset(slot, 0, sizeof(struct hisi_sas_slot));
  1145. slot->idx = slot_idx;
  1146. slot->n_elem = n_elem;
  1147. slot->dlvry_queue = dlvry_queue;
  1148. slot->dlvry_queue_slot = dlvry_queue_slot;
  1149. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  1150. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  1151. slot->task = task;
  1152. slot->port = port;
  1153. task->lldd_task = slot;
  1154. slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
  1155. GFP_ATOMIC, &slot->buf_dma);
  1156. if (!slot->buf) {
  1157. rc = -ENOMEM;
  1158. goto err_out_tag;
  1159. }
  1160. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  1161. memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
  1162. memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
  1163. rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
  1164. abort_flag, task_tag);
  1165. if (rc)
  1166. goto err_out_buf;
  1167. spin_lock_irqsave(&hisi_hba->lock, flags);
  1168. list_add_tail(&slot->entry, &sas_dev->list);
  1169. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1170. spin_lock_irqsave(&task->task_state_lock, flags);
  1171. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  1172. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1173. dq->slot_prep = slot;
  1174. atomic64_inc(&sas_dev->running_req);
  1175. /* send abort command to the chip */
  1176. hisi_hba->hw->start_delivery(dq);
  1177. spin_unlock_irqrestore(&dq->lock, flags_dq);
  1178. return 0;
  1179. err_out_buf:
  1180. dma_pool_free(hisi_hba->buffer_pool, slot->buf,
  1181. slot->buf_dma);
  1182. err_out_tag:
  1183. spin_lock_irqsave(&hisi_hba->lock, flags);
  1184. hisi_sas_slot_index_free(hisi_hba, slot_idx);
  1185. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  1186. spin_unlock_irqrestore(&dq->lock, flags_dq);
  1187. err_out:
  1188. dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
  1189. return rc;
  1190. }
  1191. /**
  1192. * hisi_sas_internal_task_abort -- execute an internal
  1193. * abort command for single IO command or a device
  1194. * @hisi_hba: host controller struct
  1195. * @device: domain device
  1196. * @abort_flag: mode of operation, device or single IO
  1197. * @tag: tag of IO to be aborted (only relevant to single
  1198. * IO mode)
  1199. */
  1200. static int
  1201. hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  1202. struct domain_device *device,
  1203. int abort_flag, int tag)
  1204. {
  1205. struct sas_task *task;
  1206. struct hisi_sas_device *sas_dev = device->lldd_dev;
  1207. struct device *dev = hisi_hba->dev;
  1208. int res;
  1209. if (!hisi_hba->hw->prep_abort)
  1210. return -EOPNOTSUPP;
  1211. task = sas_alloc_slow_task(GFP_KERNEL);
  1212. if (!task)
  1213. return -ENOMEM;
  1214. task->dev = device;
  1215. task->task_proto = device->tproto;
  1216. task->task_done = hisi_sas_task_done;
  1217. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  1218. task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
  1219. add_timer(&task->slow_task->timer);
  1220. res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
  1221. task, abort_flag, tag);
  1222. if (res) {
  1223. del_timer(&task->slow_task->timer);
  1224. dev_err(dev, "internal task abort: executing internal task failed: %d\n",
  1225. res);
  1226. goto exit;
  1227. }
  1228. wait_for_completion(&task->slow_task->completion);
  1229. res = TMF_RESP_FUNC_FAILED;
  1230. /* Internal abort timed out */
  1231. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1232. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1233. struct hisi_sas_slot *slot = task->lldd_task;
  1234. if (slot)
  1235. slot->task = NULL;
  1236. dev_err(dev, "internal task abort: timeout.\n");
  1237. goto exit;
  1238. }
  1239. }
  1240. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1241. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  1242. res = TMF_RESP_FUNC_COMPLETE;
  1243. goto exit;
  1244. }
  1245. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1246. task->task_status.stat == TMF_RESP_FUNC_SUCC) {
  1247. res = TMF_RESP_FUNC_SUCC;
  1248. goto exit;
  1249. }
  1250. exit:
  1251. dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
  1252. "resp: 0x%x sts 0x%x\n",
  1253. SAS_ADDR(device->sas_addr),
  1254. task,
  1255. task->task_status.resp, /* 0 is complete, -1 is undelivered */
  1256. task->task_status.stat);
  1257. sas_free_task(task);
  1258. return res;
  1259. }
  1260. static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
  1261. {
  1262. hisi_sas_port_notify_formed(sas_phy);
  1263. }
  1264. static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
  1265. {
  1266. phy->phy_attached = 0;
  1267. phy->phy_type = 0;
  1268. phy->port = NULL;
  1269. }
  1270. void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
  1271. {
  1272. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  1273. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1274. struct sas_ha_struct *sas_ha = &hisi_hba->sha;
  1275. if (rdy) {
  1276. /* Phy down but ready */
  1277. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  1278. hisi_sas_port_notify_formed(sas_phy);
  1279. } else {
  1280. struct hisi_sas_port *port = phy->port;
  1281. /* Phy down and not ready */
  1282. sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
  1283. sas_phy_disconnected(sas_phy);
  1284. if (port) {
  1285. if (phy->phy_type & PORT_TYPE_SAS) {
  1286. int port_id = port->id;
  1287. if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
  1288. port_id))
  1289. port->port_attached = 0;
  1290. } else if (phy->phy_type & PORT_TYPE_SATA)
  1291. port->port_attached = 0;
  1292. }
  1293. hisi_sas_phy_disconnected(phy);
  1294. }
  1295. }
  1296. EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
  1297. void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
  1298. {
  1299. int i;
  1300. for (i = 0; i < hisi_hba->queue_count; i++) {
  1301. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1302. tasklet_kill(&cq->tasklet);
  1303. }
  1304. }
  1305. EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
  1306. struct scsi_transport_template *hisi_sas_stt;
  1307. EXPORT_SYMBOL_GPL(hisi_sas_stt);
  1308. static struct scsi_host_template _hisi_sas_sht = {
  1309. .module = THIS_MODULE,
  1310. .name = DRV_NAME,
  1311. .queuecommand = sas_queuecommand,
  1312. .target_alloc = sas_target_alloc,
  1313. .slave_configure = hisi_sas_slave_configure,
  1314. .scan_finished = hisi_sas_scan_finished,
  1315. .scan_start = hisi_sas_scan_start,
  1316. .change_queue_depth = sas_change_queue_depth,
  1317. .bios_param = sas_bios_param,
  1318. .can_queue = 1,
  1319. .this_id = -1,
  1320. .sg_tablesize = SG_ALL,
  1321. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  1322. .use_clustering = ENABLE_CLUSTERING,
  1323. .eh_device_reset_handler = sas_eh_device_reset_handler,
  1324. .eh_target_reset_handler = sas_eh_target_reset_handler,
  1325. .target_destroy = sas_target_destroy,
  1326. .ioctl = sas_ioctl,
  1327. };
  1328. struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
  1329. EXPORT_SYMBOL_GPL(hisi_sas_sht);
  1330. static struct sas_domain_function_template hisi_sas_transport_ops = {
  1331. .lldd_dev_found = hisi_sas_dev_found,
  1332. .lldd_dev_gone = hisi_sas_dev_gone,
  1333. .lldd_execute_task = hisi_sas_queue_command,
  1334. .lldd_control_phy = hisi_sas_control_phy,
  1335. .lldd_abort_task = hisi_sas_abort_task,
  1336. .lldd_abort_task_set = hisi_sas_abort_task_set,
  1337. .lldd_clear_aca = hisi_sas_clear_aca,
  1338. .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
  1339. .lldd_lu_reset = hisi_sas_lu_reset,
  1340. .lldd_query_task = hisi_sas_query_task,
  1341. .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
  1342. .lldd_port_formed = hisi_sas_port_formed,
  1343. };
  1344. void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
  1345. {
  1346. int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
  1347. for (i = 0; i < hisi_hba->queue_count; i++) {
  1348. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1349. struct hisi_sas_dq *dq = &hisi_hba->dq[i];
  1350. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1351. memset(hisi_hba->cmd_hdr[i], 0, s);
  1352. dq->wr_point = 0;
  1353. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1354. memset(hisi_hba->complete_hdr[i], 0, s);
  1355. cq->rd_point = 0;
  1356. }
  1357. s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
  1358. memset(hisi_hba->initial_fis, 0, s);
  1359. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1360. memset(hisi_hba->iost, 0, s);
  1361. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1362. memset(hisi_hba->breakpoint, 0, s);
  1363. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
  1364. memset(hisi_hba->sata_breakpoint, 0, s);
  1365. }
  1366. EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
  1367. int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
  1368. {
  1369. struct device *dev = hisi_hba->dev;
  1370. int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
  1371. spin_lock_init(&hisi_hba->lock);
  1372. for (i = 0; i < hisi_hba->n_phy; i++) {
  1373. hisi_sas_phy_init(hisi_hba, i);
  1374. hisi_hba->port[i].port_attached = 0;
  1375. hisi_hba->port[i].id = -1;
  1376. }
  1377. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1378. hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
  1379. hisi_hba->devices[i].device_id = i;
  1380. hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
  1381. }
  1382. for (i = 0; i < hisi_hba->queue_count; i++) {
  1383. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1384. struct hisi_sas_dq *dq = &hisi_hba->dq[i];
  1385. /* Completion queue structure */
  1386. cq->id = i;
  1387. cq->hisi_hba = hisi_hba;
  1388. /* Delivery queue structure */
  1389. dq->id = i;
  1390. dq->hisi_hba = hisi_hba;
  1391. /* Delivery queue */
  1392. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1393. hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
  1394. &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
  1395. if (!hisi_hba->cmd_hdr[i])
  1396. goto err_out;
  1397. /* Completion queue */
  1398. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1399. hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
  1400. &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
  1401. if (!hisi_hba->complete_hdr[i])
  1402. goto err_out;
  1403. }
  1404. s = sizeof(struct hisi_sas_slot_buf_table);
  1405. hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
  1406. if (!hisi_hba->buffer_pool)
  1407. goto err_out;
  1408. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  1409. hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
  1410. GFP_KERNEL);
  1411. if (!hisi_hba->itct)
  1412. goto err_out;
  1413. memset(hisi_hba->itct, 0, s);
  1414. hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
  1415. sizeof(struct hisi_sas_slot),
  1416. GFP_KERNEL);
  1417. if (!hisi_hba->slot_info)
  1418. goto err_out;
  1419. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1420. hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
  1421. GFP_KERNEL);
  1422. if (!hisi_hba->iost)
  1423. goto err_out;
  1424. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1425. hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
  1426. &hisi_hba->breakpoint_dma, GFP_KERNEL);
  1427. if (!hisi_hba->breakpoint)
  1428. goto err_out;
  1429. hisi_hba->slot_index_count = max_command_entries;
  1430. s = hisi_hba->slot_index_count / BITS_PER_BYTE;
  1431. hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
  1432. if (!hisi_hba->slot_index_tags)
  1433. goto err_out;
  1434. s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
  1435. hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
  1436. &hisi_hba->initial_fis_dma, GFP_KERNEL);
  1437. if (!hisi_hba->initial_fis)
  1438. goto err_out;
  1439. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
  1440. hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
  1441. &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
  1442. if (!hisi_hba->sata_breakpoint)
  1443. goto err_out;
  1444. hisi_sas_init_mem(hisi_hba);
  1445. hisi_sas_slot_index_init(hisi_hba);
  1446. hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
  1447. if (!hisi_hba->wq) {
  1448. dev_err(dev, "sas_alloc: failed to create workqueue\n");
  1449. goto err_out;
  1450. }
  1451. return 0;
  1452. err_out:
  1453. return -ENOMEM;
  1454. }
  1455. EXPORT_SYMBOL_GPL(hisi_sas_alloc);
  1456. void hisi_sas_free(struct hisi_hba *hisi_hba)
  1457. {
  1458. struct device *dev = hisi_hba->dev;
  1459. int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
  1460. for (i = 0; i < hisi_hba->queue_count; i++) {
  1461. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1462. if (hisi_hba->cmd_hdr[i])
  1463. dma_free_coherent(dev, s,
  1464. hisi_hba->cmd_hdr[i],
  1465. hisi_hba->cmd_hdr_dma[i]);
  1466. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1467. if (hisi_hba->complete_hdr[i])
  1468. dma_free_coherent(dev, s,
  1469. hisi_hba->complete_hdr[i],
  1470. hisi_hba->complete_hdr_dma[i]);
  1471. }
  1472. dma_pool_destroy(hisi_hba->buffer_pool);
  1473. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  1474. if (hisi_hba->itct)
  1475. dma_free_coherent(dev, s,
  1476. hisi_hba->itct, hisi_hba->itct_dma);
  1477. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1478. if (hisi_hba->iost)
  1479. dma_free_coherent(dev, s,
  1480. hisi_hba->iost, hisi_hba->iost_dma);
  1481. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1482. if (hisi_hba->breakpoint)
  1483. dma_free_coherent(dev, s,
  1484. hisi_hba->breakpoint,
  1485. hisi_hba->breakpoint_dma);
  1486. s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
  1487. if (hisi_hba->initial_fis)
  1488. dma_free_coherent(dev, s,
  1489. hisi_hba->initial_fis,
  1490. hisi_hba->initial_fis_dma);
  1491. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
  1492. if (hisi_hba->sata_breakpoint)
  1493. dma_free_coherent(dev, s,
  1494. hisi_hba->sata_breakpoint,
  1495. hisi_hba->sata_breakpoint_dma);
  1496. if (hisi_hba->wq)
  1497. destroy_workqueue(hisi_hba->wq);
  1498. }
  1499. EXPORT_SYMBOL_GPL(hisi_sas_free);
  1500. void hisi_sas_rst_work_handler(struct work_struct *work)
  1501. {
  1502. struct hisi_hba *hisi_hba =
  1503. container_of(work, struct hisi_hba, rst_work);
  1504. hisi_sas_controller_reset(hisi_hba);
  1505. }
  1506. EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
  1507. int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
  1508. {
  1509. struct device *dev = hisi_hba->dev;
  1510. struct platform_device *pdev = hisi_hba->platform_dev;
  1511. struct device_node *np = pdev ? pdev->dev.of_node : NULL;
  1512. struct clk *refclk;
  1513. if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
  1514. SAS_ADDR_SIZE)) {
  1515. dev_err(dev, "could not get property sas-addr\n");
  1516. return -ENOENT;
  1517. }
  1518. if (np) {
  1519. /*
  1520. * These properties are only required for platform device-based
  1521. * controller with DT firmware.
  1522. */
  1523. hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
  1524. "hisilicon,sas-syscon");
  1525. if (IS_ERR(hisi_hba->ctrl)) {
  1526. dev_err(dev, "could not get syscon\n");
  1527. return -ENOENT;
  1528. }
  1529. if (device_property_read_u32(dev, "ctrl-reset-reg",
  1530. &hisi_hba->ctrl_reset_reg)) {
  1531. dev_err(dev,
  1532. "could not get property ctrl-reset-reg\n");
  1533. return -ENOENT;
  1534. }
  1535. if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
  1536. &hisi_hba->ctrl_reset_sts_reg)) {
  1537. dev_err(dev,
  1538. "could not get property ctrl-reset-sts-reg\n");
  1539. return -ENOENT;
  1540. }
  1541. if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
  1542. &hisi_hba->ctrl_clock_ena_reg)) {
  1543. dev_err(dev,
  1544. "could not get property ctrl-clock-ena-reg\n");
  1545. return -ENOENT;
  1546. }
  1547. }
  1548. refclk = devm_clk_get(dev, NULL);
  1549. if (IS_ERR(refclk))
  1550. dev_dbg(dev, "no ref clk property\n");
  1551. else
  1552. hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
  1553. if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
  1554. dev_err(dev, "could not get property phy-count\n");
  1555. return -ENOENT;
  1556. }
  1557. if (device_property_read_u32(dev, "queue-count",
  1558. &hisi_hba->queue_count)) {
  1559. dev_err(dev, "could not get property queue-count\n");
  1560. return -ENOENT;
  1561. }
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
  1565. static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
  1566. const struct hisi_sas_hw *hw)
  1567. {
  1568. struct resource *res;
  1569. struct Scsi_Host *shost;
  1570. struct hisi_hba *hisi_hba;
  1571. struct device *dev = &pdev->dev;
  1572. shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
  1573. if (!shost) {
  1574. dev_err(dev, "scsi host alloc failed\n");
  1575. return NULL;
  1576. }
  1577. hisi_hba = shost_priv(shost);
  1578. INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
  1579. hisi_hba->hw = hw;
  1580. hisi_hba->dev = dev;
  1581. hisi_hba->platform_dev = pdev;
  1582. hisi_hba->shost = shost;
  1583. SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
  1584. timer_setup(&hisi_hba->timer, NULL, 0);
  1585. if (hisi_sas_get_fw_info(hisi_hba) < 0)
  1586. goto err_out;
  1587. if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
  1588. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
  1589. dev_err(dev, "No usable DMA addressing method\n");
  1590. goto err_out;
  1591. }
  1592. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1593. hisi_hba->regs = devm_ioremap_resource(dev, res);
  1594. if (IS_ERR(hisi_hba->regs))
  1595. goto err_out;
  1596. if (hisi_sas_alloc(hisi_hba, shost)) {
  1597. hisi_sas_free(hisi_hba);
  1598. goto err_out;
  1599. }
  1600. return shost;
  1601. err_out:
  1602. scsi_host_put(shost);
  1603. dev_err(dev, "shost alloc failed\n");
  1604. return NULL;
  1605. }
  1606. void hisi_sas_init_add(struct hisi_hba *hisi_hba)
  1607. {
  1608. int i;
  1609. for (i = 0; i < hisi_hba->n_phy; i++)
  1610. memcpy(&hisi_hba->phy[i].dev_sas_addr,
  1611. hisi_hba->sas_addr,
  1612. SAS_ADDR_SIZE);
  1613. }
  1614. EXPORT_SYMBOL_GPL(hisi_sas_init_add);
  1615. int hisi_sas_probe(struct platform_device *pdev,
  1616. const struct hisi_sas_hw *hw)
  1617. {
  1618. struct Scsi_Host *shost;
  1619. struct hisi_hba *hisi_hba;
  1620. struct device *dev = &pdev->dev;
  1621. struct asd_sas_phy **arr_phy;
  1622. struct asd_sas_port **arr_port;
  1623. struct sas_ha_struct *sha;
  1624. int rc, phy_nr, port_nr, i;
  1625. shost = hisi_sas_shost_alloc(pdev, hw);
  1626. if (!shost)
  1627. return -ENOMEM;
  1628. sha = SHOST_TO_SAS_HA(shost);
  1629. hisi_hba = shost_priv(shost);
  1630. platform_set_drvdata(pdev, sha);
  1631. phy_nr = port_nr = hisi_hba->n_phy;
  1632. arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
  1633. arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
  1634. if (!arr_phy || !arr_port) {
  1635. rc = -ENOMEM;
  1636. goto err_out_ha;
  1637. }
  1638. sha->sas_phy = arr_phy;
  1639. sha->sas_port = arr_port;
  1640. sha->lldd_ha = hisi_hba;
  1641. shost->transportt = hisi_sas_stt;
  1642. shost->max_id = HISI_SAS_MAX_DEVICES;
  1643. shost->max_lun = ~0;
  1644. shost->max_channel = 1;
  1645. shost->max_cmd_len = 16;
  1646. shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
  1647. shost->can_queue = hisi_hba->hw->max_command_entries;
  1648. shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
  1649. sha->sas_ha_name = DRV_NAME;
  1650. sha->dev = hisi_hba->dev;
  1651. sha->lldd_module = THIS_MODULE;
  1652. sha->sas_addr = &hisi_hba->sas_addr[0];
  1653. sha->num_phys = hisi_hba->n_phy;
  1654. sha->core.shost = hisi_hba->shost;
  1655. for (i = 0; i < hisi_hba->n_phy; i++) {
  1656. sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
  1657. sha->sas_port[i] = &hisi_hba->port[i].sas_port;
  1658. }
  1659. hisi_sas_init_add(hisi_hba);
  1660. rc = scsi_add_host(shost, &pdev->dev);
  1661. if (rc)
  1662. goto err_out_ha;
  1663. rc = sas_register_ha(sha);
  1664. if (rc)
  1665. goto err_out_register_ha;
  1666. rc = hisi_hba->hw->hw_init(hisi_hba);
  1667. if (rc)
  1668. goto err_out_register_ha;
  1669. scsi_scan_host(shost);
  1670. return 0;
  1671. err_out_register_ha:
  1672. scsi_remove_host(shost);
  1673. err_out_ha:
  1674. hisi_sas_free(hisi_hba);
  1675. scsi_host_put(shost);
  1676. return rc;
  1677. }
  1678. EXPORT_SYMBOL_GPL(hisi_sas_probe);
  1679. int hisi_sas_remove(struct platform_device *pdev)
  1680. {
  1681. struct sas_ha_struct *sha = platform_get_drvdata(pdev);
  1682. struct hisi_hba *hisi_hba = sha->lldd_ha;
  1683. struct Scsi_Host *shost = sha->core.shost;
  1684. sas_unregister_ha(sha);
  1685. sas_remove_host(sha->core.shost);
  1686. hisi_sas_free(hisi_hba);
  1687. scsi_host_put(shost);
  1688. return 0;
  1689. }
  1690. EXPORT_SYMBOL_GPL(hisi_sas_remove);
  1691. static __init int hisi_sas_init(void)
  1692. {
  1693. hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
  1694. if (!hisi_sas_stt)
  1695. return -ENOMEM;
  1696. return 0;
  1697. }
  1698. static __exit void hisi_sas_exit(void)
  1699. {
  1700. sas_release_transport(hisi_sas_stt);
  1701. }
  1702. module_init(hisi_sas_init);
  1703. module_exit(hisi_sas_exit);
  1704. MODULE_LICENSE("GPL");
  1705. MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
  1706. MODULE_DESCRIPTION("HISILICON SAS controller driver");
  1707. MODULE_ALIAS("platform:" DRV_NAME);