hisi_sas_main.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  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 struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
  22. {
  23. return device->port->ha->lldd_ha;
  24. }
  25. static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
  26. {
  27. void *bitmap = hisi_hba->slot_index_tags;
  28. clear_bit(slot_idx, bitmap);
  29. }
  30. static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
  31. {
  32. hisi_sas_slot_index_clear(hisi_hba, slot_idx);
  33. }
  34. static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
  35. {
  36. void *bitmap = hisi_hba->slot_index_tags;
  37. set_bit(slot_idx, bitmap);
  38. }
  39. static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
  40. {
  41. unsigned int index;
  42. void *bitmap = hisi_hba->slot_index_tags;
  43. index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
  44. if (index >= hisi_hba->slot_index_count)
  45. return -SAS_QUEUE_FULL;
  46. hisi_sas_slot_index_set(hisi_hba, index);
  47. *slot_idx = index;
  48. return 0;
  49. }
  50. static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
  51. {
  52. int i;
  53. for (i = 0; i < hisi_hba->slot_index_count; ++i)
  54. hisi_sas_slot_index_clear(hisi_hba, i);
  55. }
  56. void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
  57. struct hisi_sas_slot *slot)
  58. {
  59. struct device *dev = &hisi_hba->pdev->dev;
  60. if (!slot->task)
  61. return;
  62. if (!sas_protocol_ata(task->task_proto))
  63. if (slot->n_elem)
  64. dma_unmap_sg(dev, task->scatter, slot->n_elem,
  65. task->data_dir);
  66. if (slot->command_table)
  67. dma_pool_free(hisi_hba->command_table_pool,
  68. slot->command_table, slot->command_table_dma);
  69. if (slot->status_buffer)
  70. dma_pool_free(hisi_hba->status_buffer_pool,
  71. slot->status_buffer, slot->status_buffer_dma);
  72. if (slot->sge_page)
  73. dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
  74. slot->sge_page_dma);
  75. list_del_init(&slot->entry);
  76. task->lldd_task = NULL;
  77. slot->task = NULL;
  78. slot->port = NULL;
  79. hisi_sas_slot_index_free(hisi_hba, slot->idx);
  80. /* slot memory is fully zeroed when it is reused */
  81. }
  82. EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
  83. static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
  84. struct hisi_sas_slot *slot)
  85. {
  86. return hisi_hba->hw->prep_smp(hisi_hba, slot);
  87. }
  88. static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
  89. struct hisi_sas_slot *slot, int is_tmf,
  90. struct hisi_sas_tmf_task *tmf)
  91. {
  92. return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
  93. }
  94. static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
  95. struct hisi_sas_slot *slot)
  96. {
  97. return hisi_hba->hw->prep_stp(hisi_hba, slot);
  98. }
  99. static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
  100. struct hisi_sas_slot *slot,
  101. int device_id, int abort_flag, int tag_to_abort)
  102. {
  103. return hisi_hba->hw->prep_abort(hisi_hba, slot,
  104. device_id, abort_flag, tag_to_abort);
  105. }
  106. /*
  107. * This function will issue an abort TMF regardless of whether the
  108. * task is in the sdev or not. Then it will do the task complete
  109. * cleanup and callbacks.
  110. */
  111. static void hisi_sas_slot_abort(struct work_struct *work)
  112. {
  113. struct hisi_sas_slot *abort_slot =
  114. container_of(work, struct hisi_sas_slot, abort_slot);
  115. struct sas_task *task = abort_slot->task;
  116. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  117. struct scsi_cmnd *cmnd = task->uldd_task;
  118. struct hisi_sas_tmf_task tmf_task;
  119. struct domain_device *device = task->dev;
  120. struct hisi_sas_device *sas_dev = device->lldd_dev;
  121. struct scsi_lun lun;
  122. struct device *dev = &hisi_hba->pdev->dev;
  123. int tag = abort_slot->idx;
  124. if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
  125. dev_err(dev, "cannot abort slot for non-ssp task\n");
  126. goto out;
  127. }
  128. int_to_scsilun(cmnd->device->lun, &lun);
  129. tmf_task.tmf = TMF_ABORT_TASK;
  130. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  131. hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
  132. out:
  133. /* Do cleanup for this task */
  134. hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
  135. if (task->task_done)
  136. task->task_done(task);
  137. if (sas_dev && sas_dev->running_req)
  138. sas_dev->running_req--;
  139. }
  140. static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
  141. int is_tmf, struct hisi_sas_tmf_task *tmf,
  142. int *pass)
  143. {
  144. struct domain_device *device = task->dev;
  145. struct hisi_sas_device *sas_dev = device->lldd_dev;
  146. struct hisi_sas_port *port;
  147. struct hisi_sas_slot *slot;
  148. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  149. struct device *dev = &hisi_hba->pdev->dev;
  150. int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
  151. if (!device->port) {
  152. struct task_status_struct *ts = &task->task_status;
  153. ts->resp = SAS_TASK_UNDELIVERED;
  154. ts->stat = SAS_PHY_DOWN;
  155. /*
  156. * libsas will use dev->port, should
  157. * not call task_done for sata
  158. */
  159. if (device->dev_type != SAS_SATA_DEV)
  160. task->task_done(task);
  161. return 0;
  162. }
  163. if (DEV_IS_GONE(sas_dev)) {
  164. if (sas_dev)
  165. dev_info(dev, "task prep: device %llu not ready\n",
  166. sas_dev->device_id);
  167. else
  168. dev_info(dev, "task prep: device %016llx not ready\n",
  169. SAS_ADDR(device->sas_addr));
  170. rc = SAS_PHY_DOWN;
  171. return rc;
  172. }
  173. port = device->port->lldd_port;
  174. if (port && !port->port_attached) {
  175. dev_info(dev, "task prep: %s port%d not attach device\n",
  176. (sas_protocol_ata(task->task_proto)) ?
  177. "SATA/STP" : "SAS",
  178. device->port->id);
  179. return SAS_PHY_DOWN;
  180. }
  181. if (!sas_protocol_ata(task->task_proto)) {
  182. if (task->num_scatter) {
  183. n_elem = dma_map_sg(dev, task->scatter,
  184. task->num_scatter, task->data_dir);
  185. if (!n_elem) {
  186. rc = -ENOMEM;
  187. goto prep_out;
  188. }
  189. }
  190. } else
  191. n_elem = task->num_scatter;
  192. if (hisi_hba->hw->slot_index_alloc)
  193. rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
  194. device);
  195. else
  196. rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
  197. if (rc)
  198. goto err_out;
  199. rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue,
  200. &dlvry_queue_slot);
  201. if (rc)
  202. goto err_out_tag;
  203. slot = &hisi_hba->slot_info[slot_idx];
  204. memset(slot, 0, sizeof(struct hisi_sas_slot));
  205. slot->idx = slot_idx;
  206. slot->n_elem = n_elem;
  207. slot->dlvry_queue = dlvry_queue;
  208. slot->dlvry_queue_slot = dlvry_queue_slot;
  209. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  210. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  211. slot->task = task;
  212. slot->port = port;
  213. task->lldd_task = slot;
  214. INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
  215. slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
  216. GFP_ATOMIC,
  217. &slot->status_buffer_dma);
  218. if (!slot->status_buffer) {
  219. rc = -ENOMEM;
  220. goto err_out_slot_buf;
  221. }
  222. memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
  223. slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
  224. GFP_ATOMIC,
  225. &slot->command_table_dma);
  226. if (!slot->command_table) {
  227. rc = -ENOMEM;
  228. goto err_out_status_buf;
  229. }
  230. memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
  231. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  232. switch (task->task_proto) {
  233. case SAS_PROTOCOL_SMP:
  234. rc = hisi_sas_task_prep_smp(hisi_hba, slot);
  235. break;
  236. case SAS_PROTOCOL_SSP:
  237. rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
  238. break;
  239. case SAS_PROTOCOL_SATA:
  240. case SAS_PROTOCOL_STP:
  241. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  242. rc = hisi_sas_task_prep_ata(hisi_hba, slot);
  243. break;
  244. default:
  245. dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
  246. task->task_proto);
  247. rc = -EINVAL;
  248. break;
  249. }
  250. if (rc) {
  251. dev_err(dev, "task prep: rc = 0x%x\n", rc);
  252. if (slot->sge_page)
  253. goto err_out_sge;
  254. goto err_out_command_table;
  255. }
  256. list_add_tail(&slot->entry, &port->list);
  257. spin_lock(&task->task_state_lock);
  258. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  259. spin_unlock(&task->task_state_lock);
  260. hisi_hba->slot_prep = slot;
  261. sas_dev->running_req++;
  262. ++(*pass);
  263. return 0;
  264. err_out_sge:
  265. dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
  266. slot->sge_page_dma);
  267. err_out_command_table:
  268. dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
  269. slot->command_table_dma);
  270. err_out_status_buf:
  271. dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
  272. slot->status_buffer_dma);
  273. err_out_slot_buf:
  274. /* Nothing to be done */
  275. err_out_tag:
  276. hisi_sas_slot_index_free(hisi_hba, slot_idx);
  277. err_out:
  278. dev_err(dev, "task prep: failed[%d]!\n", rc);
  279. if (!sas_protocol_ata(task->task_proto))
  280. if (n_elem)
  281. dma_unmap_sg(dev, task->scatter, n_elem,
  282. task->data_dir);
  283. prep_out:
  284. return rc;
  285. }
  286. static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
  287. int is_tmf, struct hisi_sas_tmf_task *tmf)
  288. {
  289. u32 rc;
  290. u32 pass = 0;
  291. unsigned long flags;
  292. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  293. struct device *dev = &hisi_hba->pdev->dev;
  294. /* protect task_prep and start_delivery sequence */
  295. spin_lock_irqsave(&hisi_hba->lock, flags);
  296. rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
  297. if (rc)
  298. dev_err(dev, "task exec: failed[%d]!\n", rc);
  299. if (likely(pass))
  300. hisi_hba->hw->start_delivery(hisi_hba);
  301. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  302. return rc;
  303. }
  304. static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
  305. {
  306. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  307. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  308. struct sas_ha_struct *sas_ha;
  309. if (!phy->phy_attached)
  310. return;
  311. sas_ha = &hisi_hba->sha;
  312. sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
  313. if (sas_phy->phy) {
  314. struct sas_phy *sphy = sas_phy->phy;
  315. sphy->negotiated_linkrate = sas_phy->linkrate;
  316. sphy->minimum_linkrate = phy->minimum_linkrate;
  317. sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  318. sphy->maximum_linkrate = phy->maximum_linkrate;
  319. }
  320. if (phy->phy_type & PORT_TYPE_SAS) {
  321. struct sas_identify_frame *id;
  322. id = (struct sas_identify_frame *)phy->frame_rcvd;
  323. id->dev_type = phy->identify.device_type;
  324. id->initiator_bits = SAS_PROTOCOL_ALL;
  325. id->target_bits = phy->identify.target_port_protocols;
  326. } else if (phy->phy_type & PORT_TYPE_SATA) {
  327. /*Nothing*/
  328. }
  329. sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
  330. sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
  331. }
  332. static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
  333. {
  334. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  335. struct hisi_sas_device *sas_dev = NULL;
  336. int i;
  337. spin_lock(&hisi_hba->lock);
  338. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  339. if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
  340. hisi_hba->devices[i].device_id = i;
  341. sas_dev = &hisi_hba->devices[i];
  342. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  343. sas_dev->dev_type = device->dev_type;
  344. sas_dev->hisi_hba = hisi_hba;
  345. sas_dev->sas_device = device;
  346. break;
  347. }
  348. }
  349. spin_unlock(&hisi_hba->lock);
  350. return sas_dev;
  351. }
  352. static int hisi_sas_dev_found(struct domain_device *device)
  353. {
  354. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  355. struct domain_device *parent_dev = device->parent;
  356. struct hisi_sas_device *sas_dev;
  357. struct device *dev = &hisi_hba->pdev->dev;
  358. if (hisi_hba->hw->alloc_dev)
  359. sas_dev = hisi_hba->hw->alloc_dev(device);
  360. else
  361. sas_dev = hisi_sas_alloc_dev(device);
  362. if (!sas_dev) {
  363. dev_err(dev, "fail alloc dev: max support %d devices\n",
  364. HISI_SAS_MAX_DEVICES);
  365. return -EINVAL;
  366. }
  367. device->lldd_dev = sas_dev;
  368. hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
  369. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
  370. int phy_no;
  371. u8 phy_num = parent_dev->ex_dev.num_phys;
  372. struct ex_phy *phy;
  373. for (phy_no = 0; phy_no < phy_num; phy_no++) {
  374. phy = &parent_dev->ex_dev.ex_phy[phy_no];
  375. if (SAS_ADDR(phy->attached_sas_addr) ==
  376. SAS_ADDR(device->sas_addr)) {
  377. sas_dev->attached_phy = phy_no;
  378. break;
  379. }
  380. }
  381. if (phy_no == phy_num) {
  382. dev_info(dev, "dev found: no attached "
  383. "dev:%016llx at ex:%016llx\n",
  384. SAS_ADDR(device->sas_addr),
  385. SAS_ADDR(parent_dev->sas_addr));
  386. return -EINVAL;
  387. }
  388. }
  389. return 0;
  390. }
  391. static int hisi_sas_slave_configure(struct scsi_device *sdev)
  392. {
  393. struct domain_device *dev = sdev_to_domain_dev(sdev);
  394. int ret = sas_slave_configure(sdev);
  395. if (ret)
  396. return ret;
  397. if (!dev_is_sata(dev))
  398. sas_change_queue_depth(sdev, 64);
  399. return 0;
  400. }
  401. static void hisi_sas_scan_start(struct Scsi_Host *shost)
  402. {
  403. struct hisi_hba *hisi_hba = shost_priv(shost);
  404. int i;
  405. for (i = 0; i < hisi_hba->n_phy; ++i)
  406. hisi_sas_bytes_dmaed(hisi_hba, i);
  407. hisi_hba->scan_finished = 1;
  408. }
  409. static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
  410. {
  411. struct hisi_hba *hisi_hba = shost_priv(shost);
  412. struct sas_ha_struct *sha = &hisi_hba->sha;
  413. if (hisi_hba->scan_finished == 0)
  414. return 0;
  415. sas_drain_work(sha);
  416. return 1;
  417. }
  418. static void hisi_sas_phyup_work(struct work_struct *work)
  419. {
  420. struct hisi_sas_phy *phy =
  421. container_of(work, struct hisi_sas_phy, phyup_ws);
  422. struct hisi_hba *hisi_hba = phy->hisi_hba;
  423. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  424. int phy_no = sas_phy->id;
  425. hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
  426. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  427. }
  428. static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
  429. {
  430. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  431. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  432. phy->hisi_hba = hisi_hba;
  433. phy->port = NULL;
  434. init_timer(&phy->timer);
  435. sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
  436. sas_phy->class = SAS;
  437. sas_phy->iproto = SAS_PROTOCOL_ALL;
  438. sas_phy->tproto = 0;
  439. sas_phy->type = PHY_TYPE_PHYSICAL;
  440. sas_phy->role = PHY_ROLE_INITIATOR;
  441. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  442. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  443. sas_phy->id = phy_no;
  444. sas_phy->sas_addr = &hisi_hba->sas_addr[0];
  445. sas_phy->frame_rcvd = &phy->frame_rcvd[0];
  446. sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
  447. sas_phy->lldd_phy = phy;
  448. INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
  449. }
  450. static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
  451. {
  452. struct sas_ha_struct *sas_ha = sas_phy->ha;
  453. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  454. struct hisi_sas_phy *phy = sas_phy->lldd_phy;
  455. struct asd_sas_port *sas_port = sas_phy->port;
  456. struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id];
  457. unsigned long flags;
  458. if (!sas_port)
  459. return;
  460. spin_lock_irqsave(&hisi_hba->lock, flags);
  461. port->port_attached = 1;
  462. port->id = phy->port_id;
  463. phy->port = port;
  464. sas_port->lldd_port = port;
  465. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  466. }
  467. static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no,
  468. struct domain_device *device)
  469. {
  470. struct hisi_sas_phy *phy;
  471. struct hisi_sas_port *port;
  472. struct hisi_sas_slot *slot, *slot2;
  473. struct device *dev = &hisi_hba->pdev->dev;
  474. phy = &hisi_hba->phy[phy_no];
  475. port = phy->port;
  476. if (!port)
  477. return;
  478. list_for_each_entry_safe(slot, slot2, &port->list, entry) {
  479. struct sas_task *task;
  480. task = slot->task;
  481. if (device && task->dev != device)
  482. continue;
  483. dev_info(dev, "Release slot [%d:%d], task [%p]:\n",
  484. slot->dlvry_queue, slot->dlvry_queue_slot, task);
  485. hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
  486. }
  487. }
  488. static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy)
  489. {
  490. struct domain_device *device;
  491. struct hisi_sas_phy *phy = sas_phy->lldd_phy;
  492. struct asd_sas_port *sas_port = sas_phy->port;
  493. list_for_each_entry(device, &sas_port->dev_list, dev_list_node)
  494. hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device);
  495. }
  496. static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
  497. struct domain_device *device)
  498. {
  499. struct asd_sas_port *port = device->port;
  500. struct asd_sas_phy *sas_phy;
  501. list_for_each_entry(sas_phy, &port->phy_list, port_phy_el)
  502. hisi_sas_do_release_task(hisi_hba, sas_phy->id, device);
  503. }
  504. static void hisi_sas_dev_gone(struct domain_device *device)
  505. {
  506. struct hisi_sas_device *sas_dev = device->lldd_dev;
  507. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  508. struct device *dev = &hisi_hba->pdev->dev;
  509. u64 dev_id = sas_dev->device_id;
  510. dev_info(dev, "found dev[%lld:%x] is gone\n",
  511. sas_dev->device_id, sas_dev->dev_type);
  512. hisi_sas_internal_task_abort(hisi_hba, device,
  513. HISI_SAS_INT_ABT_DEV, 0);
  514. hisi_hba->hw->free_device(hisi_hba, sas_dev);
  515. device->lldd_dev = NULL;
  516. memset(sas_dev, 0, sizeof(*sas_dev));
  517. sas_dev->device_id = dev_id;
  518. sas_dev->dev_type = SAS_PHY_UNUSED;
  519. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  520. }
  521. static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
  522. {
  523. return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
  524. }
  525. static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
  526. void *funcdata)
  527. {
  528. struct sas_ha_struct *sas_ha = sas_phy->ha;
  529. struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
  530. int phy_no = sas_phy->id;
  531. switch (func) {
  532. case PHY_FUNC_HARD_RESET:
  533. hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
  534. break;
  535. case PHY_FUNC_LINK_RESET:
  536. hisi_hba->hw->phy_enable(hisi_hba, phy_no);
  537. hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
  538. break;
  539. case PHY_FUNC_DISABLE:
  540. hisi_hba->hw->phy_disable(hisi_hba, phy_no);
  541. break;
  542. case PHY_FUNC_SET_LINK_RATE:
  543. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  544. default:
  545. return -EOPNOTSUPP;
  546. }
  547. return 0;
  548. }
  549. static void hisi_sas_task_done(struct sas_task *task)
  550. {
  551. if (!del_timer(&task->slow_task->timer))
  552. return;
  553. complete(&task->slow_task->completion);
  554. }
  555. static void hisi_sas_tmf_timedout(unsigned long data)
  556. {
  557. struct sas_task *task = (struct sas_task *)data;
  558. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  559. complete(&task->slow_task->completion);
  560. }
  561. #define TASK_TIMEOUT 20
  562. #define TASK_RETRY 3
  563. static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
  564. void *parameter, u32 para_len,
  565. struct hisi_sas_tmf_task *tmf)
  566. {
  567. struct hisi_sas_device *sas_dev = device->lldd_dev;
  568. struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
  569. struct device *dev = &hisi_hba->pdev->dev;
  570. struct sas_task *task;
  571. int res, retry;
  572. for (retry = 0; retry < TASK_RETRY; retry++) {
  573. task = sas_alloc_slow_task(GFP_KERNEL);
  574. if (!task)
  575. return -ENOMEM;
  576. task->dev = device;
  577. task->task_proto = device->tproto;
  578. memcpy(&task->ssp_task, parameter, para_len);
  579. task->task_done = hisi_sas_task_done;
  580. task->slow_task->timer.data = (unsigned long) task;
  581. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  582. task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
  583. add_timer(&task->slow_task->timer);
  584. res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
  585. if (res) {
  586. del_timer(&task->slow_task->timer);
  587. dev_err(dev, "abort tmf: executing internal task failed: %d\n",
  588. res);
  589. goto ex_err;
  590. }
  591. wait_for_completion(&task->slow_task->completion);
  592. res = TMF_RESP_FUNC_FAILED;
  593. /* Even TMF timed out, return direct. */
  594. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  595. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  596. dev_err(dev, "abort tmf: TMF task[%d] timeout\n",
  597. tmf->tag_of_task_to_be_managed);
  598. if (task->lldd_task) {
  599. struct hisi_sas_slot *slot =
  600. task->lldd_task;
  601. hisi_sas_slot_task_free(hisi_hba,
  602. task, slot);
  603. }
  604. goto ex_err;
  605. }
  606. }
  607. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  608. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  609. res = TMF_RESP_FUNC_COMPLETE;
  610. break;
  611. }
  612. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  613. task->task_status.stat == TMF_RESP_FUNC_SUCC) {
  614. res = TMF_RESP_FUNC_SUCC;
  615. break;
  616. }
  617. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  618. task->task_status.stat == SAS_DATA_UNDERRUN) {
  619. /* no error, but return the number of bytes of
  620. * underrun
  621. */
  622. dev_warn(dev, "abort tmf: task to dev %016llx "
  623. "resp: 0x%x sts 0x%x underrun\n",
  624. SAS_ADDR(device->sas_addr),
  625. task->task_status.resp,
  626. task->task_status.stat);
  627. res = task->task_status.residual;
  628. break;
  629. }
  630. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  631. task->task_status.stat == SAS_DATA_OVERRUN) {
  632. dev_warn(dev, "abort tmf: blocked task error\n");
  633. res = -EMSGSIZE;
  634. break;
  635. }
  636. dev_warn(dev, "abort tmf: task to dev "
  637. "%016llx resp: 0x%x status 0x%x\n",
  638. SAS_ADDR(device->sas_addr), task->task_status.resp,
  639. task->task_status.stat);
  640. sas_free_task(task);
  641. task = NULL;
  642. }
  643. ex_err:
  644. WARN_ON(retry == TASK_RETRY);
  645. sas_free_task(task);
  646. return res;
  647. }
  648. static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
  649. u8 *lun, struct hisi_sas_tmf_task *tmf)
  650. {
  651. struct sas_ssp_task ssp_task;
  652. if (!(device->tproto & SAS_PROTOCOL_SSP))
  653. return TMF_RESP_FUNC_ESUPP;
  654. memcpy(ssp_task.LUN, lun, 8);
  655. return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
  656. sizeof(ssp_task), tmf);
  657. }
  658. static int hisi_sas_abort_task(struct sas_task *task)
  659. {
  660. struct scsi_lun lun;
  661. struct hisi_sas_tmf_task tmf_task;
  662. struct domain_device *device = task->dev;
  663. struct hisi_sas_device *sas_dev = device->lldd_dev;
  664. struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
  665. struct device *dev = &hisi_hba->pdev->dev;
  666. int rc = TMF_RESP_FUNC_FAILED;
  667. unsigned long flags;
  668. if (!sas_dev) {
  669. dev_warn(dev, "Device has been removed\n");
  670. return TMF_RESP_FUNC_FAILED;
  671. }
  672. spin_lock_irqsave(&task->task_state_lock, flags);
  673. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  674. spin_unlock_irqrestore(&task->task_state_lock, flags);
  675. rc = TMF_RESP_FUNC_COMPLETE;
  676. goto out;
  677. }
  678. spin_unlock_irqrestore(&task->task_state_lock, flags);
  679. sas_dev->dev_status = HISI_SAS_DEV_EH;
  680. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  681. struct scsi_cmnd *cmnd = task->uldd_task;
  682. struct hisi_sas_slot *slot = task->lldd_task;
  683. u32 tag = slot->idx;
  684. int_to_scsilun(cmnd->device->lun, &lun);
  685. tmf_task.tmf = TMF_ABORT_TASK;
  686. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  687. rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
  688. &tmf_task);
  689. /* if successful, clear the task and callback forwards.*/
  690. if (rc == TMF_RESP_FUNC_COMPLETE) {
  691. if (task->lldd_task) {
  692. struct hisi_sas_slot *slot;
  693. slot = &hisi_hba->slot_info
  694. [tmf_task.tag_of_task_to_be_managed];
  695. spin_lock_irqsave(&hisi_hba->lock, flags);
  696. hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
  697. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  698. }
  699. }
  700. hisi_sas_internal_task_abort(hisi_hba, device,
  701. HISI_SAS_INT_ABT_CMD, tag);
  702. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  703. task->task_proto & SAS_PROTOCOL_STP) {
  704. if (task->dev->dev_type == SAS_SATA_DEV) {
  705. hisi_sas_internal_task_abort(hisi_hba, device,
  706. HISI_SAS_INT_ABT_DEV, 0);
  707. rc = TMF_RESP_FUNC_COMPLETE;
  708. }
  709. } else if (task->task_proto & SAS_PROTOCOL_SMP) {
  710. /* SMP */
  711. struct hisi_sas_slot *slot = task->lldd_task;
  712. u32 tag = slot->idx;
  713. hisi_sas_internal_task_abort(hisi_hba, device,
  714. HISI_SAS_INT_ABT_CMD, tag);
  715. }
  716. out:
  717. if (rc != TMF_RESP_FUNC_COMPLETE)
  718. dev_notice(dev, "abort task: rc=%d\n", rc);
  719. return rc;
  720. }
  721. static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
  722. {
  723. struct hisi_sas_tmf_task tmf_task;
  724. int rc = TMF_RESP_FUNC_FAILED;
  725. tmf_task.tmf = TMF_ABORT_TASK_SET;
  726. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  727. return rc;
  728. }
  729. static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
  730. {
  731. int rc = TMF_RESP_FUNC_FAILED;
  732. struct hisi_sas_tmf_task tmf_task;
  733. tmf_task.tmf = TMF_CLEAR_ACA;
  734. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  735. return rc;
  736. }
  737. static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
  738. {
  739. struct sas_phy *phy = sas_get_local_phy(device);
  740. int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
  741. (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
  742. rc = sas_phy_reset(phy, reset_type);
  743. sas_put_local_phy(phy);
  744. msleep(2000);
  745. return rc;
  746. }
  747. static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
  748. {
  749. struct hisi_sas_device *sas_dev = device->lldd_dev;
  750. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  751. unsigned long flags;
  752. int rc = TMF_RESP_FUNC_FAILED;
  753. if (sas_dev->dev_status != HISI_SAS_DEV_EH)
  754. return TMF_RESP_FUNC_FAILED;
  755. sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
  756. rc = hisi_sas_debug_I_T_nexus_reset(device);
  757. spin_lock_irqsave(&hisi_hba->lock, flags);
  758. hisi_sas_release_task(hisi_hba, device);
  759. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  760. return 0;
  761. }
  762. static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
  763. {
  764. struct hisi_sas_tmf_task tmf_task;
  765. struct hisi_sas_device *sas_dev = device->lldd_dev;
  766. struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
  767. struct device *dev = &hisi_hba->pdev->dev;
  768. unsigned long flags;
  769. int rc = TMF_RESP_FUNC_FAILED;
  770. tmf_task.tmf = TMF_LU_RESET;
  771. sas_dev->dev_status = HISI_SAS_DEV_EH;
  772. rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
  773. if (rc == TMF_RESP_FUNC_COMPLETE) {
  774. spin_lock_irqsave(&hisi_hba->lock, flags);
  775. hisi_sas_release_task(hisi_hba, device);
  776. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  777. }
  778. /* If failed, fall-through I_T_Nexus reset */
  779. dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
  780. sas_dev->device_id, rc);
  781. return rc;
  782. }
  783. static int hisi_sas_query_task(struct sas_task *task)
  784. {
  785. struct scsi_lun lun;
  786. struct hisi_sas_tmf_task tmf_task;
  787. int rc = TMF_RESP_FUNC_FAILED;
  788. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  789. struct scsi_cmnd *cmnd = task->uldd_task;
  790. struct domain_device *device = task->dev;
  791. struct hisi_sas_slot *slot = task->lldd_task;
  792. u32 tag = slot->idx;
  793. int_to_scsilun(cmnd->device->lun, &lun);
  794. tmf_task.tmf = TMF_QUERY_TASK;
  795. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  796. rc = hisi_sas_debug_issue_ssp_tmf(device,
  797. lun.scsi_lun,
  798. &tmf_task);
  799. switch (rc) {
  800. /* The task is still in Lun, release it then */
  801. case TMF_RESP_FUNC_SUCC:
  802. /* The task is not in Lun or failed, reset the phy */
  803. case TMF_RESP_FUNC_FAILED:
  804. case TMF_RESP_FUNC_COMPLETE:
  805. break;
  806. }
  807. }
  808. return rc;
  809. }
  810. static int
  811. hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
  812. struct sas_task *task, int abort_flag,
  813. int task_tag)
  814. {
  815. struct domain_device *device = task->dev;
  816. struct hisi_sas_device *sas_dev = device->lldd_dev;
  817. struct device *dev = &hisi_hba->pdev->dev;
  818. struct hisi_sas_port *port;
  819. struct hisi_sas_slot *slot;
  820. struct hisi_sas_cmd_hdr *cmd_hdr_base;
  821. int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
  822. if (!device->port)
  823. return -1;
  824. port = device->port->lldd_port;
  825. /* simply get a slot and send abort command */
  826. rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
  827. if (rc)
  828. goto err_out;
  829. rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue,
  830. &dlvry_queue_slot);
  831. if (rc)
  832. goto err_out_tag;
  833. slot = &hisi_hba->slot_info[slot_idx];
  834. memset(slot, 0, sizeof(struct hisi_sas_slot));
  835. slot->idx = slot_idx;
  836. slot->n_elem = n_elem;
  837. slot->dlvry_queue = dlvry_queue;
  838. slot->dlvry_queue_slot = dlvry_queue_slot;
  839. cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
  840. slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
  841. slot->task = task;
  842. slot->port = port;
  843. task->lldd_task = slot;
  844. memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
  845. rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
  846. abort_flag, task_tag);
  847. if (rc)
  848. goto err_out_tag;
  849. /* Port structure is static for the HBA, so
  850. * even if the port is deformed it is ok
  851. * to reference.
  852. */
  853. list_add_tail(&slot->entry, &port->list);
  854. spin_lock(&task->task_state_lock);
  855. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  856. spin_unlock(&task->task_state_lock);
  857. hisi_hba->slot_prep = slot;
  858. sas_dev->running_req++;
  859. /* send abort command to our chip */
  860. hisi_hba->hw->start_delivery(hisi_hba);
  861. return 0;
  862. err_out_tag:
  863. hisi_sas_slot_index_free(hisi_hba, slot_idx);
  864. err_out:
  865. dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
  866. return rc;
  867. }
  868. /**
  869. * hisi_sas_internal_task_abort -- execute an internal
  870. * abort command for single IO command or a device
  871. * @hisi_hba: host controller struct
  872. * @device: domain device
  873. * @abort_flag: mode of operation, device or single IO
  874. * @tag: tag of IO to be aborted (only relevant to single
  875. * IO mode)
  876. */
  877. static int
  878. hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
  879. struct domain_device *device,
  880. int abort_flag, int tag)
  881. {
  882. struct sas_task *task;
  883. struct hisi_sas_device *sas_dev = device->lldd_dev;
  884. struct device *dev = &hisi_hba->pdev->dev;
  885. int res;
  886. unsigned long flags;
  887. if (!hisi_hba->hw->prep_abort)
  888. return -EOPNOTSUPP;
  889. task = sas_alloc_slow_task(GFP_KERNEL);
  890. if (!task)
  891. return -ENOMEM;
  892. task->dev = device;
  893. task->task_proto = device->tproto;
  894. task->task_done = hisi_sas_task_done;
  895. task->slow_task->timer.data = (unsigned long)task;
  896. task->slow_task->timer.function = hisi_sas_tmf_timedout;
  897. task->slow_task->timer.expires = jiffies + 20*HZ;
  898. add_timer(&task->slow_task->timer);
  899. /* Lock as we are alloc'ing a slot, which cannot be interrupted */
  900. spin_lock_irqsave(&hisi_hba->lock, flags);
  901. res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
  902. task, abort_flag, tag);
  903. spin_unlock_irqrestore(&hisi_hba->lock, flags);
  904. if (res) {
  905. del_timer(&task->slow_task->timer);
  906. dev_err(dev, "internal task abort: executing internal task failed: %d\n",
  907. res);
  908. goto exit;
  909. }
  910. wait_for_completion(&task->slow_task->completion);
  911. res = TMF_RESP_FUNC_FAILED;
  912. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  913. task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
  914. res = TMF_RESP_FUNC_COMPLETE;
  915. goto exit;
  916. }
  917. /* TMF timed out, return direct. */
  918. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  919. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  920. dev_err(dev, "internal task abort: timeout.\n");
  921. if (task->lldd_task) {
  922. struct hisi_sas_slot *slot = task->lldd_task;
  923. hisi_sas_slot_task_free(hisi_hba, task, slot);
  924. }
  925. }
  926. }
  927. exit:
  928. dev_info(dev, "internal task abort: task to dev %016llx task=%p "
  929. "resp: 0x%x sts 0x%x\n",
  930. SAS_ADDR(device->sas_addr),
  931. task,
  932. task->task_status.resp, /* 0 is complete, -1 is undelivered */
  933. task->task_status.stat);
  934. sas_free_task(task);
  935. return res;
  936. }
  937. static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
  938. {
  939. hisi_sas_port_notify_formed(sas_phy);
  940. }
  941. static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
  942. {
  943. hisi_sas_port_notify_deformed(sas_phy);
  944. }
  945. static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
  946. {
  947. phy->phy_attached = 0;
  948. phy->phy_type = 0;
  949. phy->port = NULL;
  950. }
  951. void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
  952. {
  953. struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
  954. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  955. struct sas_ha_struct *sas_ha = &hisi_hba->sha;
  956. if (rdy) {
  957. /* Phy down but ready */
  958. hisi_sas_bytes_dmaed(hisi_hba, phy_no);
  959. hisi_sas_port_notify_formed(sas_phy);
  960. } else {
  961. struct hisi_sas_port *port = phy->port;
  962. /* Phy down and not ready */
  963. sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
  964. sas_phy_disconnected(sas_phy);
  965. if (port) {
  966. if (phy->phy_type & PORT_TYPE_SAS) {
  967. int port_id = port->id;
  968. if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
  969. port_id))
  970. port->port_attached = 0;
  971. } else if (phy->phy_type & PORT_TYPE_SATA)
  972. port->port_attached = 0;
  973. }
  974. hisi_sas_phy_disconnected(phy);
  975. }
  976. }
  977. EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
  978. static struct scsi_transport_template *hisi_sas_stt;
  979. static struct scsi_host_template hisi_sas_sht = {
  980. .module = THIS_MODULE,
  981. .name = DRV_NAME,
  982. .queuecommand = sas_queuecommand,
  983. .target_alloc = sas_target_alloc,
  984. .slave_configure = hisi_sas_slave_configure,
  985. .scan_finished = hisi_sas_scan_finished,
  986. .scan_start = hisi_sas_scan_start,
  987. .change_queue_depth = sas_change_queue_depth,
  988. .bios_param = sas_bios_param,
  989. .can_queue = 1,
  990. .this_id = -1,
  991. .sg_tablesize = SG_ALL,
  992. .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
  993. .use_clustering = ENABLE_CLUSTERING,
  994. .eh_device_reset_handler = sas_eh_device_reset_handler,
  995. .eh_bus_reset_handler = sas_eh_bus_reset_handler,
  996. .target_destroy = sas_target_destroy,
  997. .ioctl = sas_ioctl,
  998. };
  999. static struct sas_domain_function_template hisi_sas_transport_ops = {
  1000. .lldd_dev_found = hisi_sas_dev_found,
  1001. .lldd_dev_gone = hisi_sas_dev_gone,
  1002. .lldd_execute_task = hisi_sas_queue_command,
  1003. .lldd_control_phy = hisi_sas_control_phy,
  1004. .lldd_abort_task = hisi_sas_abort_task,
  1005. .lldd_abort_task_set = hisi_sas_abort_task_set,
  1006. .lldd_clear_aca = hisi_sas_clear_aca,
  1007. .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
  1008. .lldd_lu_reset = hisi_sas_lu_reset,
  1009. .lldd_query_task = hisi_sas_query_task,
  1010. .lldd_port_formed = hisi_sas_port_formed,
  1011. .lldd_port_deformed = hisi_sas_port_deformed,
  1012. };
  1013. static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
  1014. {
  1015. struct platform_device *pdev = hisi_hba->pdev;
  1016. struct device *dev = &pdev->dev;
  1017. int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
  1018. spin_lock_init(&hisi_hba->lock);
  1019. for (i = 0; i < hisi_hba->n_phy; i++) {
  1020. hisi_sas_phy_init(hisi_hba, i);
  1021. hisi_hba->port[i].port_attached = 0;
  1022. hisi_hba->port[i].id = -1;
  1023. INIT_LIST_HEAD(&hisi_hba->port[i].list);
  1024. }
  1025. for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
  1026. hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
  1027. hisi_hba->devices[i].device_id = i;
  1028. hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
  1029. }
  1030. for (i = 0; i < hisi_hba->queue_count; i++) {
  1031. struct hisi_sas_cq *cq = &hisi_hba->cq[i];
  1032. struct hisi_sas_dq *dq = &hisi_hba->dq[i];
  1033. /* Completion queue structure */
  1034. cq->id = i;
  1035. cq->hisi_hba = hisi_hba;
  1036. /* Delivery queue structure */
  1037. dq->id = i;
  1038. dq->hisi_hba = hisi_hba;
  1039. /* Delivery queue */
  1040. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1041. hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
  1042. &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
  1043. if (!hisi_hba->cmd_hdr[i])
  1044. goto err_out;
  1045. memset(hisi_hba->cmd_hdr[i], 0, s);
  1046. /* Completion queue */
  1047. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1048. hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
  1049. &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
  1050. if (!hisi_hba->complete_hdr[i])
  1051. goto err_out;
  1052. memset(hisi_hba->complete_hdr[i], 0, s);
  1053. }
  1054. s = HISI_SAS_STATUS_BUF_SZ;
  1055. hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
  1056. dev, s, 16, 0);
  1057. if (!hisi_hba->status_buffer_pool)
  1058. goto err_out;
  1059. s = HISI_SAS_COMMAND_TABLE_SZ;
  1060. hisi_hba->command_table_pool = dma_pool_create("command_table",
  1061. dev, s, 16, 0);
  1062. if (!hisi_hba->command_table_pool)
  1063. goto err_out;
  1064. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  1065. hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
  1066. GFP_KERNEL);
  1067. if (!hisi_hba->itct)
  1068. goto err_out;
  1069. memset(hisi_hba->itct, 0, s);
  1070. hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
  1071. sizeof(struct hisi_sas_slot),
  1072. GFP_KERNEL);
  1073. if (!hisi_hba->slot_info)
  1074. goto err_out;
  1075. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1076. hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
  1077. GFP_KERNEL);
  1078. if (!hisi_hba->iost)
  1079. goto err_out;
  1080. memset(hisi_hba->iost, 0, s);
  1081. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1082. hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
  1083. &hisi_hba->breakpoint_dma, GFP_KERNEL);
  1084. if (!hisi_hba->breakpoint)
  1085. goto err_out;
  1086. memset(hisi_hba->breakpoint, 0, s);
  1087. hisi_hba->slot_index_count = max_command_entries;
  1088. s = hisi_hba->slot_index_count / BITS_PER_BYTE;
  1089. hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
  1090. if (!hisi_hba->slot_index_tags)
  1091. goto err_out;
  1092. hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
  1093. sizeof(struct hisi_sas_sge_page), 16, 0);
  1094. if (!hisi_hba->sge_page_pool)
  1095. goto err_out;
  1096. s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
  1097. hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
  1098. &hisi_hba->initial_fis_dma, GFP_KERNEL);
  1099. if (!hisi_hba->initial_fis)
  1100. goto err_out;
  1101. memset(hisi_hba->initial_fis, 0, s);
  1102. s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
  1103. hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
  1104. &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
  1105. if (!hisi_hba->sata_breakpoint)
  1106. goto err_out;
  1107. memset(hisi_hba->sata_breakpoint, 0, s);
  1108. hisi_sas_slot_index_init(hisi_hba);
  1109. hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
  1110. if (!hisi_hba->wq) {
  1111. dev_err(dev, "sas_alloc: failed to create workqueue\n");
  1112. goto err_out;
  1113. }
  1114. return 0;
  1115. err_out:
  1116. return -ENOMEM;
  1117. }
  1118. static void hisi_sas_free(struct hisi_hba *hisi_hba)
  1119. {
  1120. struct device *dev = &hisi_hba->pdev->dev;
  1121. int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
  1122. for (i = 0; i < hisi_hba->queue_count; i++) {
  1123. s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
  1124. if (hisi_hba->cmd_hdr[i])
  1125. dma_free_coherent(dev, s,
  1126. hisi_hba->cmd_hdr[i],
  1127. hisi_hba->cmd_hdr_dma[i]);
  1128. s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
  1129. if (hisi_hba->complete_hdr[i])
  1130. dma_free_coherent(dev, s,
  1131. hisi_hba->complete_hdr[i],
  1132. hisi_hba->complete_hdr_dma[i]);
  1133. }
  1134. dma_pool_destroy(hisi_hba->status_buffer_pool);
  1135. dma_pool_destroy(hisi_hba->command_table_pool);
  1136. dma_pool_destroy(hisi_hba->sge_page_pool);
  1137. s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
  1138. if (hisi_hba->itct)
  1139. dma_free_coherent(dev, s,
  1140. hisi_hba->itct, hisi_hba->itct_dma);
  1141. s = max_command_entries * sizeof(struct hisi_sas_iost);
  1142. if (hisi_hba->iost)
  1143. dma_free_coherent(dev, s,
  1144. hisi_hba->iost, hisi_hba->iost_dma);
  1145. s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
  1146. if (hisi_hba->breakpoint)
  1147. dma_free_coherent(dev, s,
  1148. hisi_hba->breakpoint,
  1149. hisi_hba->breakpoint_dma);
  1150. s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
  1151. if (hisi_hba->initial_fis)
  1152. dma_free_coherent(dev, s,
  1153. hisi_hba->initial_fis,
  1154. hisi_hba->initial_fis_dma);
  1155. s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
  1156. if (hisi_hba->sata_breakpoint)
  1157. dma_free_coherent(dev, s,
  1158. hisi_hba->sata_breakpoint,
  1159. hisi_hba->sata_breakpoint_dma);
  1160. if (hisi_hba->wq)
  1161. destroy_workqueue(hisi_hba->wq);
  1162. }
  1163. static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
  1164. const struct hisi_sas_hw *hw)
  1165. {
  1166. struct resource *res;
  1167. struct Scsi_Host *shost;
  1168. struct hisi_hba *hisi_hba;
  1169. struct device *dev = &pdev->dev;
  1170. struct device_node *np = pdev->dev.of_node;
  1171. shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
  1172. if (!shost)
  1173. goto err_out;
  1174. hisi_hba = shost_priv(shost);
  1175. hisi_hba->hw = hw;
  1176. hisi_hba->pdev = pdev;
  1177. hisi_hba->shost = shost;
  1178. SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
  1179. init_timer(&hisi_hba->timer);
  1180. if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
  1181. SAS_ADDR_SIZE))
  1182. goto err_out;
  1183. if (np) {
  1184. hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
  1185. "hisilicon,sas-syscon");
  1186. if (IS_ERR(hisi_hba->ctrl))
  1187. goto err_out;
  1188. if (device_property_read_u32(dev, "ctrl-reset-reg",
  1189. &hisi_hba->ctrl_reset_reg))
  1190. goto err_out;
  1191. if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
  1192. &hisi_hba->ctrl_reset_sts_reg))
  1193. goto err_out;
  1194. if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
  1195. &hisi_hba->ctrl_clock_ena_reg))
  1196. goto err_out;
  1197. }
  1198. if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
  1199. goto err_out;
  1200. if (device_property_read_u32(dev, "queue-count",
  1201. &hisi_hba->queue_count))
  1202. goto err_out;
  1203. if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
  1204. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
  1205. dev_err(dev, "No usable DMA addressing method\n");
  1206. goto err_out;
  1207. }
  1208. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1209. hisi_hba->regs = devm_ioremap_resource(dev, res);
  1210. if (IS_ERR(hisi_hba->regs))
  1211. goto err_out;
  1212. if (hisi_sas_alloc(hisi_hba, shost)) {
  1213. hisi_sas_free(hisi_hba);
  1214. goto err_out;
  1215. }
  1216. return shost;
  1217. err_out:
  1218. dev_err(dev, "shost alloc failed\n");
  1219. return NULL;
  1220. }
  1221. static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
  1222. {
  1223. int i;
  1224. for (i = 0; i < hisi_hba->n_phy; i++)
  1225. memcpy(&hisi_hba->phy[i].dev_sas_addr,
  1226. hisi_hba->sas_addr,
  1227. SAS_ADDR_SIZE);
  1228. }
  1229. int hisi_sas_probe(struct platform_device *pdev,
  1230. const struct hisi_sas_hw *hw)
  1231. {
  1232. struct Scsi_Host *shost;
  1233. struct hisi_hba *hisi_hba;
  1234. struct device *dev = &pdev->dev;
  1235. struct asd_sas_phy **arr_phy;
  1236. struct asd_sas_port **arr_port;
  1237. struct sas_ha_struct *sha;
  1238. int rc, phy_nr, port_nr, i;
  1239. shost = hisi_sas_shost_alloc(pdev, hw);
  1240. if (!shost) {
  1241. rc = -ENOMEM;
  1242. goto err_out_ha;
  1243. }
  1244. sha = SHOST_TO_SAS_HA(shost);
  1245. hisi_hba = shost_priv(shost);
  1246. platform_set_drvdata(pdev, sha);
  1247. phy_nr = port_nr = hisi_hba->n_phy;
  1248. arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
  1249. arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
  1250. if (!arr_phy || !arr_port)
  1251. return -ENOMEM;
  1252. sha->sas_phy = arr_phy;
  1253. sha->sas_port = arr_port;
  1254. sha->core.shost = shost;
  1255. sha->lldd_ha = hisi_hba;
  1256. shost->transportt = hisi_sas_stt;
  1257. shost->max_id = HISI_SAS_MAX_DEVICES;
  1258. shost->max_lun = ~0;
  1259. shost->max_channel = 1;
  1260. shost->max_cmd_len = 16;
  1261. shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
  1262. shost->can_queue = hisi_hba->hw->max_command_entries;
  1263. shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
  1264. sha->sas_ha_name = DRV_NAME;
  1265. sha->dev = &hisi_hba->pdev->dev;
  1266. sha->lldd_module = THIS_MODULE;
  1267. sha->sas_addr = &hisi_hba->sas_addr[0];
  1268. sha->num_phys = hisi_hba->n_phy;
  1269. sha->core.shost = hisi_hba->shost;
  1270. for (i = 0; i < hisi_hba->n_phy; i++) {
  1271. sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
  1272. sha->sas_port[i] = &hisi_hba->port[i].sas_port;
  1273. }
  1274. hisi_sas_init_add(hisi_hba);
  1275. rc = hisi_hba->hw->hw_init(hisi_hba);
  1276. if (rc)
  1277. goto err_out_ha;
  1278. rc = scsi_add_host(shost, &pdev->dev);
  1279. if (rc)
  1280. goto err_out_ha;
  1281. rc = sas_register_ha(sha);
  1282. if (rc)
  1283. goto err_out_register_ha;
  1284. scsi_scan_host(shost);
  1285. return 0;
  1286. err_out_register_ha:
  1287. scsi_remove_host(shost);
  1288. err_out_ha:
  1289. kfree(shost);
  1290. return rc;
  1291. }
  1292. EXPORT_SYMBOL_GPL(hisi_sas_probe);
  1293. int hisi_sas_remove(struct platform_device *pdev)
  1294. {
  1295. struct sas_ha_struct *sha = platform_get_drvdata(pdev);
  1296. struct hisi_hba *hisi_hba = sha->lldd_ha;
  1297. scsi_remove_host(sha->core.shost);
  1298. sas_unregister_ha(sha);
  1299. sas_remove_host(sha->core.shost);
  1300. hisi_sas_free(hisi_hba);
  1301. return 0;
  1302. }
  1303. EXPORT_SYMBOL_GPL(hisi_sas_remove);
  1304. static __init int hisi_sas_init(void)
  1305. {
  1306. pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
  1307. hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
  1308. if (!hisi_sas_stt)
  1309. return -ENOMEM;
  1310. return 0;
  1311. }
  1312. static __exit void hisi_sas_exit(void)
  1313. {
  1314. sas_release_transport(hisi_sas_stt);
  1315. }
  1316. module_init(hisi_sas_init);
  1317. module_exit(hisi_sas_exit);
  1318. MODULE_VERSION(DRV_VERSION);
  1319. MODULE_LICENSE("GPL");
  1320. MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
  1321. MODULE_DESCRIPTION("HISILICON SAS controller driver");
  1322. MODULE_ALIAS("platform:" DRV_NAME);