fsl_sata.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /*-
  2. * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
  3. * Copyright (c) 2017 Justin Hibbits <jhibbits@FreeBSD.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer,
  11. * without modification, immediately at the beginning of the file.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <sys/cdefs.h>
  28. __FBSDID("$FreeBSD$");
  29. #include <sys/param.h>
  30. #include <sys/module.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/bus.h>
  34. #include <sys/conf.h>
  35. #include <sys/endian.h>
  36. #include <sys/lock.h>
  37. #include <sys/malloc.h>
  38. #include <sys/mutex.h>
  39. #include <sys/rman.h>
  40. #include <cam/cam.h>
  41. #include <cam/cam_ccb.h>
  42. #include <cam/cam_sim.h>
  43. #include <cam/cam_xpt_sim.h>
  44. #include <cam/cam_debug.h>
  45. #include <dev/ofw/ofw_bus_subr.h>
  46. #include <machine/bus.h>
  47. #include <machine/resource.h>
  48. #include "fsl_sata.h"
  49. struct fsl_sata_channel;
  50. struct fsl_sata_slot;
  51. enum fsl_sata_err_type;
  52. struct fsl_sata_cmd_tab;
  53. /* local prototypes */
  54. static int fsl_sata_init(device_t dev);
  55. static int fsl_sata_deinit(device_t dev);
  56. static int fsl_sata_suspend(device_t dev);
  57. static int fsl_sata_resume(device_t dev);
  58. static void fsl_sata_pm(void *arg);
  59. static void fsl_sata_intr(void *arg);
  60. static void fsl_sata_intr_main(struct fsl_sata_channel *ch, uint32_t istatus);
  61. static void fsl_sata_begin_transaction(struct fsl_sata_channel *ch, union ccb *ccb);
  62. static void fsl_sata_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
  63. static void fsl_sata_execute_transaction(struct fsl_sata_slot *slot);
  64. static void fsl_sata_timeout(void *arg);
  65. static void fsl_sata_end_transaction(struct fsl_sata_slot *slot, enum fsl_sata_err_type et);
  66. static int fsl_sata_setup_fis(struct fsl_sata_channel *ch, struct fsl_sata_cmd_tab *ctp, union ccb *ccb, int tag);
  67. static void fsl_sata_dmainit(device_t dev);
  68. static void fsl_sata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
  69. static void fsl_sata_dmafini(device_t dev);
  70. static void fsl_sata_slotsalloc(device_t dev);
  71. static void fsl_sata_slotsfree(device_t dev);
  72. static void fsl_sata_reset(struct fsl_sata_channel *ch);
  73. static void fsl_sata_start(struct fsl_sata_channel *ch);
  74. static void fsl_sata_stop(struct fsl_sata_channel *ch);
  75. static void fsl_sata_issue_recovery(struct fsl_sata_channel *ch);
  76. static void fsl_sata_process_read_log(struct fsl_sata_channel *ch, union ccb *ccb);
  77. static void fsl_sata_process_request_sense(struct fsl_sata_channel *ch, union ccb *ccb);
  78. static void fsl_sataaction(struct cam_sim *sim, union ccb *ccb);
  79. static void fsl_satapoll(struct cam_sim *sim);
  80. static MALLOC_DEFINE(M_FSL_SATA, "FSL SATA driver", "FSL SATA driver data buffers");
  81. #define recovery_type spriv_field0
  82. #define RECOVERY_NONE 0
  83. #define RECOVERY_READ_LOG 1
  84. #define RECOVERY_REQUEST_SENSE 2
  85. #define recovery_slot spriv_field1
  86. #define FSL_SATA_P_CQR 0x0
  87. #define FSL_SATA_P_CAR 0x4
  88. #define FSL_SATA_P_CCR 0x10
  89. #define FSL_SATA_P_CER 0x18
  90. #define FSL_SATA_P_DER 0x20
  91. #define FSL_SATA_P_CHBA 0x24
  92. #define FSL_SATA_P_HSTS 0x28
  93. #define FSL_SATA_P_HSTS_HS_ON 0x80000000
  94. #define FSL_SATA_P_HSTS_ME 0x00040000
  95. #define FSL_SATA_P_HSTS_DLM 0x00001000
  96. #define FSL_SATA_P_HSTS_FOT 0x00000200
  97. #define FSL_SATA_P_HSTS_FOR 0x00000100
  98. #define FSL_SATA_P_HSTS_FE 0x00000020
  99. #define FSL_SATA_P_HSTS_PR 0x00000010
  100. #define FSL_SATA_P_HSTS_SNTFU 0x00000004
  101. #define FSL_SATA_P_HSTS_DE 0x00000002
  102. #define FSL_SATA_P_HCTRL 0x2c
  103. #define FSL_SATA_P_HCTRL_HC_ON 0x80000000
  104. #define FSL_SATA_P_HCTRL_HC_FORCE_OFF 0x40000000
  105. #define FSL_SATA_P_HCTRL_ENT 0x10000000
  106. #define FSL_SATA_P_HCTRL_SNOOP 0x00000400
  107. #define FSL_SATA_P_HCTRL_PM 0x00000200
  108. #define FSL_SATA_P_HCTRL_FATAL 0x00000020
  109. #define FSL_SATA_P_HCTRL_PHYRDY 0x00000010
  110. #define FSL_SATA_P_HCTRL_SIG 0x00000008
  111. #define FSL_SATA_P_HCTRL_SNTFY 0x00000004
  112. #define FSL_SATA_P_HCTRL_DE 0x00000002
  113. #define FSL_SATA_P_HCTRL_CC 0x00000001
  114. #define FSL_SATA_P_HCTRL_INT_MASK 0x0000003f
  115. #define FSL_SATA_P_CQPMP 0x30
  116. #define FSL_SATA_P_SIG 0x34
  117. #define FSL_SATA_P_ICC 0x38
  118. #define FSL_SATA_P_ICC_ITC_M 0x1f000000
  119. #define FSL_SATA_P_ICC_ITC_S 24
  120. #define FSL_SATA_P_ICC_ITTCV_M 0x0007ffff
  121. #define FSL_SATA_P_PCC 0x15c
  122. #define FSL_SATA_P_PCC_SLUMBER 0x0000000c
  123. #define FSL_SATA_P_PCC_PARTIAL 0x0000000a
  124. #define FSL_SATA_PCC_LPB_EN 0x0000000e
  125. #define FSL_SATA_MAX_SLOTS 16
  126. /* FSL_SATA register defines */
  127. #define FSL_SATA_P_SSTS 0x100
  128. #define FSL_SATA_P_SERR 0x104
  129. #define FSL_SATA_P_SCTL 0x108
  130. #define FSL_SATA_P_SNTF 0x10c
  131. /* Pessimistic prognosis on number of required S/G entries */
  132. #define FSL_SATA_SG_ENTRIES 63
  133. /* Command list. 16 commands. First, 1Kbyte aligned. */
  134. #define FSL_SATA_CL_OFFSET 0
  135. #define FSL_SATA_CL_SIZE 16
  136. /* Command tables. Up to 32 commands, Each, 4-byte aligned. */
  137. #define FSL_SATA_CT_OFFSET (FSL_SATA_CL_OFFSET + FSL_SATA_CL_SIZE * FSL_SATA_MAX_SLOTS)
  138. #define FSL_SATA_CT_SIZE (96 + FSL_SATA_SG_ENTRIES * 16)
  139. /* Total main work area. */
  140. #define FSL_SATA_WORK_SIZE (FSL_SATA_CT_OFFSET + FSL_SATA_CT_SIZE * FSL_SATA_MAX_SLOTS)
  141. #define FSL_SATA_MAX_XFER (64 * 1024 * 1024)
  142. /* Some convenience macros for getting the CTP and CLP */
  143. #define FSL_SATA_CTP_BUS(ch, slot) \
  144. ((ch->dma.work_bus + FSL_SATA_CT_OFFSET + (FSL_SATA_CT_SIZE * slot->slot)))
  145. #define FSL_SATA_PRD_OFFSET(prd) (96 + (prd) * 16)
  146. #define FSL_SATA_CTP(ch, slot) \
  147. ((struct fsl_sata_cmd_tab *)(ch->dma.work + FSL_SATA_CT_OFFSET + \
  148. (FSL_SATA_CT_SIZE * slot->slot)))
  149. #define FSL_SATA_CLP(ch, slot) \
  150. ((struct fsl_sata_cmd_list *) (ch->dma.work + FSL_SATA_CL_OFFSET + \
  151. (FSL_SATA_CL_SIZE * slot->slot)))
  152. struct fsl_sata_dma_prd {
  153. uint32_t dba;
  154. uint32_t reserved;
  155. uint32_t reserved2;
  156. uint32_t dwc_flg; /* 0 based */
  157. #define FSL_SATA_PRD_MASK 0x01fffffc /* max 32MB */
  158. #define FSL_SATA_PRD_MAX (FSL_SATA_PRD_MASK + 4)
  159. #define FSL_SATA_PRD_SNOOP 0x10000000
  160. #define FSL_SATA_PRD_EXT 0x80000000
  161. } __packed;
  162. struct fsl_sata_cmd_tab {
  163. uint8_t cfis[32];
  164. uint8_t sfis[32];
  165. uint8_t acmd[16];
  166. uint8_t reserved[16];
  167. struct fsl_sata_dma_prd prd_tab[FSL_SATA_SG_ENTRIES];
  168. #define FSL_SATA_PRD_EXT_INDEX 15
  169. #define FSL_SATA_PRD_MAX_DIRECT 16
  170. } __packed;
  171. struct fsl_sata_cmd_list {
  172. uint32_t cda; /* word aligned */
  173. uint16_t fis_length; /* length in bytes (aligned to words) */
  174. uint16_t prd_length; /* PRD entries */
  175. uint32_t ttl;
  176. uint32_t cmd_flags;
  177. #define FSL_SATA_CMD_TAG_MASK 0x001f
  178. #define FSL_SATA_CMD_ATAPI 0x0020
  179. #define FSL_SATA_CMD_BIST 0x0040
  180. #define FSL_SATA_CMD_RESET 0x0080
  181. #define FSL_SATA_CMD_QUEUED 0x0100
  182. #define FSL_SATA_CMD_SNOOP 0x0200
  183. #define FSL_SATA_CMD_VBIST 0x0400
  184. #define FSL_SATA_CMD_WRITE 0x0800
  185. } __packed;
  186. /* misc defines */
  187. #define ATA_IRQ_RID 0
  188. #define ATA_INTR_FLAGS (INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY)
  189. struct ata_dmaslot {
  190. bus_dmamap_t data_map; /* data DMA map */
  191. int nsegs; /* Number of segs loaded */
  192. };
  193. /* structure holding DMA related information */
  194. struct ata_dma {
  195. bus_dma_tag_t work_tag; /* workspace DMA tag */
  196. bus_dmamap_t work_map; /* workspace DMA map */
  197. uint8_t *work; /* workspace */
  198. bus_addr_t work_bus; /* bus address of work */
  199. bus_dma_tag_t data_tag; /* data DMA tag */
  200. };
  201. enum fsl_sata_slot_states {
  202. FSL_SATA_SLOT_EMPTY,
  203. FSL_SATA_SLOT_LOADING,
  204. FSL_SATA_SLOT_RUNNING,
  205. FSL_SATA_SLOT_EXECUTING
  206. };
  207. struct fsl_sata_slot {
  208. struct fsl_sata_channel *ch; /* Channel */
  209. uint8_t slot; /* Number of this slot */
  210. enum fsl_sata_slot_states state; /* Slot state */
  211. union ccb *ccb; /* CCB occupying slot */
  212. struct ata_dmaslot dma; /* DMA data of this slot */
  213. struct callout timeout; /* Execution timeout */
  214. uint32_t ttl;
  215. };
  216. struct fsl_sata_device {
  217. int revision;
  218. int mode;
  219. u_int bytecount;
  220. u_int atapi;
  221. u_int tags;
  222. u_int caps;
  223. };
  224. /* structure describing an ATA channel */
  225. struct fsl_sata_channel {
  226. device_t dev; /* Device handle */
  227. int r_mid; /* Physical channel RID */
  228. struct resource *r_mem; /* Memory of this channel */
  229. struct resource *r_irq; /* Interrupt of this channel */
  230. void *ih; /* Interrupt handle */
  231. struct ata_dma dma; /* DMA data */
  232. struct cam_sim *sim;
  233. struct cam_path *path;
  234. uint32_t caps; /* Controller capabilities */
  235. int pm_level; /* power management level */
  236. int devices; /* What is present */
  237. int pm_present; /* PM presence reported */
  238. union ccb *hold[FSL_SATA_MAX_SLOTS];
  239. struct fsl_sata_slot slot[FSL_SATA_MAX_SLOTS];
  240. uint32_t oslots; /* Occupied slots */
  241. uint32_t rslots; /* Running slots */
  242. uint32_t aslots; /* Slots with atomic commands */
  243. uint32_t eslots; /* Slots in error */
  244. uint32_t toslots; /* Slots in timeout */
  245. int lastslot; /* Last used slot */
  246. int taggedtarget; /* Last tagged target */
  247. int numrslots; /* Number of running slots */
  248. int numrslotspd[16];/* Number of running slots per dev */
  249. int numtslots; /* Number of tagged slots */
  250. int numtslotspd[16];/* Number of tagged slots per dev */
  251. int numhslots; /* Number of held slots */
  252. int recoverycmd; /* Our READ LOG active */
  253. int fatalerr; /* Fatal error happend */
  254. int resetting; /* Hard-reset in progress. */
  255. int resetpolldiv; /* Hard-reset poll divider. */
  256. union ccb *frozen; /* Frozen command */
  257. struct callout pm_timer; /* Power management events */
  258. struct callout reset_timer; /* Hard-reset timeout */
  259. struct fsl_sata_device user[16]; /* User-specified settings */
  260. struct fsl_sata_device curr[16]; /* Current settings */
  261. struct mtx_padalign mtx; /* state lock */
  262. STAILQ_HEAD(, ccb_hdr) doneq; /* queue of completed CCBs */
  263. int batch; /* doneq is in use */
  264. };
  265. enum fsl_sata_err_type {
  266. FSL_SATA_ERR_NONE, /* No error */
  267. FSL_SATA_ERR_INVALID, /* Error detected by us before submitting. */
  268. FSL_SATA_ERR_INNOCENT, /* Innocent victim. */
  269. FSL_SATA_ERR_TFE, /* Task File Error. */
  270. FSL_SATA_ERR_SATA, /* SATA error. */
  271. FSL_SATA_ERR_TIMEOUT, /* Command execution timeout. */
  272. FSL_SATA_ERR_NCQ, /* NCQ command error. CCB should be put on hold
  273. * until READ LOG executed to reveal error. */
  274. };
  275. /* macros to hide busspace uglyness */
  276. #define ATA_INL(res, offset) \
  277. bus_read_4((res), (offset))
  278. #define ATA_OUTL(res, offset, value) \
  279. bus_write_4((res), (offset), (value))
  280. static int
  281. fsl_sata_probe(device_t dev)
  282. {
  283. if (!ofw_bus_is_compatible(dev, "fsl,pq-sata-v2") &&
  284. !ofw_bus_is_compatible(dev, "fsl,pq-sata"))
  285. return (ENXIO);
  286. device_set_desc_copy(dev, "Freescale Integrated SATA Controller");
  287. return (BUS_PROBE_DEFAULT);
  288. }
  289. static int
  290. fsl_sata_attach(device_t dev)
  291. {
  292. struct fsl_sata_channel *ch = device_get_softc(dev);
  293. struct cam_devq *devq;
  294. int rid, error, i, sata_rev = 0;
  295. ch->dev = dev;
  296. mtx_init(&ch->mtx, "FSL SATA channel lock", NULL, MTX_DEF);
  297. ch->pm_level = 0;
  298. resource_int_value(device_get_name(dev),
  299. device_get_unit(dev), "pm_level", &ch->pm_level);
  300. STAILQ_INIT(&ch->doneq);
  301. if (ch->pm_level > 3)
  302. callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
  303. resource_int_value(device_get_name(dev),
  304. device_get_unit(dev), "sata_rev", &sata_rev);
  305. for (i = 0; i < 16; i++) {
  306. ch->user[i].revision = sata_rev;
  307. ch->user[i].mode = 0;
  308. ch->user[i].bytecount = 8192;
  309. ch->user[i].tags = FSL_SATA_MAX_SLOTS;
  310. ch->user[i].caps = 0;
  311. ch->curr[i] = ch->user[i];
  312. if (ch->pm_level) {
  313. ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ |
  314. CTS_SATA_CAPS_D_PMREQ;
  315. }
  316. ch->user[i].caps |= CTS_SATA_CAPS_H_AN;
  317. }
  318. ch->r_mid = 0;
  319. if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  320. &ch->r_mid, RF_ACTIVE)))
  321. return (ENXIO);
  322. rman_set_bustag(ch->r_mem, &bs_le_tag);
  323. fsl_sata_dmainit(dev);
  324. fsl_sata_slotsalloc(dev);
  325. fsl_sata_init(dev);
  326. rid = ATA_IRQ_RID;
  327. if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
  328. &rid, RF_SHAREABLE | RF_ACTIVE))) {
  329. device_printf(dev, "Unable to map interrupt\n");
  330. error = ENXIO;
  331. goto err0;
  332. }
  333. if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
  334. fsl_sata_intr, ch, &ch->ih))) {
  335. device_printf(dev, "Unable to setup interrupt\n");
  336. error = ENXIO;
  337. goto err1;
  338. }
  339. mtx_lock(&ch->mtx);
  340. /* Create the device queue for our SIM. */
  341. devq = cam_simq_alloc(FSL_SATA_MAX_SLOTS);
  342. if (devq == NULL) {
  343. device_printf(dev, "Unable to allocate simq\n");
  344. error = ENOMEM;
  345. goto err1;
  346. }
  347. /* Construct SIM entry */
  348. ch->sim = cam_sim_alloc(fsl_sataaction, fsl_satapoll, "fslsata", ch,
  349. device_get_unit(dev), (struct mtx *)&ch->mtx, 2, FSL_SATA_MAX_SLOTS,
  350. devq);
  351. if (ch->sim == NULL) {
  352. cam_simq_free(devq);
  353. device_printf(dev, "unable to allocate sim\n");
  354. error = ENOMEM;
  355. goto err1;
  356. }
  357. if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
  358. device_printf(dev, "unable to register xpt bus\n");
  359. error = ENXIO;
  360. goto err2;
  361. }
  362. if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
  363. CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  364. device_printf(dev, "unable to create path\n");
  365. error = ENXIO;
  366. goto err3;
  367. }
  368. if (ch->pm_level > 3) {
  369. callout_reset(&ch->pm_timer,
  370. (ch->pm_level == 4) ? hz / 1000 : hz / 8,
  371. fsl_sata_pm, ch);
  372. }
  373. mtx_unlock(&ch->mtx);
  374. return (0);
  375. err3:
  376. xpt_bus_deregister(cam_sim_path(ch->sim));
  377. err2:
  378. cam_sim_free(ch->sim, /*free_devq*/TRUE);
  379. err1:
  380. mtx_unlock(&ch->mtx);
  381. bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
  382. err0:
  383. bus_release_resource(dev, SYS_RES_MEMORY, ch->r_mid, ch->r_mem);
  384. mtx_destroy(&ch->mtx);
  385. return (error);
  386. }
  387. static int
  388. fsl_sata_detach(device_t dev)
  389. {
  390. struct fsl_sata_channel *ch = device_get_softc(dev);
  391. mtx_lock(&ch->mtx);
  392. xpt_async(AC_LOST_DEVICE, ch->path, NULL);
  393. xpt_free_path(ch->path);
  394. xpt_bus_deregister(cam_sim_path(ch->sim));
  395. cam_sim_free(ch->sim, /*free_devq*/TRUE);
  396. mtx_unlock(&ch->mtx);
  397. if (ch->pm_level > 3)
  398. callout_drain(&ch->pm_timer);
  399. bus_teardown_intr(dev, ch->r_irq, ch->ih);
  400. bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
  401. fsl_sata_deinit(dev);
  402. fsl_sata_slotsfree(dev);
  403. fsl_sata_dmafini(dev);
  404. bus_release_resource(dev, SYS_RES_MEMORY, ch->r_mid, ch->r_mem);
  405. mtx_destroy(&ch->mtx);
  406. return (0);
  407. }
  408. static int
  409. fsl_sata_wait_register(struct fsl_sata_channel *ch, bus_size_t off,
  410. unsigned int mask, unsigned int val, int t)
  411. {
  412. int timeout = 0;
  413. uint32_t rval;
  414. while (((rval = ATA_INL(ch->r_mem, off)) & mask) != val) {
  415. if (timeout > t) {
  416. return (EBUSY);
  417. }
  418. DELAY(1000);
  419. timeout++;
  420. }
  421. return (0);
  422. }
  423. static int
  424. fsl_sata_init(device_t dev)
  425. {
  426. struct fsl_sata_channel *ch = device_get_softc(dev);
  427. uint64_t work;
  428. uint32_t r;
  429. /* Disable port interrupts */
  430. r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
  431. r &= ~FSL_SATA_P_HCTRL_HC_ON;
  432. r |= FSL_SATA_P_HCTRL_HC_FORCE_OFF;
  433. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_INT_MASK);
  434. fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
  435. FSL_SATA_P_HSTS_HS_ON, 0, 1000);
  436. /* Setup work areas */
  437. work = ch->dma.work_bus + FSL_SATA_CL_OFFSET;
  438. ATA_OUTL(ch->r_mem, FSL_SATA_P_CHBA, work);
  439. r &= ~FSL_SATA_P_HCTRL_ENT;
  440. r &= ~FSL_SATA_P_HCTRL_PM;
  441. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r);
  442. r = ATA_INL(ch->r_mem, FSL_SATA_P_PCC);
  443. ATA_OUTL(ch->r_mem, FSL_SATA_P_PCC, r & ~FSL_SATA_PCC_LPB_EN);
  444. ATA_OUTL(ch->r_mem, FSL_SATA_P_ICC, (1 << FSL_SATA_P_ICC_ITC_S));
  445. fsl_sata_start(ch);
  446. return (0);
  447. }
  448. static int
  449. fsl_sata_deinit(device_t dev)
  450. {
  451. struct fsl_sata_channel *ch = device_get_softc(dev);
  452. uint32_t r;
  453. /* Disable port interrupts. */
  454. r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
  455. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_INT_MASK);
  456. /* Reset command register. */
  457. fsl_sata_stop(ch);
  458. /* Allow everything, including partial and slumber modes. */
  459. ATA_OUTL(ch->r_mem, FSL_SATA_P_SCTL, 0);
  460. DELAY(100);
  461. /* Disable PHY. */
  462. ATA_OUTL(ch->r_mem, FSL_SATA_P_SCTL, ATA_SC_DET_DISABLE);
  463. r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
  464. /* Turn off the controller. */
  465. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_HC_ON);
  466. return (0);
  467. }
  468. static int
  469. fsl_sata_suspend(device_t dev)
  470. {
  471. struct fsl_sata_channel *ch = device_get_softc(dev);
  472. mtx_lock(&ch->mtx);
  473. xpt_freeze_simq(ch->sim, 1);
  474. while (ch->oslots)
  475. msleep(ch, &ch->mtx, PRIBIO, "fsl_satasusp", hz/100);
  476. fsl_sata_deinit(dev);
  477. mtx_unlock(&ch->mtx);
  478. return (0);
  479. }
  480. static int
  481. fsl_sata_resume(device_t dev)
  482. {
  483. struct fsl_sata_channel *ch = device_get_softc(dev);
  484. mtx_lock(&ch->mtx);
  485. fsl_sata_init(dev);
  486. fsl_sata_reset(ch);
  487. xpt_release_simq(ch->sim, TRUE);
  488. mtx_unlock(&ch->mtx);
  489. return (0);
  490. }
  491. devclass_t fsl_satach_devclass;
  492. static device_method_t fsl_satach_methods[] = {
  493. DEVMETHOD(device_probe, fsl_sata_probe),
  494. DEVMETHOD(device_attach, fsl_sata_attach),
  495. DEVMETHOD(device_detach, fsl_sata_detach),
  496. DEVMETHOD(device_suspend, fsl_sata_suspend),
  497. DEVMETHOD(device_resume, fsl_sata_resume),
  498. DEVMETHOD_END
  499. };
  500. static driver_t fsl_satach_driver = {
  501. "fslsata",
  502. fsl_satach_methods,
  503. sizeof(struct fsl_sata_channel)
  504. };
  505. DRIVER_MODULE(fsl_satach, simplebus, fsl_satach_driver, fsl_satach_devclass, NULL, NULL);
  506. struct fsl_sata_dc_cb_args {
  507. bus_addr_t maddr;
  508. int error;
  509. };
  510. static void
  511. fsl_sata_dmainit(device_t dev)
  512. {
  513. struct fsl_sata_channel *ch = device_get_softc(dev);
  514. struct fsl_sata_dc_cb_args dcba;
  515. /* Command area. */
  516. if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
  517. BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
  518. NULL, NULL, FSL_SATA_WORK_SIZE, 1, FSL_SATA_WORK_SIZE,
  519. 0, NULL, NULL, &ch->dma.work_tag))
  520. goto error;
  521. if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
  522. BUS_DMA_ZERO, &ch->dma.work_map))
  523. goto error;
  524. if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
  525. FSL_SATA_WORK_SIZE, fsl_sata_dmasetupc_cb, &dcba, 0) || dcba.error) {
  526. bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
  527. goto error;
  528. }
  529. ch->dma.work_bus = dcba.maddr;
  530. /* Data area. */
  531. if (bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
  532. BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
  533. NULL, NULL, FSL_SATA_MAX_XFER,
  534. FSL_SATA_SG_ENTRIES - 1, FSL_SATA_PRD_MAX,
  535. 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
  536. goto error;
  537. }
  538. if (bootverbose)
  539. device_printf(dev, "work area: %p\n", ch->dma.work);
  540. return;
  541. error:
  542. device_printf(dev, "WARNING - DMA initialization failed\n");
  543. fsl_sata_dmafini(dev);
  544. }
  545. static void
  546. fsl_sata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
  547. {
  548. struct fsl_sata_dc_cb_args *dcba = (struct fsl_sata_dc_cb_args *)xsc;
  549. if (!(dcba->error = error))
  550. dcba->maddr = segs[0].ds_addr;
  551. }
  552. static void
  553. fsl_sata_dmafini(device_t dev)
  554. {
  555. struct fsl_sata_channel *ch = device_get_softc(dev);
  556. if (ch->dma.data_tag) {
  557. bus_dma_tag_destroy(ch->dma.data_tag);
  558. ch->dma.data_tag = NULL;
  559. }
  560. if (ch->dma.work_bus) {
  561. bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
  562. bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
  563. ch->dma.work_bus = 0;
  564. ch->dma.work = NULL;
  565. }
  566. if (ch->dma.work_tag) {
  567. bus_dma_tag_destroy(ch->dma.work_tag);
  568. ch->dma.work_tag = NULL;
  569. }
  570. }
  571. static void
  572. fsl_sata_slotsalloc(device_t dev)
  573. {
  574. struct fsl_sata_channel *ch = device_get_softc(dev);
  575. int i;
  576. /* Alloc and setup command/dma slots */
  577. bzero(ch->slot, sizeof(ch->slot));
  578. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  579. struct fsl_sata_slot *slot = &ch->slot[i];
  580. slot->ch = ch;
  581. slot->slot = i;
  582. slot->state = FSL_SATA_SLOT_EMPTY;
  583. slot->ccb = NULL;
  584. callout_init_mtx(&slot->timeout, &ch->mtx, 0);
  585. if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
  586. device_printf(ch->dev, "FAILURE - create data_map\n");
  587. }
  588. }
  589. static void
  590. fsl_sata_slotsfree(device_t dev)
  591. {
  592. struct fsl_sata_channel *ch = device_get_softc(dev);
  593. int i;
  594. /* Free all dma slots */
  595. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  596. struct fsl_sata_slot *slot = &ch->slot[i];
  597. callout_drain(&slot->timeout);
  598. if (slot->dma.data_map) {
  599. bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
  600. slot->dma.data_map = NULL;
  601. }
  602. }
  603. }
  604. static int
  605. fsl_sata_phy_check_events(struct fsl_sata_channel *ch, u_int32_t serr)
  606. {
  607. if (((ch->pm_level == 0) && (serr & ATA_SE_PHY_CHANGED)) ||
  608. ((ch->pm_level != 0) && (serr & ATA_SE_EXCHANGED))) {
  609. u_int32_t status = ATA_INL(ch->r_mem, FSL_SATA_P_SSTS);
  610. union ccb *ccb;
  611. if (bootverbose) {
  612. if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE)
  613. device_printf(ch->dev, "CONNECT requested\n");
  614. else
  615. device_printf(ch->dev, "DISCONNECT requested\n");
  616. }
  617. /* Issue soft reset */
  618. xpt_async(AC_BUS_RESET, ch->path, NULL);
  619. if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
  620. return (0);
  621. if (xpt_create_path(&ccb->ccb_h.path, NULL,
  622. cam_sim_path(ch->sim),
  623. CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
  624. xpt_free_ccb(ccb);
  625. return (0);
  626. }
  627. xpt_rescan(ccb);
  628. return (1);
  629. }
  630. return (0);
  631. }
  632. static void
  633. fsl_sata_notify_events(struct fsl_sata_channel *ch, u_int32_t status)
  634. {
  635. struct cam_path *dpath;
  636. int i;
  637. ATA_OUTL(ch->r_mem, FSL_SATA_P_SNTF, status);
  638. if (bootverbose)
  639. device_printf(ch->dev, "SNTF 0x%04x\n", status);
  640. for (i = 0; i < 16; i++) {
  641. if ((status & (1 << i)) == 0)
  642. continue;
  643. if (xpt_create_path(&dpath, NULL,
  644. xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
  645. xpt_async(AC_SCSI_AEN, dpath, NULL);
  646. xpt_free_path(dpath);
  647. }
  648. }
  649. }
  650. static void
  651. fsl_sata_done(struct fsl_sata_channel *ch, union ccb *ccb)
  652. {
  653. mtx_assert(&ch->mtx, MA_OWNED);
  654. if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0 ||
  655. ch->batch == 0) {
  656. xpt_done(ccb);
  657. return;
  658. }
  659. STAILQ_INSERT_TAIL(&ch->doneq, &ccb->ccb_h, sim_links.stqe);
  660. }
  661. static void
  662. fsl_sata_intr(void *arg)
  663. {
  664. struct fsl_sata_channel *ch = (struct fsl_sata_channel *)arg;
  665. struct ccb_hdr *ccb_h;
  666. uint32_t istatus;
  667. STAILQ_HEAD(, ccb_hdr) tmp_doneq = STAILQ_HEAD_INITIALIZER(tmp_doneq);
  668. /* Read interrupt statuses. */
  669. istatus = ATA_INL(ch->r_mem, FSL_SATA_P_HSTS) & 0x7ffff;
  670. if ((istatus & 0x3f) == 0)
  671. return;
  672. mtx_lock(&ch->mtx);
  673. ch->batch = 1;
  674. fsl_sata_intr_main(ch, istatus);
  675. ch->batch = 0;
  676. /*
  677. * Prevent the possibility of issues caused by processing the queue
  678. * while unlocked below by moving the contents to a local queue.
  679. */
  680. STAILQ_CONCAT(&tmp_doneq, &ch->doneq);
  681. mtx_unlock(&ch->mtx);
  682. while ((ccb_h = STAILQ_FIRST(&tmp_doneq)) != NULL) {
  683. STAILQ_REMOVE_HEAD(&tmp_doneq, sim_links.stqe);
  684. xpt_done_direct((union ccb *)ccb_h);
  685. }
  686. /* Clear interrupt statuses. */
  687. ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS, istatus & 0x3f);
  688. }
  689. static void
  690. fsl_sata_pm(void *arg)
  691. {
  692. struct fsl_sata_channel *ch = (struct fsl_sata_channel *)arg;
  693. uint32_t work;
  694. if (ch->numrslots != 0)
  695. return;
  696. work = ATA_INL(ch->r_mem, FSL_SATA_P_PCC) & ~FSL_SATA_PCC_LPB_EN;
  697. if (ch->pm_level == 4)
  698. work |= FSL_SATA_P_PCC_PARTIAL;
  699. else
  700. work |= FSL_SATA_P_PCC_SLUMBER;
  701. ATA_OUTL(ch->r_mem, FSL_SATA_P_PCC, work);
  702. }
  703. /* XXX: interrupt todo */
  704. static void
  705. fsl_sata_intr_main(struct fsl_sata_channel *ch, uint32_t istatus)
  706. {
  707. uint32_t cer, der, serr = 0, sntf = 0, ok, err;
  708. enum fsl_sata_err_type et;
  709. int i;
  710. /* Complete all successful commands. */
  711. ok = ATA_INL(ch->r_mem, FSL_SATA_P_CCR);
  712. /* Mark all commands complete, to complete the interrupt. */
  713. ATA_OUTL(ch->r_mem, FSL_SATA_P_CCR, ok);
  714. if (ch->aslots == 0 && ok != 0) {
  715. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  716. if (((ok >> i) & 1) && ch->slot[i].ccb != NULL)
  717. fsl_sata_end_transaction(&ch->slot[i],
  718. FSL_SATA_ERR_NONE);
  719. }
  720. }
  721. /* Read command statuses. */
  722. if (istatus & FSL_SATA_P_HSTS_SNTFU)
  723. sntf = ATA_INL(ch->r_mem, FSL_SATA_P_SNTF);
  724. /* XXX: Process PHY events */
  725. serr = ATA_INL(ch->r_mem, FSL_SATA_P_SERR);
  726. ATA_OUTL(ch->r_mem, FSL_SATA_P_SERR, serr);
  727. if (istatus & (FSL_SATA_P_HSTS_PR)) {
  728. if (serr) {
  729. fsl_sata_phy_check_events(ch, serr);
  730. }
  731. }
  732. /* Process command errors */
  733. err = (istatus & (FSL_SATA_P_HSTS_FE | FSL_SATA_P_HSTS_DE));
  734. cer = ATA_INL(ch->r_mem, FSL_SATA_P_CER);
  735. ATA_OUTL(ch->r_mem, FSL_SATA_P_CER, cer);
  736. der = ATA_INL(ch->r_mem, FSL_SATA_P_DER);
  737. ATA_OUTL(ch->r_mem, FSL_SATA_P_DER, der);
  738. /* On error, complete the rest of commands with error statuses. */
  739. if (err) {
  740. if (ch->frozen) {
  741. union ccb *fccb = ch->frozen;
  742. ch->frozen = NULL;
  743. fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
  744. if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
  745. xpt_freeze_devq(fccb->ccb_h.path, 1);
  746. fccb->ccb_h.status |= CAM_DEV_QFRZN;
  747. }
  748. fsl_sata_done(ch, fccb);
  749. }
  750. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  751. if (ch->slot[i].ccb == NULL)
  752. continue;
  753. if ((cer & (1 << i)) != 0)
  754. et = FSL_SATA_ERR_TFE;
  755. else if ((der & (1 << ch->slot[i].ccb->ccb_h.target_id)) != 0)
  756. et = FSL_SATA_ERR_SATA;
  757. else
  758. et = FSL_SATA_ERR_INVALID;
  759. fsl_sata_end_transaction(&ch->slot[i], et);
  760. }
  761. }
  762. /* Process NOTIFY events */
  763. if (sntf)
  764. fsl_sata_notify_events(ch, sntf);
  765. }
  766. /* Must be called with channel locked. */
  767. static int
  768. fsl_sata_check_collision(struct fsl_sata_channel *ch, union ccb *ccb)
  769. {
  770. int t = ccb->ccb_h.target_id;
  771. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  772. (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
  773. /* Tagged command while we have no supported tag free. */
  774. if (((~ch->oslots) & (0xffff >> (16 - ch->curr[t].tags))) == 0)
  775. return (1);
  776. /* Tagged command while untagged are active. */
  777. if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] == 0)
  778. return (1);
  779. } else {
  780. /* Untagged command while tagged are active. */
  781. if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] != 0)
  782. return (1);
  783. }
  784. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  785. (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
  786. /* Atomic command while anything active. */
  787. if (ch->numrslots != 0)
  788. return (1);
  789. }
  790. /* We have some atomic command running. */
  791. if (ch->aslots != 0)
  792. return (1);
  793. return (0);
  794. }
  795. /* Must be called with channel locked. */
  796. static void
  797. fsl_sata_begin_transaction(struct fsl_sata_channel *ch, union ccb *ccb)
  798. {
  799. struct fsl_sata_slot *slot;
  800. int tag, tags;
  801. CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
  802. ("fsl_sata_begin_transaction func_code=0x%x\n", ccb->ccb_h.func_code));
  803. /* Choose empty slot. */
  804. tags = FSL_SATA_MAX_SLOTS;
  805. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  806. (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
  807. tags = ch->curr[ccb->ccb_h.target_id].tags;
  808. if (ch->lastslot + 1 < tags)
  809. tag = ffs(~(ch->oslots >> (ch->lastslot + 1)));
  810. else
  811. tag = 0;
  812. if (tag == 0 || tag + ch->lastslot >= tags)
  813. tag = ffs(~ch->oslots) - 1;
  814. else
  815. tag += ch->lastslot;
  816. ch->lastslot = tag;
  817. /* Occupy chosen slot. */
  818. slot = &ch->slot[tag];
  819. slot->ccb = ccb;
  820. slot->ttl = 0;
  821. /* Stop PM timer. */
  822. if (ch->numrslots == 0 && ch->pm_level > 3)
  823. callout_stop(&ch->pm_timer);
  824. /* Update channel stats. */
  825. ch->oslots |= (1 << tag);
  826. ch->numrslots++;
  827. ch->numrslotspd[ccb->ccb_h.target_id]++;
  828. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  829. (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
  830. ch->numtslots++;
  831. ch->numtslotspd[ccb->ccb_h.target_id]++;
  832. ch->taggedtarget = ccb->ccb_h.target_id;
  833. }
  834. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  835. (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
  836. ch->aslots |= (1 << tag);
  837. if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  838. slot->state = FSL_SATA_SLOT_LOADING;
  839. bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, ccb,
  840. fsl_sata_dmasetprd, slot, 0);
  841. } else {
  842. slot->dma.nsegs = 0;
  843. fsl_sata_execute_transaction(slot);
  844. }
  845. CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
  846. ("fsl_sata_begin_transaction exit\n"));
  847. }
  848. /* Locked by busdma engine. */
  849. static void
  850. fsl_sata_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
  851. {
  852. struct fsl_sata_slot *slot = arg;
  853. struct fsl_sata_channel *ch = slot->ch;
  854. struct fsl_sata_cmd_tab *ctp;
  855. struct fsl_sata_dma_prd *prd;
  856. int i, j, len, extlen;
  857. if (error) {
  858. device_printf(ch->dev, "DMA load error %d\n", error);
  859. fsl_sata_end_transaction(slot, FSL_SATA_ERR_INVALID);
  860. return;
  861. }
  862. KASSERT(nsegs <= FSL_SATA_SG_ENTRIES - 1,
  863. ("too many DMA segment entries\n"));
  864. /* Get a piece of the workspace for this request */
  865. ctp = FSL_SATA_CTP(ch, slot);
  866. /* Fill S/G table */
  867. prd = &ctp->prd_tab[0];
  868. for (i = 0, j = 0; i < nsegs; i++, j++) {
  869. if (j == FSL_SATA_PRD_EXT_INDEX &&
  870. FSL_SATA_PRD_MAX_DIRECT < nsegs) {
  871. prd[j].dba = htole32(FSL_SATA_CTP_BUS(ch, slot) +
  872. FSL_SATA_PRD_OFFSET(j+1));
  873. j++;
  874. extlen = 0;
  875. }
  876. len = segs[i].ds_len;
  877. len = roundup2(len, sizeof(uint32_t));
  878. prd[j].dba = htole32((uint32_t)segs[i].ds_addr);
  879. prd[j].dwc_flg = htole32(FSL_SATA_PRD_SNOOP | len);
  880. slot->ttl += len;
  881. if (j > FSL_SATA_PRD_MAX_DIRECT)
  882. extlen += len;
  883. }
  884. slot->dma.nsegs = j;
  885. if (j > FSL_SATA_PRD_MAX_DIRECT)
  886. prd[FSL_SATA_PRD_EXT_INDEX].dwc_flg =
  887. htole32(FSL_SATA_PRD_SNOOP | FSL_SATA_PRD_EXT | extlen);
  888. bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
  889. ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
  890. BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
  891. fsl_sata_execute_transaction(slot);
  892. }
  893. /* Must be called with channel locked. */
  894. static void
  895. fsl_sata_execute_transaction(struct fsl_sata_slot *slot)
  896. {
  897. struct fsl_sata_channel *ch = slot->ch;
  898. struct fsl_sata_cmd_tab *ctp;
  899. struct fsl_sata_cmd_list *clp;
  900. union ccb *ccb = slot->ccb;
  901. int port = ccb->ccb_h.target_id & 0x0f;
  902. int fis_size, i, softreset;
  903. uint32_t tmp;
  904. uint32_t cmd_flags = FSL_SATA_CMD_WRITE | FSL_SATA_CMD_SNOOP;
  905. softreset = 0;
  906. CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
  907. ("fsl_sata_execute_transaction func_code=0x%x\n", ccb->ccb_h.func_code));
  908. /* Get a piece of the workspace for this request */
  909. ctp = FSL_SATA_CTP(ch, slot);
  910. /* Setup the FIS for this request */
  911. if (!(fis_size = fsl_sata_setup_fis(ch, ctp, ccb, slot->slot))) {
  912. device_printf(ch->dev, "Setting up SATA FIS failed\n");
  913. fsl_sata_end_transaction(slot, FSL_SATA_ERR_INVALID);
  914. return;
  915. }
  916. /* Setup the command list entry */
  917. clp = FSL_SATA_CLP(ch, slot);
  918. clp->fis_length = htole16(fis_size);
  919. clp->prd_length = htole16(slot->dma.nsegs);
  920. /* Special handling for Soft Reset command. */
  921. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  922. (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
  923. if (ccb->ataio.cmd.control & ATA_A_RESET) {
  924. softreset = 1;
  925. cmd_flags |= FSL_SATA_CMD_RESET;
  926. } else {
  927. /* Prepare FIS receive area for check. */
  928. for (i = 0; i < 32; i++)
  929. ctp->sfis[i] = 0xff;
  930. softreset = 2;
  931. }
  932. }
  933. if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)
  934. cmd_flags |= FSL_SATA_CMD_QUEUED;
  935. clp->cmd_flags = htole32(cmd_flags |
  936. (ccb->ccb_h.func_code == XPT_SCSI_IO ? FSL_SATA_CMD_ATAPI : 0) |
  937. slot->slot);
  938. clp->ttl = htole32(slot->ttl);
  939. clp->cda = htole32(FSL_SATA_CTP_BUS(ch, slot));
  940. bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
  941. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  942. /* Issue command to the controller. */
  943. slot->state = FSL_SATA_SLOT_RUNNING;
  944. ch->rslots |= (1 << slot->slot);
  945. ATA_OUTL(ch->r_mem, FSL_SATA_P_CQPMP, port);
  946. ATA_OUTL(ch->r_mem, FSL_SATA_P_CQR, (1 << slot->slot));
  947. /* Device reset commands don't interrupt. Poll them. */
  948. if (ccb->ccb_h.func_code == XPT_ATA_IO &&
  949. (ccb->ataio.cmd.command == ATA_DEVICE_RESET || softreset)) {
  950. int count, timeout = ccb->ccb_h.timeout * 100;
  951. enum fsl_sata_err_type et = FSL_SATA_ERR_NONE;
  952. for (count = 0; count < timeout; count++) {
  953. DELAY(10);
  954. tmp = 0;
  955. if (softreset == 2) {
  956. tmp = ATA_INL(ch->r_mem, FSL_SATA_P_SIG);
  957. if (tmp != 0 && tmp != 0xffffffff)
  958. break;
  959. continue;
  960. }
  961. if ((ATA_INL(ch->r_mem, FSL_SATA_P_CCR) & (1 << slot->slot)) != 0)
  962. break;
  963. }
  964. if (timeout && (count >= timeout)) {
  965. device_printf(ch->dev, "Poll timeout on slot %d port %d (round %d)\n",
  966. slot->slot, port, softreset);
  967. device_printf(ch->dev, "hsts %08x cqr %08x ccr %08x ss %08x "
  968. "rs %08x cer %08x der %08x serr %08x car %08x sig %08x\n",
  969. ATA_INL(ch->r_mem, FSL_SATA_P_HSTS),
  970. ATA_INL(ch->r_mem, FSL_SATA_P_CQR),
  971. ATA_INL(ch->r_mem, FSL_SATA_P_CCR),
  972. ATA_INL(ch->r_mem, FSL_SATA_P_SSTS), ch->rslots,
  973. ATA_INL(ch->r_mem, FSL_SATA_P_CER),
  974. ATA_INL(ch->r_mem, FSL_SATA_P_DER),
  975. ATA_INL(ch->r_mem, FSL_SATA_P_SERR),
  976. ATA_INL(ch->r_mem, FSL_SATA_P_CAR),
  977. ATA_INL(ch->r_mem, FSL_SATA_P_SIG));
  978. et = FSL_SATA_ERR_TIMEOUT;
  979. }
  980. fsl_sata_end_transaction(slot, et);
  981. return;
  982. }
  983. /* Start command execution timeout */
  984. callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout / 2,
  985. 0, fsl_sata_timeout, slot, 0);
  986. return;
  987. }
  988. /* Must be called with channel locked. */
  989. static void
  990. fsl_sata_process_timeout(struct fsl_sata_channel *ch)
  991. {
  992. int i;
  993. mtx_assert(&ch->mtx, MA_OWNED);
  994. /* Handle the rest of commands. */
  995. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  996. /* Do we have a running request on slot? */
  997. if (ch->slot[i].state < FSL_SATA_SLOT_RUNNING)
  998. continue;
  999. fsl_sata_end_transaction(&ch->slot[i], FSL_SATA_ERR_TIMEOUT);
  1000. }
  1001. }
  1002. /* Must be called with channel locked. */
  1003. static void
  1004. fsl_sata_rearm_timeout(struct fsl_sata_channel *ch)
  1005. {
  1006. int i;
  1007. mtx_assert(&ch->mtx, MA_OWNED);
  1008. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1009. struct fsl_sata_slot *slot = &ch->slot[i];
  1010. /* Do we have a running request on slot? */
  1011. if (slot->state < FSL_SATA_SLOT_RUNNING)
  1012. continue;
  1013. if ((ch->toslots & (1 << i)) == 0)
  1014. continue;
  1015. callout_reset_sbt(&slot->timeout,
  1016. SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
  1017. fsl_sata_timeout, slot, 0);
  1018. }
  1019. }
  1020. /* Locked by callout mechanism. */
  1021. static void
  1022. fsl_sata_timeout(void *arg)
  1023. {
  1024. struct fsl_sata_slot *slot = arg;
  1025. struct fsl_sata_channel *ch = slot->ch;
  1026. device_t dev = ch->dev;
  1027. uint32_t sstatus;
  1028. /* Check for stale timeout. */
  1029. if (slot->state < FSL_SATA_SLOT_RUNNING)
  1030. return;
  1031. /* Check if slot was not being executed last time we checked. */
  1032. if (slot->state < FSL_SATA_SLOT_EXECUTING) {
  1033. /* Check if slot started executing. */
  1034. sstatus = ATA_INL(ch->r_mem, FSL_SATA_P_CAR);
  1035. if ((sstatus & (1 << slot->slot)) != 0)
  1036. slot->state = FSL_SATA_SLOT_EXECUTING;
  1037. callout_reset_sbt(&slot->timeout,
  1038. SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
  1039. fsl_sata_timeout, slot, 0);
  1040. return;
  1041. }
  1042. device_printf(dev, "Timeout on slot %d port %d\n",
  1043. slot->slot, slot->ccb->ccb_h.target_id & 0x0f);
  1044. /* Handle frozen command. */
  1045. if (ch->frozen) {
  1046. union ccb *fccb = ch->frozen;
  1047. ch->frozen = NULL;
  1048. fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
  1049. if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
  1050. xpt_freeze_devq(fccb->ccb_h.path, 1);
  1051. fccb->ccb_h.status |= CAM_DEV_QFRZN;
  1052. }
  1053. fsl_sata_done(ch, fccb);
  1054. }
  1055. if (ch->toslots == 0)
  1056. xpt_freeze_simq(ch->sim, 1);
  1057. ch->toslots |= (1 << slot->slot);
  1058. if ((ch->rslots & ~ch->toslots) == 0)
  1059. fsl_sata_process_timeout(ch);
  1060. else
  1061. device_printf(dev, " ... waiting for slots %08x\n",
  1062. ch->rslots & ~ch->toslots);
  1063. }
  1064. /* Must be called with channel locked. */
  1065. static void
  1066. fsl_sata_end_transaction(struct fsl_sata_slot *slot, enum fsl_sata_err_type et)
  1067. {
  1068. struct fsl_sata_channel *ch = slot->ch;
  1069. union ccb *ccb = slot->ccb;
  1070. struct fsl_sata_cmd_list *clp;
  1071. int lastto;
  1072. uint32_t sig;
  1073. bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
  1074. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1075. clp = FSL_SATA_CLP(ch, slot);
  1076. /* Read result registers to the result struct */
  1077. if (ccb->ccb_h.func_code == XPT_ATA_IO) {
  1078. struct ata_res *res = &ccb->ataio.res;
  1079. if ((et == FSL_SATA_ERR_TFE) ||
  1080. (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
  1081. struct fsl_sata_cmd_tab *ctp = FSL_SATA_CTP(ch, slot);
  1082. uint8_t *fis = ctp->sfis;
  1083. res->status = fis[2];
  1084. res->error = fis[3];
  1085. res->lba_low = fis[4];
  1086. res->lba_mid = fis[5];
  1087. res->lba_high = fis[6];
  1088. res->device = fis[7];
  1089. res->lba_low_exp = fis[8];
  1090. res->lba_mid_exp = fis[9];
  1091. res->lba_high_exp = fis[10];
  1092. res->sector_count = fis[12];
  1093. res->sector_count_exp = fis[13];
  1094. if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
  1095. (ccb->ataio.cmd.control & ATA_A_RESET) == 0) {
  1096. sig = ATA_INL(ch->r_mem, FSL_SATA_P_SIG);
  1097. res->lba_high = sig >> 24;
  1098. res->lba_mid = sig >> 16;
  1099. res->lba_low = sig >> 8;
  1100. res->sector_count = sig;
  1101. }
  1102. } else
  1103. bzero(res, sizeof(*res));
  1104. if ((ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) == 0 &&
  1105. (ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  1106. ccb->ataio.resid =
  1107. ccb->ataio.dxfer_len - le32toh(clp->ttl);
  1108. }
  1109. } else {
  1110. if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  1111. ccb->csio.resid =
  1112. ccb->csio.dxfer_len - le32toh(clp->ttl);
  1113. }
  1114. }
  1115. if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
  1116. bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
  1117. (ccb->ccb_h.flags & CAM_DIR_IN) ?
  1118. BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
  1119. bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
  1120. }
  1121. if (et != FSL_SATA_ERR_NONE)
  1122. ch->eslots |= (1 << slot->slot);
  1123. /* In case of error, freeze device for proper recovery. */
  1124. if ((et != FSL_SATA_ERR_NONE) && (!ch->recoverycmd) &&
  1125. !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
  1126. xpt_freeze_devq(ccb->ccb_h.path, 1);
  1127. ccb->ccb_h.status |= CAM_DEV_QFRZN;
  1128. }
  1129. /* Set proper result status. */
  1130. ccb->ccb_h.status &= ~CAM_STATUS_MASK;
  1131. switch (et) {
  1132. case FSL_SATA_ERR_NONE:
  1133. ccb->ccb_h.status |= CAM_REQ_CMP;
  1134. if (ccb->ccb_h.func_code == XPT_SCSI_IO)
  1135. ccb->csio.scsi_status = SCSI_STATUS_OK;
  1136. break;
  1137. case FSL_SATA_ERR_INVALID:
  1138. ch->fatalerr = 1;
  1139. ccb->ccb_h.status |= CAM_REQ_INVALID;
  1140. break;
  1141. case FSL_SATA_ERR_INNOCENT:
  1142. ccb->ccb_h.status |= CAM_REQUEUE_REQ;
  1143. break;
  1144. case FSL_SATA_ERR_TFE:
  1145. case FSL_SATA_ERR_NCQ:
  1146. if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
  1147. ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
  1148. ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
  1149. } else {
  1150. ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
  1151. }
  1152. break;
  1153. case FSL_SATA_ERR_SATA:
  1154. ch->fatalerr = 1;
  1155. if (!ch->recoverycmd) {
  1156. xpt_freeze_simq(ch->sim, 1);
  1157. ccb->ccb_h.status &= ~CAM_STATUS_MASK;
  1158. ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
  1159. }
  1160. ccb->ccb_h.status |= CAM_UNCOR_PARITY;
  1161. break;
  1162. case FSL_SATA_ERR_TIMEOUT:
  1163. if (!ch->recoverycmd) {
  1164. xpt_freeze_simq(ch->sim, 1);
  1165. ccb->ccb_h.status &= ~CAM_STATUS_MASK;
  1166. ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
  1167. }
  1168. ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
  1169. break;
  1170. default:
  1171. ch->fatalerr = 1;
  1172. ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
  1173. }
  1174. /* Free slot. */
  1175. ch->oslots &= ~(1 << slot->slot);
  1176. ch->rslots &= ~(1 << slot->slot);
  1177. ch->aslots &= ~(1 << slot->slot);
  1178. slot->state = FSL_SATA_SLOT_EMPTY;
  1179. slot->ccb = NULL;
  1180. /* Update channel stats. */
  1181. ch->numrslots--;
  1182. ch->numrslotspd[ccb->ccb_h.target_id]--;
  1183. ATA_OUTL(ch->r_mem, FSL_SATA_P_CCR, 1 << slot->slot);
  1184. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  1185. (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
  1186. ch->numtslots--;
  1187. ch->numtslotspd[ccb->ccb_h.target_id]--;
  1188. }
  1189. /* Cancel timeout state if request completed normally. */
  1190. if (et != FSL_SATA_ERR_TIMEOUT) {
  1191. lastto = (ch->toslots == (1 << slot->slot));
  1192. ch->toslots &= ~(1 << slot->slot);
  1193. if (lastto)
  1194. xpt_release_simq(ch->sim, TRUE);
  1195. }
  1196. /* If it was first request of reset sequence and there is no error,
  1197. * proceed to second request. */
  1198. if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
  1199. (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
  1200. (ccb->ataio.cmd.control & ATA_A_RESET) &&
  1201. et == FSL_SATA_ERR_NONE) {
  1202. ccb->ataio.cmd.control &= ~ATA_A_RESET;
  1203. fsl_sata_begin_transaction(ch, ccb);
  1204. return;
  1205. }
  1206. /* If it was our READ LOG command - process it. */
  1207. if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
  1208. fsl_sata_process_read_log(ch, ccb);
  1209. /* If it was our REQUEST SENSE command - process it. */
  1210. } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
  1211. fsl_sata_process_request_sense(ch, ccb);
  1212. /* If it was NCQ or ATAPI command error, put result on hold. */
  1213. } else if (et == FSL_SATA_ERR_NCQ ||
  1214. ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
  1215. (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
  1216. ch->hold[slot->slot] = ccb;
  1217. ch->numhslots++;
  1218. } else
  1219. fsl_sata_done(ch, ccb);
  1220. /* If we have no other active commands, ... */
  1221. if (ch->rslots == 0) {
  1222. /* if there was fatal error - reset port. */
  1223. if (ch->toslots != 0 || ch->fatalerr) {
  1224. fsl_sata_reset(ch);
  1225. } else {
  1226. /* if we have slots in error, we can reinit port. */
  1227. if (ch->eslots != 0) {
  1228. fsl_sata_stop(ch);
  1229. fsl_sata_start(ch);
  1230. }
  1231. /* if there commands on hold, we can do READ LOG. */
  1232. if (!ch->recoverycmd && ch->numhslots)
  1233. fsl_sata_issue_recovery(ch);
  1234. }
  1235. /* If all the rest of commands are in timeout - give them chance. */
  1236. } else if ((ch->rslots & ~ch->toslots) == 0 &&
  1237. et != FSL_SATA_ERR_TIMEOUT)
  1238. fsl_sata_rearm_timeout(ch);
  1239. /* Unfreeze frozen command. */
  1240. if (ch->frozen && !fsl_sata_check_collision(ch, ch->frozen)) {
  1241. union ccb *fccb = ch->frozen;
  1242. ch->frozen = NULL;
  1243. fsl_sata_begin_transaction(ch, fccb);
  1244. xpt_release_simq(ch->sim, TRUE);
  1245. }
  1246. /* Start PM timer. */
  1247. if (ch->numrslots == 0 && ch->pm_level > 3 &&
  1248. (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) {
  1249. callout_schedule(&ch->pm_timer,
  1250. (ch->pm_level == 4) ? hz / 1000 : hz / 8);
  1251. }
  1252. }
  1253. static void
  1254. fsl_sata_issue_recovery(struct fsl_sata_channel *ch)
  1255. {
  1256. union ccb *ccb;
  1257. struct ccb_ataio *ataio;
  1258. struct ccb_scsiio *csio;
  1259. int i;
  1260. /* Find some held command. */
  1261. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1262. if (ch->hold[i])
  1263. break;
  1264. }
  1265. ccb = xpt_alloc_ccb_nowait();
  1266. if (ccb == NULL) {
  1267. device_printf(ch->dev, "Unable to allocate recovery command\n");
  1268. completeall:
  1269. /* We can't do anything -- complete held commands. */
  1270. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1271. if (ch->hold[i] == NULL)
  1272. continue;
  1273. ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
  1274. ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL;
  1275. fsl_sata_done(ch, ch->hold[i]);
  1276. ch->hold[i] = NULL;
  1277. ch->numhslots--;
  1278. }
  1279. fsl_sata_reset(ch);
  1280. return;
  1281. }
  1282. ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */
  1283. if (ccb->ccb_h.func_code == XPT_ATA_IO) {
  1284. /* READ LOG */
  1285. ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
  1286. ccb->ccb_h.func_code = XPT_ATA_IO;
  1287. ccb->ccb_h.flags = CAM_DIR_IN;
  1288. ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
  1289. ataio = &ccb->ataio;
  1290. ataio->data_ptr = malloc(512, M_FSL_SATA, M_NOWAIT);
  1291. if (ataio->data_ptr == NULL) {
  1292. xpt_free_ccb(ccb);
  1293. device_printf(ch->dev,
  1294. "Unable to allocate memory for READ LOG command\n");
  1295. goto completeall;
  1296. }
  1297. ataio->dxfer_len = 512;
  1298. bzero(&ataio->cmd, sizeof(ataio->cmd));
  1299. ataio->cmd.flags = CAM_ATAIO_48BIT;
  1300. ataio->cmd.command = 0x2F; /* READ LOG EXT */
  1301. ataio->cmd.sector_count = 1;
  1302. ataio->cmd.sector_count_exp = 0;
  1303. ataio->cmd.lba_low = 0x10;
  1304. ataio->cmd.lba_mid = 0;
  1305. ataio->cmd.lba_mid_exp = 0;
  1306. } else {
  1307. /* REQUEST SENSE */
  1308. ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
  1309. ccb->ccb_h.recovery_slot = i;
  1310. ccb->ccb_h.func_code = XPT_SCSI_IO;
  1311. ccb->ccb_h.flags = CAM_DIR_IN;
  1312. ccb->ccb_h.status = 0;
  1313. ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
  1314. csio = &ccb->csio;
  1315. csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
  1316. csio->dxfer_len = ch->hold[i]->csio.sense_len;
  1317. csio->cdb_len = 6;
  1318. bzero(&csio->cdb_io, sizeof(csio->cdb_io));
  1319. csio->cdb_io.cdb_bytes[0] = 0x03;
  1320. csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
  1321. }
  1322. /* Freeze SIM while doing recovery. */
  1323. ch->recoverycmd = 1;
  1324. xpt_freeze_simq(ch->sim, 1);
  1325. fsl_sata_begin_transaction(ch, ccb);
  1326. }
  1327. static void
  1328. fsl_sata_process_read_log(struct fsl_sata_channel *ch, union ccb *ccb)
  1329. {
  1330. uint8_t *data;
  1331. struct ata_res *res;
  1332. int i;
  1333. ch->recoverycmd = 0;
  1334. data = ccb->ataio.data_ptr;
  1335. if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
  1336. (data[0] & 0x80) == 0) {
  1337. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1338. if (!ch->hold[i])
  1339. continue;
  1340. if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
  1341. continue;
  1342. if ((data[0] & 0x1F) == i) {
  1343. res = &ch->hold[i]->ataio.res;
  1344. res->status = data[2];
  1345. res->error = data[3];
  1346. res->lba_low = data[4];
  1347. res->lba_mid = data[5];
  1348. res->lba_high = data[6];
  1349. res->device = data[7];
  1350. res->lba_low_exp = data[8];
  1351. res->lba_mid_exp = data[9];
  1352. res->lba_high_exp = data[10];
  1353. res->sector_count = data[12];
  1354. res->sector_count_exp = data[13];
  1355. } else {
  1356. ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
  1357. ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
  1358. }
  1359. fsl_sata_done(ch, ch->hold[i]);
  1360. ch->hold[i] = NULL;
  1361. ch->numhslots--;
  1362. }
  1363. } else {
  1364. if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
  1365. device_printf(ch->dev, "Error while READ LOG EXT\n");
  1366. else if ((data[0] & 0x80) == 0) {
  1367. device_printf(ch->dev, "Non-queued command error in READ LOG EXT\n");
  1368. }
  1369. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1370. if (!ch->hold[i])
  1371. continue;
  1372. if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
  1373. continue;
  1374. fsl_sata_done(ch, ch->hold[i]);
  1375. ch->hold[i] = NULL;
  1376. ch->numhslots--;
  1377. }
  1378. }
  1379. free(ccb->ataio.data_ptr, M_FSL_SATA);
  1380. xpt_free_ccb(ccb);
  1381. xpt_release_simq(ch->sim, TRUE);
  1382. }
  1383. static void
  1384. fsl_sata_process_request_sense(struct fsl_sata_channel *ch, union ccb *ccb)
  1385. {
  1386. int i;
  1387. ch->recoverycmd = 0;
  1388. i = ccb->ccb_h.recovery_slot;
  1389. if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
  1390. ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
  1391. } else {
  1392. ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
  1393. ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
  1394. }
  1395. fsl_sata_done(ch, ch->hold[i]);
  1396. ch->hold[i] = NULL;
  1397. ch->numhslots--;
  1398. xpt_free_ccb(ccb);
  1399. xpt_release_simq(ch->sim, TRUE);
  1400. }
  1401. static void
  1402. fsl_sata_start(struct fsl_sata_channel *ch)
  1403. {
  1404. u_int32_t cmd;
  1405. /* Clear SATA error register */
  1406. ATA_OUTL(ch->r_mem, FSL_SATA_P_SERR, 0xFFFFFFFF);
  1407. /* Clear any interrupts pending on this channel */
  1408. ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS, 0x3F);
  1409. ATA_OUTL(ch->r_mem, FSL_SATA_P_CER, 0xFFFF);
  1410. ATA_OUTL(ch->r_mem, FSL_SATA_P_DER, 0xFFFF);
  1411. /* Start operations on this channel */
  1412. cmd = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
  1413. cmd |= FSL_SATA_P_HCTRL_HC_ON | FSL_SATA_P_HCTRL_SNOOP;
  1414. cmd &= ~FSL_SATA_P_HCTRL_HC_FORCE_OFF;
  1415. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, cmd |
  1416. (ch->pm_present ? FSL_SATA_P_HCTRL_PM : 0));
  1417. fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
  1418. FSL_SATA_P_HSTS_PR, FSL_SATA_P_HSTS_PR, 500);
  1419. ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS,
  1420. ATA_INL(ch->r_mem, FSL_SATA_P_HSTS) & FSL_SATA_P_HSTS_PR);
  1421. }
  1422. static void
  1423. fsl_sata_stop(struct fsl_sata_channel *ch)
  1424. {
  1425. uint32_t cmd;
  1426. int i;
  1427. /* Kill all activity on this channel */
  1428. cmd = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
  1429. cmd &= ~FSL_SATA_P_HCTRL_HC_ON;
  1430. for (i = 0; i < 2; i++) {
  1431. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, cmd);
  1432. if (fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
  1433. FSL_SATA_P_HSTS_HS_ON, 0, 500)) {
  1434. if (i != 0)
  1435. device_printf(ch->dev,
  1436. "stopping FSL SATA engine failed\n");
  1437. cmd |= FSL_SATA_P_HCTRL_HC_FORCE_OFF;
  1438. } else
  1439. break;
  1440. }
  1441. ch->eslots = 0;
  1442. }
  1443. static void
  1444. fsl_sata_reset(struct fsl_sata_channel *ch)
  1445. {
  1446. uint32_t ctrl;
  1447. int i;
  1448. xpt_freeze_simq(ch->sim, 1);
  1449. if (bootverbose)
  1450. device_printf(ch->dev, "FSL SATA reset...\n");
  1451. /* Requeue freezed command. */
  1452. if (ch->frozen) {
  1453. union ccb *fccb = ch->frozen;
  1454. ch->frozen = NULL;
  1455. fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
  1456. if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
  1457. xpt_freeze_devq(fccb->ccb_h.path, 1);
  1458. fccb->ccb_h.status |= CAM_DEV_QFRZN;
  1459. }
  1460. fsl_sata_done(ch, fccb);
  1461. }
  1462. /* Kill the engine and requeue all running commands. */
  1463. fsl_sata_stop(ch);
  1464. DELAY(1000); /* sleep for 1ms */
  1465. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1466. /* Do we have a running request on slot? */
  1467. if (ch->slot[i].state < FSL_SATA_SLOT_RUNNING)
  1468. continue;
  1469. /* XXX; Commands in loading state. */
  1470. fsl_sata_end_transaction(&ch->slot[i], FSL_SATA_ERR_INNOCENT);
  1471. }
  1472. for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
  1473. if (!ch->hold[i])
  1474. continue;
  1475. fsl_sata_done(ch, ch->hold[i]);
  1476. ch->hold[i] = NULL;
  1477. ch->numhslots--;
  1478. }
  1479. if (ch->toslots != 0)
  1480. xpt_release_simq(ch->sim, TRUE);
  1481. ch->eslots = 0;
  1482. ch->toslots = 0;
  1483. ch->fatalerr = 0;
  1484. /* Tell the XPT about the event */
  1485. xpt_async(AC_BUS_RESET, ch->path, NULL);
  1486. /* Disable port interrupts */
  1487. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
  1488. ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) & ~0x3f);
  1489. /* Reset and reconnect PHY, */
  1490. fsl_sata_start(ch);
  1491. if (fsl_sata_wait_register(ch, FSL_SATA_P_HSTS, 0x08, 0x08, 500)) {
  1492. if (bootverbose)
  1493. device_printf(ch->dev,
  1494. "FSL SATA reset: device not found\n");
  1495. ch->devices = 0;
  1496. /* Enable wanted port interrupts */
  1497. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
  1498. ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) | FSL_SATA_P_HCTRL_PHYRDY);
  1499. xpt_release_simq(ch->sim, TRUE);
  1500. return;
  1501. }
  1502. if (bootverbose)
  1503. device_printf(ch->dev, "FSL SATA reset: device found\n");
  1504. ch->devices = 1;
  1505. /* Enable wanted port interrupts */
  1506. ctrl = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) & ~0x3f;
  1507. ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
  1508. ctrl | FSL_SATA_P_HCTRL_FATAL | FSL_SATA_P_HCTRL_PHYRDY |
  1509. FSL_SATA_P_HCTRL_SIG | FSL_SATA_P_HCTRL_SNTFY |
  1510. FSL_SATA_P_HCTRL_DE | FSL_SATA_P_HCTRL_CC);
  1511. xpt_release_simq(ch->sim, TRUE);
  1512. }
  1513. static int
  1514. fsl_sata_setup_fis(struct fsl_sata_channel *ch, struct fsl_sata_cmd_tab *ctp, union ccb *ccb, int tag)
  1515. {
  1516. uint8_t *fis = &ctp->cfis[0];
  1517. bzero(fis, 32);
  1518. fis[0] = 0x27; /* host to device */
  1519. fis[1] = (ccb->ccb_h.target_id & 0x0f);
  1520. if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
  1521. fis[1] |= 0x80;
  1522. fis[2] = ATA_PACKET_CMD;
  1523. if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
  1524. ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
  1525. fis[3] = ATA_F_DMA;
  1526. else {
  1527. fis[5] = ccb->csio.dxfer_len;
  1528. fis[6] = ccb->csio.dxfer_len >> 8;
  1529. }
  1530. fis[7] = ATA_D_LBA;
  1531. fis[15] = ATA_A_4BIT;
  1532. bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
  1533. ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
  1534. ctp->acmd, ccb->csio.cdb_len);
  1535. bzero(ctp->acmd + ccb->csio.cdb_len, 32 - ccb->csio.cdb_len);
  1536. } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
  1537. fis[1] |= 0x80;
  1538. fis[2] = ccb->ataio.cmd.command;
  1539. fis[3] = ccb->ataio.cmd.features;
  1540. fis[4] = ccb->ataio.cmd.lba_low;
  1541. fis[5] = ccb->ataio.cmd.lba_mid;
  1542. fis[6] = ccb->ataio.cmd.lba_high;
  1543. fis[7] = ccb->ataio.cmd.device;
  1544. fis[8] = ccb->ataio.cmd.lba_low_exp;
  1545. fis[9] = ccb->ataio.cmd.lba_mid_exp;
  1546. fis[10] = ccb->ataio.cmd.lba_high_exp;
  1547. fis[11] = ccb->ataio.cmd.features_exp;
  1548. if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
  1549. fis[12] = tag << 3;
  1550. fis[13] = 0;
  1551. } else {
  1552. fis[12] = ccb->ataio.cmd.sector_count;
  1553. fis[13] = ccb->ataio.cmd.sector_count_exp;
  1554. }
  1555. fis[15] = ATA_A_4BIT;
  1556. } else {
  1557. fis[15] = ccb->ataio.cmd.control;
  1558. }
  1559. return (20);
  1560. }
  1561. static int
  1562. fsl_sata_check_ids(struct fsl_sata_channel *ch, union ccb *ccb)
  1563. {
  1564. if (ccb->ccb_h.target_id > 15) {
  1565. ccb->ccb_h.status = CAM_TID_INVALID;
  1566. fsl_sata_done(ch, ccb);
  1567. return (-1);
  1568. }
  1569. if (ccb->ccb_h.target_lun != 0) {
  1570. ccb->ccb_h.status = CAM_LUN_INVALID;
  1571. fsl_sata_done(ch, ccb);
  1572. return (-1);
  1573. }
  1574. return (0);
  1575. }
  1576. static void
  1577. fsl_sataaction(struct cam_sim *sim, union ccb *ccb)
  1578. {
  1579. struct fsl_sata_channel *ch;
  1580. CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
  1581. ("fsl_sataaction func_code=0x%x\n", ccb->ccb_h.func_code));
  1582. ch = (struct fsl_sata_channel *)cam_sim_softc(sim);
  1583. switch (ccb->ccb_h.func_code) {
  1584. /* Common cases first */
  1585. case XPT_ATA_IO: /* Execute the requested I/O operation */
  1586. case XPT_SCSI_IO:
  1587. if (fsl_sata_check_ids(ch, ccb))
  1588. return;
  1589. if (ch->devices == 0 ||
  1590. (ch->pm_present == 0 &&
  1591. ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
  1592. ccb->ccb_h.status = CAM_SEL_TIMEOUT;
  1593. break;
  1594. }
  1595. ccb->ccb_h.recovery_type = RECOVERY_NONE;
  1596. /* Check for command collision. */
  1597. if (fsl_sata_check_collision(ch, ccb)) {
  1598. /* Freeze command. */
  1599. ch->frozen = ccb;
  1600. /* We have only one frozen slot, so freeze simq also. */
  1601. xpt_freeze_simq(ch->sim, 1);
  1602. return;
  1603. }
  1604. fsl_sata_begin_transaction(ch, ccb);
  1605. return;
  1606. case XPT_ABORT: /* Abort the specified CCB */
  1607. /* XXX Implement */
  1608. ccb->ccb_h.status = CAM_REQ_INVALID;
  1609. break;
  1610. case XPT_SET_TRAN_SETTINGS:
  1611. {
  1612. struct ccb_trans_settings *cts = &ccb->cts;
  1613. struct fsl_sata_device *d;
  1614. if (fsl_sata_check_ids(ch, ccb))
  1615. return;
  1616. if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
  1617. d = &ch->curr[ccb->ccb_h.target_id];
  1618. else
  1619. d = &ch->user[ccb->ccb_h.target_id];
  1620. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
  1621. d->revision = cts->xport_specific.sata.revision;
  1622. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
  1623. d->mode = cts->xport_specific.sata.mode;
  1624. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
  1625. d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
  1626. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
  1627. d->tags = min(FSL_SATA_MAX_SLOTS, cts->xport_specific.sata.tags);
  1628. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM)
  1629. ch->pm_present = cts->xport_specific.sata.pm_present;
  1630. if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
  1631. d->atapi = cts->xport_specific.sata.atapi;
  1632. ccb->ccb_h.status = CAM_REQ_CMP;
  1633. break;
  1634. }
  1635. case XPT_GET_TRAN_SETTINGS:
  1636. /* Get default/user set transfer settings for the target */
  1637. {
  1638. struct ccb_trans_settings *cts = &ccb->cts;
  1639. struct fsl_sata_device *d;
  1640. uint32_t status;
  1641. if (fsl_sata_check_ids(ch, ccb))
  1642. return;
  1643. if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
  1644. d = &ch->curr[ccb->ccb_h.target_id];
  1645. else
  1646. d = &ch->user[ccb->ccb_h.target_id];
  1647. cts->protocol = PROTO_UNSPECIFIED;
  1648. cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
  1649. cts->transport = XPORT_SATA;
  1650. cts->transport_version = XPORT_VERSION_UNSPECIFIED;
  1651. cts->proto_specific.valid = 0;
  1652. cts->xport_specific.sata.valid = 0;
  1653. if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
  1654. (ccb->ccb_h.target_id == 15 ||
  1655. (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
  1656. status = ATA_INL(ch->r_mem, FSL_SATA_P_SSTS) & ATA_SS_SPD_MASK;
  1657. if (status & 0x0f0) {
  1658. cts->xport_specific.sata.revision =
  1659. (status & 0x0f0) >> 4;
  1660. cts->xport_specific.sata.valid |=
  1661. CTS_SATA_VALID_REVISION;
  1662. }
  1663. cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
  1664. if (ch->pm_level) {
  1665. cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
  1666. }
  1667. cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
  1668. cts->xport_specific.sata.caps &=
  1669. ch->user[ccb->ccb_h.target_id].caps;
  1670. cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
  1671. } else {
  1672. cts->xport_specific.sata.revision = d->revision;
  1673. cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
  1674. cts->xport_specific.sata.caps = d->caps;
  1675. cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
  1676. }
  1677. cts->xport_specific.sata.mode = d->mode;
  1678. cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
  1679. cts->xport_specific.sata.bytecount = d->bytecount;
  1680. cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
  1681. cts->xport_specific.sata.pm_present = ch->pm_present;
  1682. cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
  1683. cts->xport_specific.sata.tags = d->tags;
  1684. cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
  1685. cts->xport_specific.sata.atapi = d->atapi;
  1686. cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
  1687. ccb->ccb_h.status = CAM_REQ_CMP;
  1688. break;
  1689. }
  1690. case XPT_RESET_BUS: /* Reset the specified SCSI bus */
  1691. case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
  1692. fsl_sata_reset(ch);
  1693. ccb->ccb_h.status = CAM_REQ_CMP;
  1694. break;
  1695. case XPT_TERM_IO: /* Terminate the I/O process */
  1696. /* XXX Implement */
  1697. ccb->ccb_h.status = CAM_REQ_INVALID;
  1698. break;
  1699. case XPT_PATH_INQ: /* Path routing inquiry */
  1700. {
  1701. struct ccb_pathinq *cpi = &ccb->cpi;
  1702. cpi->version_num = 1; /* XXX??? */
  1703. cpi->hba_inquiry = PI_SDTR_ABLE;
  1704. cpi->hba_inquiry |= PI_TAG_ABLE;
  1705. #if 0
  1706. /*
  1707. * XXX: CAM tries to reset port 15 if it sees port multiplier
  1708. * support. Disable it for now.
  1709. */
  1710. cpi->hba_inquiry |= PI_SATAPM;
  1711. #endif
  1712. cpi->target_sprt = 0;
  1713. cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED;
  1714. cpi->hba_eng_cnt = 0;
  1715. /*
  1716. * XXX: This should be 15, since hardware *does* support a port
  1717. * multiplier. See above.
  1718. */
  1719. cpi->max_target = 0;
  1720. cpi->max_lun = 0;
  1721. cpi->initiator_id = 0;
  1722. cpi->bus_id = cam_sim_bus(sim);
  1723. cpi->base_transfer_speed = 150000;
  1724. strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
  1725. strncpy(cpi->hba_vid, "FSL SATA", HBA_IDLEN);
  1726. strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
  1727. cpi->unit_number = cam_sim_unit(sim);
  1728. cpi->transport = XPORT_SATA;
  1729. cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
  1730. cpi->protocol = PROTO_ATA;
  1731. cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
  1732. cpi->maxio = maxphys;
  1733. cpi->ccb_h.status = CAM_REQ_CMP;
  1734. break;
  1735. }
  1736. default:
  1737. ccb->ccb_h.status = CAM_REQ_INVALID;
  1738. break;
  1739. }
  1740. fsl_sata_done(ch, ccb);
  1741. }
  1742. static void
  1743. fsl_satapoll(struct cam_sim *sim)
  1744. {
  1745. struct fsl_sata_channel *ch = (struct fsl_sata_channel *)cam_sim_softc(sim);
  1746. uint32_t istatus;
  1747. /* Read interrupt statuses and process if any. */
  1748. istatus = ATA_INL(ch->r_mem, FSL_SATA_P_HSTS);
  1749. if (istatus != 0)
  1750. fsl_sata_intr_main(ch, istatus);
  1751. }
  1752. MODULE_VERSION(fsl_sata, 1);
  1753. MODULE_DEPEND(fsl_sata, cam, 1, 1, 1);