mv_sas.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*
  2. * Marvell 88SE64xx/88SE94xx main function
  3. *
  4. * Copyright 2007 Red Hat, Inc.
  5. * Copyright 2008 Marvell. <kewei@marvell.com>
  6. * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
  7. *
  8. * This file is licensed under GPLv2.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; version 2 of the
  13. * License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. * USA
  24. */
  25. #include "mv_sas.h"
  26. static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
  27. {
  28. if (task->lldd_task) {
  29. struct mvs_slot_info *slot;
  30. slot = task->lldd_task;
  31. *tag = slot->slot_tag;
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
  37. {
  38. void *bitmap = mvi->tags;
  39. clear_bit(tag, bitmap);
  40. }
  41. void mvs_tag_free(struct mvs_info *mvi, u32 tag)
  42. {
  43. mvs_tag_clear(mvi, tag);
  44. }
  45. void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
  46. {
  47. void *bitmap = mvi->tags;
  48. set_bit(tag, bitmap);
  49. }
  50. inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
  51. {
  52. unsigned int index, tag;
  53. void *bitmap = mvi->tags;
  54. index = find_first_zero_bit(bitmap, mvi->tags_num);
  55. tag = index;
  56. if (tag >= mvi->tags_num)
  57. return -SAS_QUEUE_FULL;
  58. mvs_tag_set(mvi, tag);
  59. *tag_out = tag;
  60. return 0;
  61. }
  62. void mvs_tag_init(struct mvs_info *mvi)
  63. {
  64. int i;
  65. for (i = 0; i < mvi->tags_num; ++i)
  66. mvs_tag_clear(mvi, i);
  67. }
  68. struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
  69. {
  70. unsigned long i = 0, j = 0, hi = 0;
  71. struct sas_ha_struct *sha = dev->port->ha;
  72. struct mvs_info *mvi = NULL;
  73. struct asd_sas_phy *phy;
  74. while (sha->sas_port[i]) {
  75. if (sha->sas_port[i] == dev->port) {
  76. phy = container_of(sha->sas_port[i]->phy_list.next,
  77. struct asd_sas_phy, port_phy_el);
  78. j = 0;
  79. while (sha->sas_phy[j]) {
  80. if (sha->sas_phy[j] == phy)
  81. break;
  82. j++;
  83. }
  84. break;
  85. }
  86. i++;
  87. }
  88. hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
  89. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
  90. return mvi;
  91. }
  92. int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
  93. {
  94. unsigned long i = 0, j = 0, n = 0, num = 0;
  95. struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
  96. struct mvs_info *mvi = mvi_dev->mvi_info;
  97. struct sas_ha_struct *sha = dev->port->ha;
  98. while (sha->sas_port[i]) {
  99. if (sha->sas_port[i] == dev->port) {
  100. struct asd_sas_phy *phy;
  101. list_for_each_entry(phy,
  102. &sha->sas_port[i]->phy_list, port_phy_el) {
  103. j = 0;
  104. while (sha->sas_phy[j]) {
  105. if (sha->sas_phy[j] == phy)
  106. break;
  107. j++;
  108. }
  109. phyno[n] = (j >= mvi->chip->n_phy) ?
  110. (j - mvi->chip->n_phy) : j;
  111. num++;
  112. n++;
  113. }
  114. break;
  115. }
  116. i++;
  117. }
  118. return num;
  119. }
  120. struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
  121. u8 reg_set)
  122. {
  123. u32 dev_no;
  124. for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
  125. if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
  126. continue;
  127. if (mvi->devices[dev_no].taskfileset == reg_set)
  128. return &mvi->devices[dev_no];
  129. }
  130. return NULL;
  131. }
  132. static inline void mvs_free_reg_set(struct mvs_info *mvi,
  133. struct mvs_device *dev)
  134. {
  135. if (!dev) {
  136. mv_printk("device has been free.\n");
  137. return;
  138. }
  139. if (dev->taskfileset == MVS_ID_NOT_MAPPED)
  140. return;
  141. MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
  142. }
  143. static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
  144. struct mvs_device *dev)
  145. {
  146. if (dev->taskfileset != MVS_ID_NOT_MAPPED)
  147. return 0;
  148. return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
  149. }
  150. void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
  151. {
  152. u32 no;
  153. for_each_phy(phy_mask, phy_mask, no) {
  154. if (!(phy_mask & 1))
  155. continue;
  156. MVS_CHIP_DISP->phy_reset(mvi, no, hard);
  157. }
  158. }
  159. int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
  160. void *funcdata)
  161. {
  162. int rc = 0, phy_id = sas_phy->id;
  163. u32 tmp, i = 0, hi;
  164. struct sas_ha_struct *sha = sas_phy->ha;
  165. struct mvs_info *mvi = NULL;
  166. while (sha->sas_phy[i]) {
  167. if (sha->sas_phy[i] == sas_phy)
  168. break;
  169. i++;
  170. }
  171. hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
  172. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
  173. switch (func) {
  174. case PHY_FUNC_SET_LINK_RATE:
  175. MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
  176. break;
  177. case PHY_FUNC_HARD_RESET:
  178. tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
  179. if (tmp & PHY_RST_HARD)
  180. break;
  181. MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
  182. break;
  183. case PHY_FUNC_LINK_RESET:
  184. MVS_CHIP_DISP->phy_enable(mvi, phy_id);
  185. MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_SOFT_RESET);
  186. break;
  187. case PHY_FUNC_DISABLE:
  188. MVS_CHIP_DISP->phy_disable(mvi, phy_id);
  189. break;
  190. case PHY_FUNC_RELEASE_SPINUP_HOLD:
  191. default:
  192. rc = -ENOSYS;
  193. }
  194. msleep(200);
  195. return rc;
  196. }
  197. void mvs_set_sas_addr(struct mvs_info *mvi, int port_id, u32 off_lo,
  198. u32 off_hi, u64 sas_addr)
  199. {
  200. u32 lo = (u32)sas_addr;
  201. u32 hi = (u32)(sas_addr>>32);
  202. MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
  203. MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
  204. MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
  205. MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
  206. }
  207. static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
  208. {
  209. struct mvs_phy *phy = &mvi->phy[i];
  210. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  211. struct sas_ha_struct *sas_ha;
  212. if (!phy->phy_attached)
  213. return;
  214. if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
  215. && phy->phy_type & PORT_TYPE_SAS) {
  216. return;
  217. }
  218. sas_ha = mvi->sas;
  219. sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
  220. if (sas_phy->phy) {
  221. struct sas_phy *sphy = sas_phy->phy;
  222. sphy->negotiated_linkrate = sas_phy->linkrate;
  223. sphy->minimum_linkrate = phy->minimum_linkrate;
  224. sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  225. sphy->maximum_linkrate = phy->maximum_linkrate;
  226. sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
  227. }
  228. if (phy->phy_type & PORT_TYPE_SAS) {
  229. struct sas_identify_frame *id;
  230. id = (struct sas_identify_frame *)phy->frame_rcvd;
  231. id->dev_type = phy->identify.device_type;
  232. id->initiator_bits = SAS_PROTOCOL_ALL;
  233. id->target_bits = phy->identify.target_port_protocols;
  234. /* direct attached SAS device */
  235. if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
  236. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
  237. MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x00);
  238. }
  239. } else if (phy->phy_type & PORT_TYPE_SATA) {
  240. /*Nothing*/
  241. }
  242. mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
  243. sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
  244. mvi->sas->notify_port_event(sas_phy,
  245. PORTE_BYTES_DMAED);
  246. }
  247. void mvs_scan_start(struct Scsi_Host *shost)
  248. {
  249. int i, j;
  250. unsigned short core_nr;
  251. struct mvs_info *mvi;
  252. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  253. struct mvs_prv_info *mvs_prv = sha->lldd_ha;
  254. core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
  255. for (j = 0; j < core_nr; j++) {
  256. mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
  257. for (i = 0; i < mvi->chip->n_phy; ++i)
  258. mvs_bytes_dmaed(mvi, i);
  259. }
  260. mvs_prv->scan_finished = 1;
  261. }
  262. int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
  263. {
  264. struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
  265. struct mvs_prv_info *mvs_prv = sha->lldd_ha;
  266. if (mvs_prv->scan_finished == 0)
  267. return 0;
  268. sas_drain_work(sha);
  269. return 1;
  270. }
  271. static int mvs_task_prep_smp(struct mvs_info *mvi,
  272. struct mvs_task_exec_info *tei)
  273. {
  274. int elem, rc, i;
  275. struct sas_ha_struct *sha = mvi->sas;
  276. struct sas_task *task = tei->task;
  277. struct mvs_cmd_hdr *hdr = tei->hdr;
  278. struct domain_device *dev = task->dev;
  279. struct asd_sas_port *sas_port = dev->port;
  280. struct sas_phy *sphy = dev->phy;
  281. struct asd_sas_phy *sas_phy = sha->sas_phy[sphy->number];
  282. struct scatterlist *sg_req, *sg_resp;
  283. u32 req_len, resp_len, tag = tei->tag;
  284. void *buf_tmp;
  285. u8 *buf_oaf;
  286. dma_addr_t buf_tmp_dma;
  287. void *buf_prd;
  288. struct mvs_slot_info *slot = &mvi->slot_info[tag];
  289. u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
  290. /*
  291. * DMA-map SMP request, response buffers
  292. */
  293. sg_req = &task->smp_task.smp_req;
  294. elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
  295. if (!elem)
  296. return -ENOMEM;
  297. req_len = sg_dma_len(sg_req);
  298. sg_resp = &task->smp_task.smp_resp;
  299. elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
  300. if (!elem) {
  301. rc = -ENOMEM;
  302. goto err_out;
  303. }
  304. resp_len = SB_RFB_MAX;
  305. /* must be in dwords */
  306. if ((req_len & 0x3) || (resp_len & 0x3)) {
  307. rc = -EINVAL;
  308. goto err_out_2;
  309. }
  310. /*
  311. * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
  312. */
  313. /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
  314. buf_tmp = slot->buf;
  315. buf_tmp_dma = slot->buf_dma;
  316. hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
  317. /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
  318. buf_oaf = buf_tmp;
  319. hdr->open_frame = cpu_to_le64(buf_tmp_dma);
  320. buf_tmp += MVS_OAF_SZ;
  321. buf_tmp_dma += MVS_OAF_SZ;
  322. /* region 3: PRD table *********************************** */
  323. buf_prd = buf_tmp;
  324. if (tei->n_elem)
  325. hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
  326. else
  327. hdr->prd_tbl = 0;
  328. i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
  329. buf_tmp += i;
  330. buf_tmp_dma += i;
  331. /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
  332. slot->response = buf_tmp;
  333. hdr->status_buf = cpu_to_le64(buf_tmp_dma);
  334. if (mvi->flags & MVF_FLAG_SOC)
  335. hdr->reserved[0] = 0;
  336. /*
  337. * Fill in TX ring and command slot header
  338. */
  339. slot->tx = mvi->tx_prod;
  340. mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
  341. TXQ_MODE_I | tag |
  342. (MVS_PHY_ID << TXQ_PHY_SHIFT));
  343. hdr->flags |= flags;
  344. hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
  345. hdr->tags = cpu_to_le32(tag);
  346. hdr->data_len = 0;
  347. /* generate open address frame hdr (first 12 bytes) */
  348. /* initiator, SMP, ftype 1h */
  349. buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
  350. buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
  351. *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
  352. memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
  353. /* fill in PRD (scatter/gather) table, if any */
  354. MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
  355. return 0;
  356. err_out_2:
  357. dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
  358. PCI_DMA_FROMDEVICE);
  359. err_out:
  360. dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
  361. PCI_DMA_TODEVICE);
  362. return rc;
  363. }
  364. static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
  365. {
  366. struct ata_queued_cmd *qc = task->uldd_task;
  367. if (qc) {
  368. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  369. qc->tf.command == ATA_CMD_FPDMA_READ) {
  370. *tag = qc->tag;
  371. return 1;
  372. }
  373. }
  374. return 0;
  375. }
  376. static int mvs_task_prep_ata(struct mvs_info *mvi,
  377. struct mvs_task_exec_info *tei)
  378. {
  379. struct sas_task *task = tei->task;
  380. struct domain_device *dev = task->dev;
  381. struct mvs_device *mvi_dev = dev->lldd_dev;
  382. struct mvs_cmd_hdr *hdr = tei->hdr;
  383. struct asd_sas_port *sas_port = dev->port;
  384. struct mvs_slot_info *slot;
  385. void *buf_prd;
  386. u32 tag = tei->tag, hdr_tag;
  387. u32 flags, del_q;
  388. void *buf_tmp;
  389. u8 *buf_cmd, *buf_oaf;
  390. dma_addr_t buf_tmp_dma;
  391. u32 i, req_len, resp_len;
  392. const u32 max_resp_len = SB_RFB_MAX;
  393. if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
  394. mv_dprintk("Have not enough regiset for dev %d.\n",
  395. mvi_dev->device_id);
  396. return -EBUSY;
  397. }
  398. slot = &mvi->slot_info[tag];
  399. slot->tx = mvi->tx_prod;
  400. del_q = TXQ_MODE_I | tag |
  401. (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
  402. ((sas_port->phy_mask & TXQ_PHY_MASK) << TXQ_PHY_SHIFT) |
  403. (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
  404. mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
  405. if (task->data_dir == DMA_FROM_DEVICE)
  406. flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
  407. else
  408. flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
  409. if (task->ata_task.use_ncq)
  410. flags |= MCH_FPDMA;
  411. if (dev->sata_dev.class == ATA_DEV_ATAPI) {
  412. if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
  413. flags |= MCH_ATAPI;
  414. }
  415. hdr->flags = cpu_to_le32(flags);
  416. if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
  417. task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
  418. else
  419. hdr_tag = tag;
  420. hdr->tags = cpu_to_le32(hdr_tag);
  421. hdr->data_len = cpu_to_le32(task->total_xfer_len);
  422. /*
  423. * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
  424. */
  425. /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
  426. buf_cmd = buf_tmp = slot->buf;
  427. buf_tmp_dma = slot->buf_dma;
  428. hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
  429. buf_tmp += MVS_ATA_CMD_SZ;
  430. buf_tmp_dma += MVS_ATA_CMD_SZ;
  431. /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
  432. /* used for STP. unused for SATA? */
  433. buf_oaf = buf_tmp;
  434. hdr->open_frame = cpu_to_le64(buf_tmp_dma);
  435. buf_tmp += MVS_OAF_SZ;
  436. buf_tmp_dma += MVS_OAF_SZ;
  437. /* region 3: PRD table ********************************************* */
  438. buf_prd = buf_tmp;
  439. if (tei->n_elem)
  440. hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
  441. else
  442. hdr->prd_tbl = 0;
  443. i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
  444. buf_tmp += i;
  445. buf_tmp_dma += i;
  446. /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
  447. slot->response = buf_tmp;
  448. hdr->status_buf = cpu_to_le64(buf_tmp_dma);
  449. if (mvi->flags & MVF_FLAG_SOC)
  450. hdr->reserved[0] = 0;
  451. req_len = sizeof(struct host_to_dev_fis);
  452. resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
  453. sizeof(struct mvs_err_info) - i;
  454. /* request, response lengths */
  455. resp_len = min(resp_len, max_resp_len);
  456. hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
  457. if (likely(!task->ata_task.device_control_reg_update))
  458. task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
  459. /* fill in command FIS and ATAPI CDB */
  460. memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
  461. if (dev->sata_dev.class == ATA_DEV_ATAPI)
  462. memcpy(buf_cmd + STP_ATAPI_CMD,
  463. task->ata_task.atapi_packet, 16);
  464. /* generate open address frame hdr (first 12 bytes) */
  465. /* initiator, STP, ftype 1h */
  466. buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
  467. buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
  468. *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
  469. memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
  470. /* fill in PRD (scatter/gather) table, if any */
  471. MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
  472. if (task->data_dir == DMA_FROM_DEVICE)
  473. MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
  474. TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
  475. return 0;
  476. }
  477. static int mvs_task_prep_ssp(struct mvs_info *mvi,
  478. struct mvs_task_exec_info *tei, int is_tmf,
  479. struct mvs_tmf_task *tmf)
  480. {
  481. struct sas_task *task = tei->task;
  482. struct mvs_cmd_hdr *hdr = tei->hdr;
  483. struct mvs_port *port = tei->port;
  484. struct domain_device *dev = task->dev;
  485. struct mvs_device *mvi_dev = dev->lldd_dev;
  486. struct asd_sas_port *sas_port = dev->port;
  487. struct mvs_slot_info *slot;
  488. void *buf_prd;
  489. struct ssp_frame_hdr *ssp_hdr;
  490. void *buf_tmp;
  491. u8 *buf_cmd, *buf_oaf, fburst = 0;
  492. dma_addr_t buf_tmp_dma;
  493. u32 flags;
  494. u32 resp_len, req_len, i, tag = tei->tag;
  495. const u32 max_resp_len = SB_RFB_MAX;
  496. u32 phy_mask;
  497. slot = &mvi->slot_info[tag];
  498. phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
  499. sas_port->phy_mask) & TXQ_PHY_MASK;
  500. slot->tx = mvi->tx_prod;
  501. mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
  502. (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
  503. (phy_mask << TXQ_PHY_SHIFT));
  504. flags = MCH_RETRY;
  505. if (task->ssp_task.enable_first_burst) {
  506. flags |= MCH_FBURST;
  507. fburst = (1 << 7);
  508. }
  509. if (is_tmf)
  510. flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
  511. else
  512. flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
  513. hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
  514. hdr->tags = cpu_to_le32(tag);
  515. hdr->data_len = cpu_to_le32(task->total_xfer_len);
  516. /*
  517. * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
  518. */
  519. /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
  520. buf_cmd = buf_tmp = slot->buf;
  521. buf_tmp_dma = slot->buf_dma;
  522. hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
  523. buf_tmp += MVS_SSP_CMD_SZ;
  524. buf_tmp_dma += MVS_SSP_CMD_SZ;
  525. /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
  526. buf_oaf = buf_tmp;
  527. hdr->open_frame = cpu_to_le64(buf_tmp_dma);
  528. buf_tmp += MVS_OAF_SZ;
  529. buf_tmp_dma += MVS_OAF_SZ;
  530. /* region 3: PRD table ********************************************* */
  531. buf_prd = buf_tmp;
  532. if (tei->n_elem)
  533. hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
  534. else
  535. hdr->prd_tbl = 0;
  536. i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
  537. buf_tmp += i;
  538. buf_tmp_dma += i;
  539. /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
  540. slot->response = buf_tmp;
  541. hdr->status_buf = cpu_to_le64(buf_tmp_dma);
  542. if (mvi->flags & MVF_FLAG_SOC)
  543. hdr->reserved[0] = 0;
  544. resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
  545. sizeof(struct mvs_err_info) - i;
  546. resp_len = min(resp_len, max_resp_len);
  547. req_len = sizeof(struct ssp_frame_hdr) + 28;
  548. /* request, response lengths */
  549. hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
  550. /* generate open address frame hdr (first 12 bytes) */
  551. /* initiator, SSP, ftype 1h */
  552. buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
  553. buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
  554. *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
  555. memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
  556. /* fill in SSP frame header (Command Table.SSP frame header) */
  557. ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
  558. if (is_tmf)
  559. ssp_hdr->frame_type = SSP_TASK;
  560. else
  561. ssp_hdr->frame_type = SSP_COMMAND;
  562. memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
  563. HASHED_SAS_ADDR_SIZE);
  564. memcpy(ssp_hdr->hashed_src_addr,
  565. dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
  566. ssp_hdr->tag = cpu_to_be16(tag);
  567. /* fill in IU for TASK and Command Frame */
  568. buf_cmd += sizeof(*ssp_hdr);
  569. memcpy(buf_cmd, &task->ssp_task.LUN, 8);
  570. if (ssp_hdr->frame_type != SSP_TASK) {
  571. buf_cmd[9] = fburst | task->ssp_task.task_attr |
  572. (task->ssp_task.task_prio << 3);
  573. memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
  574. task->ssp_task.cmd->cmd_len);
  575. } else{
  576. buf_cmd[10] = tmf->tmf;
  577. switch (tmf->tmf) {
  578. case TMF_ABORT_TASK:
  579. case TMF_QUERY_TASK:
  580. buf_cmd[12] =
  581. (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
  582. buf_cmd[13] =
  583. tmf->tag_of_task_to_be_managed & 0xff;
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. /* fill in PRD (scatter/gather) table, if any */
  590. MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
  591. return 0;
  592. }
  593. #define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == SAS_PHY_UNUSED)))
  594. static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
  595. struct mvs_tmf_task *tmf, int *pass)
  596. {
  597. struct domain_device *dev = task->dev;
  598. struct mvs_device *mvi_dev = dev->lldd_dev;
  599. struct mvs_task_exec_info tei;
  600. struct mvs_slot_info *slot;
  601. u32 tag = 0xdeadbeef, n_elem = 0;
  602. int rc = 0;
  603. if (!dev->port) {
  604. struct task_status_struct *tsm = &task->task_status;
  605. tsm->resp = SAS_TASK_UNDELIVERED;
  606. tsm->stat = SAS_PHY_DOWN;
  607. /*
  608. * libsas will use dev->port, should
  609. * not call task_done for sata
  610. */
  611. if (dev->dev_type != SAS_SATA_DEV)
  612. task->task_done(task);
  613. return rc;
  614. }
  615. if (DEV_IS_GONE(mvi_dev)) {
  616. if (mvi_dev)
  617. mv_dprintk("device %d not ready.\n",
  618. mvi_dev->device_id);
  619. else
  620. mv_dprintk("device %016llx not ready.\n",
  621. SAS_ADDR(dev->sas_addr));
  622. rc = SAS_PHY_DOWN;
  623. return rc;
  624. }
  625. tei.port = dev->port->lldd_port;
  626. if (tei.port && !tei.port->port_attached && !tmf) {
  627. if (sas_protocol_ata(task->task_proto)) {
  628. struct task_status_struct *ts = &task->task_status;
  629. mv_dprintk("SATA/STP port %d does not attach"
  630. "device.\n", dev->port->id);
  631. ts->resp = SAS_TASK_COMPLETE;
  632. ts->stat = SAS_PHY_DOWN;
  633. task->task_done(task);
  634. } else {
  635. struct task_status_struct *ts = &task->task_status;
  636. mv_dprintk("SAS port %d does not attach"
  637. "device.\n", dev->port->id);
  638. ts->resp = SAS_TASK_UNDELIVERED;
  639. ts->stat = SAS_PHY_DOWN;
  640. task->task_done(task);
  641. }
  642. return rc;
  643. }
  644. if (!sas_protocol_ata(task->task_proto)) {
  645. if (task->num_scatter) {
  646. n_elem = dma_map_sg(mvi->dev,
  647. task->scatter,
  648. task->num_scatter,
  649. task->data_dir);
  650. if (!n_elem) {
  651. rc = -ENOMEM;
  652. goto prep_out;
  653. }
  654. }
  655. } else {
  656. n_elem = task->num_scatter;
  657. }
  658. rc = mvs_tag_alloc(mvi, &tag);
  659. if (rc)
  660. goto err_out;
  661. slot = &mvi->slot_info[tag];
  662. task->lldd_task = NULL;
  663. slot->n_elem = n_elem;
  664. slot->slot_tag = tag;
  665. slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
  666. if (!slot->buf)
  667. goto err_out_tag;
  668. memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
  669. tei.task = task;
  670. tei.hdr = &mvi->slot[tag];
  671. tei.tag = tag;
  672. tei.n_elem = n_elem;
  673. switch (task->task_proto) {
  674. case SAS_PROTOCOL_SMP:
  675. rc = mvs_task_prep_smp(mvi, &tei);
  676. break;
  677. case SAS_PROTOCOL_SSP:
  678. rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
  679. break;
  680. case SAS_PROTOCOL_SATA:
  681. case SAS_PROTOCOL_STP:
  682. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  683. rc = mvs_task_prep_ata(mvi, &tei);
  684. break;
  685. default:
  686. dev_printk(KERN_ERR, mvi->dev,
  687. "unknown sas_task proto: 0x%x\n",
  688. task->task_proto);
  689. rc = -EINVAL;
  690. break;
  691. }
  692. if (rc) {
  693. mv_dprintk("rc is %x\n", rc);
  694. goto err_out_slot_buf;
  695. }
  696. slot->task = task;
  697. slot->port = tei.port;
  698. task->lldd_task = slot;
  699. list_add_tail(&slot->entry, &tei.port->list);
  700. spin_lock(&task->task_state_lock);
  701. task->task_state_flags |= SAS_TASK_AT_INITIATOR;
  702. spin_unlock(&task->task_state_lock);
  703. mvi_dev->running_req++;
  704. ++(*pass);
  705. mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
  706. return rc;
  707. err_out_slot_buf:
  708. pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
  709. err_out_tag:
  710. mvs_tag_free(mvi, tag);
  711. err_out:
  712. dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
  713. if (!sas_protocol_ata(task->task_proto))
  714. if (n_elem)
  715. dma_unmap_sg(mvi->dev, task->scatter, n_elem,
  716. task->data_dir);
  717. prep_out:
  718. return rc;
  719. }
  720. static int mvs_task_exec(struct sas_task *task, gfp_t gfp_flags,
  721. struct completion *completion, int is_tmf,
  722. struct mvs_tmf_task *tmf)
  723. {
  724. struct mvs_info *mvi = NULL;
  725. u32 rc = 0;
  726. u32 pass = 0;
  727. unsigned long flags = 0;
  728. mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
  729. spin_lock_irqsave(&mvi->lock, flags);
  730. rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
  731. if (rc)
  732. dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
  733. if (likely(pass))
  734. MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
  735. (MVS_CHIP_SLOT_SZ - 1));
  736. spin_unlock_irqrestore(&mvi->lock, flags);
  737. return rc;
  738. }
  739. int mvs_queue_command(struct sas_task *task, gfp_t gfp_flags)
  740. {
  741. return mvs_task_exec(task, gfp_flags, NULL, 0, NULL);
  742. }
  743. static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
  744. {
  745. u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
  746. mvs_tag_clear(mvi, slot_idx);
  747. }
  748. static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
  749. struct mvs_slot_info *slot, u32 slot_idx)
  750. {
  751. if (!slot->task)
  752. return;
  753. if (!sas_protocol_ata(task->task_proto))
  754. if (slot->n_elem)
  755. dma_unmap_sg(mvi->dev, task->scatter,
  756. slot->n_elem, task->data_dir);
  757. switch (task->task_proto) {
  758. case SAS_PROTOCOL_SMP:
  759. dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
  760. PCI_DMA_FROMDEVICE);
  761. dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
  762. PCI_DMA_TODEVICE);
  763. break;
  764. case SAS_PROTOCOL_SATA:
  765. case SAS_PROTOCOL_STP:
  766. case SAS_PROTOCOL_SSP:
  767. default:
  768. /* do nothing */
  769. break;
  770. }
  771. if (slot->buf) {
  772. pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
  773. slot->buf = NULL;
  774. }
  775. list_del_init(&slot->entry);
  776. task->lldd_task = NULL;
  777. slot->task = NULL;
  778. slot->port = NULL;
  779. slot->slot_tag = 0xFFFFFFFF;
  780. mvs_slot_free(mvi, slot_idx);
  781. }
  782. static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
  783. {
  784. struct mvs_phy *phy = &mvi->phy[phy_no];
  785. struct mvs_port *port = phy->port;
  786. int j, no;
  787. for_each_phy(port->wide_port_phymap, j, no) {
  788. if (j & 1) {
  789. MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
  790. PHYR_WIDE_PORT);
  791. MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
  792. port->wide_port_phymap);
  793. } else {
  794. MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
  795. PHYR_WIDE_PORT);
  796. MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
  797. 0);
  798. }
  799. }
  800. }
  801. static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
  802. {
  803. u32 tmp;
  804. struct mvs_phy *phy = &mvi->phy[i];
  805. struct mvs_port *port = phy->port;
  806. tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
  807. if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
  808. if (!port)
  809. phy->phy_attached = 1;
  810. return tmp;
  811. }
  812. if (port) {
  813. if (phy->phy_type & PORT_TYPE_SAS) {
  814. port->wide_port_phymap &= ~(1U << i);
  815. if (!port->wide_port_phymap)
  816. port->port_attached = 0;
  817. mvs_update_wideport(mvi, i);
  818. } else if (phy->phy_type & PORT_TYPE_SATA)
  819. port->port_attached = 0;
  820. phy->port = NULL;
  821. phy->phy_attached = 0;
  822. phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
  823. }
  824. return 0;
  825. }
  826. static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
  827. {
  828. u32 *s = (u32 *) buf;
  829. if (!s)
  830. return NULL;
  831. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
  832. s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
  833. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
  834. s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
  835. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
  836. s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
  837. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
  838. s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
  839. if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
  840. s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
  841. return s;
  842. }
  843. static u32 mvs_is_sig_fis_received(u32 irq_status)
  844. {
  845. return irq_status & PHYEV_SIG_FIS;
  846. }
  847. static void mvs_sig_remove_timer(struct mvs_phy *phy)
  848. {
  849. if (phy->timer.function)
  850. del_timer(&phy->timer);
  851. phy->timer.function = NULL;
  852. }
  853. void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
  854. {
  855. struct mvs_phy *phy = &mvi->phy[i];
  856. struct sas_identify_frame *id;
  857. id = (struct sas_identify_frame *)phy->frame_rcvd;
  858. if (get_st) {
  859. phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
  860. phy->phy_status = mvs_is_phy_ready(mvi, i);
  861. }
  862. if (phy->phy_status) {
  863. int oob_done = 0;
  864. struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
  865. oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
  866. MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
  867. if (phy->phy_type & PORT_TYPE_SATA) {
  868. phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
  869. if (mvs_is_sig_fis_received(phy->irq_status)) {
  870. mvs_sig_remove_timer(phy);
  871. phy->phy_attached = 1;
  872. phy->att_dev_sas_addr =
  873. i + mvi->id * mvi->chip->n_phy;
  874. if (oob_done)
  875. sas_phy->oob_mode = SATA_OOB_MODE;
  876. phy->frame_rcvd_size =
  877. sizeof(struct dev_to_host_fis);
  878. mvs_get_d2h_reg(mvi, i, id);
  879. } else {
  880. u32 tmp;
  881. dev_printk(KERN_DEBUG, mvi->dev,
  882. "Phy%d : No sig fis\n", i);
  883. tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
  884. MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
  885. tmp | PHYEV_SIG_FIS);
  886. phy->phy_attached = 0;
  887. phy->phy_type &= ~PORT_TYPE_SATA;
  888. goto out_done;
  889. }
  890. } else if (phy->phy_type & PORT_TYPE_SAS
  891. || phy->att_dev_info & PORT_SSP_INIT_MASK) {
  892. phy->phy_attached = 1;
  893. phy->identify.device_type =
  894. phy->att_dev_info & PORT_DEV_TYPE_MASK;
  895. if (phy->identify.device_type == SAS_END_DEVICE)
  896. phy->identify.target_port_protocols =
  897. SAS_PROTOCOL_SSP;
  898. else if (phy->identify.device_type != SAS_PHY_UNUSED)
  899. phy->identify.target_port_protocols =
  900. SAS_PROTOCOL_SMP;
  901. if (oob_done)
  902. sas_phy->oob_mode = SAS_OOB_MODE;
  903. phy->frame_rcvd_size =
  904. sizeof(struct sas_identify_frame);
  905. }
  906. memcpy(sas_phy->attached_sas_addr,
  907. &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
  908. if (MVS_CHIP_DISP->phy_work_around)
  909. MVS_CHIP_DISP->phy_work_around(mvi, i);
  910. }
  911. mv_dprintk("phy %d attach dev info is %x\n",
  912. i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
  913. mv_dprintk("phy %d attach sas addr is %llx\n",
  914. i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
  915. out_done:
  916. if (get_st)
  917. MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
  918. }
  919. static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
  920. {
  921. struct sas_ha_struct *sas_ha = sas_phy->ha;
  922. struct mvs_info *mvi = NULL; int i = 0, hi;
  923. struct mvs_phy *phy = sas_phy->lldd_phy;
  924. struct asd_sas_port *sas_port = sas_phy->port;
  925. struct mvs_port *port;
  926. unsigned long flags = 0;
  927. if (!sas_port)
  928. return;
  929. while (sas_ha->sas_phy[i]) {
  930. if (sas_ha->sas_phy[i] == sas_phy)
  931. break;
  932. i++;
  933. }
  934. hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
  935. mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
  936. if (i >= mvi->chip->n_phy)
  937. port = &mvi->port[i - mvi->chip->n_phy];
  938. else
  939. port = &mvi->port[i];
  940. if (lock)
  941. spin_lock_irqsave(&mvi->lock, flags);
  942. port->port_attached = 1;
  943. phy->port = port;
  944. sas_port->lldd_port = port;
  945. if (phy->phy_type & PORT_TYPE_SAS) {
  946. port->wide_port_phymap = sas_port->phy_mask;
  947. mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
  948. mvs_update_wideport(mvi, sas_phy->id);
  949. /* direct attached SAS device */
  950. if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
  951. MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
  952. MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x04);
  953. }
  954. }
  955. if (lock)
  956. spin_unlock_irqrestore(&mvi->lock, flags);
  957. }
  958. static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
  959. {
  960. struct domain_device *dev;
  961. struct mvs_phy *phy = sas_phy->lldd_phy;
  962. struct mvs_info *mvi = phy->mvi;
  963. struct asd_sas_port *port = sas_phy->port;
  964. int phy_no = 0;
  965. while (phy != &mvi->phy[phy_no]) {
  966. phy_no++;
  967. if (phy_no >= MVS_MAX_PHYS)
  968. return;
  969. }
  970. list_for_each_entry(dev, &port->dev_list, dev_list_node)
  971. mvs_do_release_task(phy->mvi, phy_no, dev);
  972. }
  973. void mvs_port_formed(struct asd_sas_phy *sas_phy)
  974. {
  975. mvs_port_notify_formed(sas_phy, 1);
  976. }
  977. void mvs_port_deformed(struct asd_sas_phy *sas_phy)
  978. {
  979. mvs_port_notify_deformed(sas_phy, 1);
  980. }
  981. struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
  982. {
  983. u32 dev;
  984. for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
  985. if (mvi->devices[dev].dev_type == SAS_PHY_UNUSED) {
  986. mvi->devices[dev].device_id = dev;
  987. return &mvi->devices[dev];
  988. }
  989. }
  990. if (dev == MVS_MAX_DEVICES)
  991. mv_printk("max support %d devices, ignore ..\n",
  992. MVS_MAX_DEVICES);
  993. return NULL;
  994. }
  995. void mvs_free_dev(struct mvs_device *mvi_dev)
  996. {
  997. u32 id = mvi_dev->device_id;
  998. memset(mvi_dev, 0, sizeof(*mvi_dev));
  999. mvi_dev->device_id = id;
  1000. mvi_dev->dev_type = SAS_PHY_UNUSED;
  1001. mvi_dev->dev_status = MVS_DEV_NORMAL;
  1002. mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
  1003. }
  1004. int mvs_dev_found_notify(struct domain_device *dev, int lock)
  1005. {
  1006. unsigned long flags = 0;
  1007. int res = 0;
  1008. struct mvs_info *mvi = NULL;
  1009. struct domain_device *parent_dev = dev->parent;
  1010. struct mvs_device *mvi_device;
  1011. mvi = mvs_find_dev_mvi(dev);
  1012. if (lock)
  1013. spin_lock_irqsave(&mvi->lock, flags);
  1014. mvi_device = mvs_alloc_dev(mvi);
  1015. if (!mvi_device) {
  1016. res = -1;
  1017. goto found_out;
  1018. }
  1019. dev->lldd_dev = mvi_device;
  1020. mvi_device->dev_status = MVS_DEV_NORMAL;
  1021. mvi_device->dev_type = dev->dev_type;
  1022. mvi_device->mvi_info = mvi;
  1023. mvi_device->sas_device = dev;
  1024. if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
  1025. int phy_id;
  1026. u8 phy_num = parent_dev->ex_dev.num_phys;
  1027. struct ex_phy *phy;
  1028. for (phy_id = 0; phy_id < phy_num; phy_id++) {
  1029. phy = &parent_dev->ex_dev.ex_phy[phy_id];
  1030. if (SAS_ADDR(phy->attached_sas_addr) ==
  1031. SAS_ADDR(dev->sas_addr)) {
  1032. mvi_device->attached_phy = phy_id;
  1033. break;
  1034. }
  1035. }
  1036. if (phy_id == phy_num) {
  1037. mv_printk("Error: no attached dev:%016llx"
  1038. "at ex:%016llx.\n",
  1039. SAS_ADDR(dev->sas_addr),
  1040. SAS_ADDR(parent_dev->sas_addr));
  1041. res = -1;
  1042. }
  1043. }
  1044. found_out:
  1045. if (lock)
  1046. spin_unlock_irqrestore(&mvi->lock, flags);
  1047. return res;
  1048. }
  1049. int mvs_dev_found(struct domain_device *dev)
  1050. {
  1051. return mvs_dev_found_notify(dev, 1);
  1052. }
  1053. void mvs_dev_gone_notify(struct domain_device *dev)
  1054. {
  1055. unsigned long flags = 0;
  1056. struct mvs_device *mvi_dev = dev->lldd_dev;
  1057. struct mvs_info *mvi;
  1058. if (!mvi_dev) {
  1059. mv_dprintk("found dev has gone.\n");
  1060. return;
  1061. }
  1062. mvi = mvi_dev->mvi_info;
  1063. spin_lock_irqsave(&mvi->lock, flags);
  1064. mv_dprintk("found dev[%d:%x] is gone.\n",
  1065. mvi_dev->device_id, mvi_dev->dev_type);
  1066. mvs_release_task(mvi, dev);
  1067. mvs_free_reg_set(mvi, mvi_dev);
  1068. mvs_free_dev(mvi_dev);
  1069. dev->lldd_dev = NULL;
  1070. mvi_dev->sas_device = NULL;
  1071. spin_unlock_irqrestore(&mvi->lock, flags);
  1072. }
  1073. void mvs_dev_gone(struct domain_device *dev)
  1074. {
  1075. mvs_dev_gone_notify(dev);
  1076. }
  1077. static void mvs_task_done(struct sas_task *task)
  1078. {
  1079. if (!del_timer(&task->slow_task->timer))
  1080. return;
  1081. complete(&task->slow_task->completion);
  1082. }
  1083. static void mvs_tmf_timedout(unsigned long data)
  1084. {
  1085. struct sas_task *task = (struct sas_task *)data;
  1086. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1087. complete(&task->slow_task->completion);
  1088. }
  1089. #define MVS_TASK_TIMEOUT 20
  1090. static int mvs_exec_internal_tmf_task(struct domain_device *dev,
  1091. void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
  1092. {
  1093. int res, retry;
  1094. struct sas_task *task = NULL;
  1095. for (retry = 0; retry < 3; retry++) {
  1096. task = sas_alloc_slow_task(GFP_KERNEL);
  1097. if (!task)
  1098. return -ENOMEM;
  1099. task->dev = dev;
  1100. task->task_proto = dev->tproto;
  1101. memcpy(&task->ssp_task, parameter, para_len);
  1102. task->task_done = mvs_task_done;
  1103. task->slow_task->timer.data = (unsigned long) task;
  1104. task->slow_task->timer.function = mvs_tmf_timedout;
  1105. task->slow_task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
  1106. add_timer(&task->slow_task->timer);
  1107. res = mvs_task_exec(task, GFP_KERNEL, NULL, 1, tmf);
  1108. if (res) {
  1109. del_timer(&task->slow_task->timer);
  1110. mv_printk("executing internal task failed:%d\n", res);
  1111. goto ex_err;
  1112. }
  1113. wait_for_completion(&task->slow_task->completion);
  1114. res = TMF_RESP_FUNC_FAILED;
  1115. /* Even TMF timed out, return direct. */
  1116. if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
  1117. if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
  1118. mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
  1119. goto ex_err;
  1120. }
  1121. }
  1122. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1123. task->task_status.stat == SAM_STAT_GOOD) {
  1124. res = TMF_RESP_FUNC_COMPLETE;
  1125. break;
  1126. }
  1127. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1128. task->task_status.stat == SAS_DATA_UNDERRUN) {
  1129. /* no error, but return the number of bytes of
  1130. * underrun */
  1131. res = task->task_status.residual;
  1132. break;
  1133. }
  1134. if (task->task_status.resp == SAS_TASK_COMPLETE &&
  1135. task->task_status.stat == SAS_DATA_OVERRUN) {
  1136. mv_dprintk("blocked task error.\n");
  1137. res = -EMSGSIZE;
  1138. break;
  1139. } else {
  1140. mv_dprintk(" task to dev %016llx response: 0x%x "
  1141. "status 0x%x\n",
  1142. SAS_ADDR(dev->sas_addr),
  1143. task->task_status.resp,
  1144. task->task_status.stat);
  1145. sas_free_task(task);
  1146. task = NULL;
  1147. }
  1148. }
  1149. ex_err:
  1150. BUG_ON(retry == 3 && task != NULL);
  1151. sas_free_task(task);
  1152. return res;
  1153. }
  1154. static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
  1155. u8 *lun, struct mvs_tmf_task *tmf)
  1156. {
  1157. struct sas_ssp_task ssp_task;
  1158. if (!(dev->tproto & SAS_PROTOCOL_SSP))
  1159. return TMF_RESP_FUNC_ESUPP;
  1160. memcpy(ssp_task.LUN, lun, 8);
  1161. return mvs_exec_internal_tmf_task(dev, &ssp_task,
  1162. sizeof(ssp_task), tmf);
  1163. }
  1164. /* Standard mandates link reset for ATA (type 0)
  1165. and hard reset for SSP (type 1) , only for RECOVERY */
  1166. static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
  1167. {
  1168. int rc;
  1169. struct sas_phy *phy = sas_get_local_phy(dev);
  1170. int reset_type = (dev->dev_type == SAS_SATA_DEV ||
  1171. (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
  1172. rc = sas_phy_reset(phy, reset_type);
  1173. sas_put_local_phy(phy);
  1174. msleep(2000);
  1175. return rc;
  1176. }
  1177. /* mandatory SAM-3 */
  1178. int mvs_lu_reset(struct domain_device *dev, u8 *lun)
  1179. {
  1180. unsigned long flags;
  1181. int rc = TMF_RESP_FUNC_FAILED;
  1182. struct mvs_tmf_task tmf_task;
  1183. struct mvs_device * mvi_dev = dev->lldd_dev;
  1184. struct mvs_info *mvi = mvi_dev->mvi_info;
  1185. tmf_task.tmf = TMF_LU_RESET;
  1186. mvi_dev->dev_status = MVS_DEV_EH;
  1187. rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
  1188. if (rc == TMF_RESP_FUNC_COMPLETE) {
  1189. spin_lock_irqsave(&mvi->lock, flags);
  1190. mvs_release_task(mvi, dev);
  1191. spin_unlock_irqrestore(&mvi->lock, flags);
  1192. }
  1193. /* If failed, fall-through I_T_Nexus reset */
  1194. mv_printk("%s for device[%x]:rc= %d\n", __func__,
  1195. mvi_dev->device_id, rc);
  1196. return rc;
  1197. }
  1198. int mvs_I_T_nexus_reset(struct domain_device *dev)
  1199. {
  1200. unsigned long flags;
  1201. int rc = TMF_RESP_FUNC_FAILED;
  1202. struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
  1203. struct mvs_info *mvi = mvi_dev->mvi_info;
  1204. if (mvi_dev->dev_status != MVS_DEV_EH)
  1205. return TMF_RESP_FUNC_COMPLETE;
  1206. else
  1207. mvi_dev->dev_status = MVS_DEV_NORMAL;
  1208. rc = mvs_debug_I_T_nexus_reset(dev);
  1209. mv_printk("%s for device[%x]:rc= %d\n",
  1210. __func__, mvi_dev->device_id, rc);
  1211. spin_lock_irqsave(&mvi->lock, flags);
  1212. mvs_release_task(mvi, dev);
  1213. spin_unlock_irqrestore(&mvi->lock, flags);
  1214. return rc;
  1215. }
  1216. /* optional SAM-3 */
  1217. int mvs_query_task(struct sas_task *task)
  1218. {
  1219. u32 tag;
  1220. struct scsi_lun lun;
  1221. struct mvs_tmf_task tmf_task;
  1222. int rc = TMF_RESP_FUNC_FAILED;
  1223. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  1224. struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
  1225. struct domain_device *dev = task->dev;
  1226. struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
  1227. struct mvs_info *mvi = mvi_dev->mvi_info;
  1228. int_to_scsilun(cmnd->device->lun, &lun);
  1229. rc = mvs_find_tag(mvi, task, &tag);
  1230. if (rc == 0) {
  1231. rc = TMF_RESP_FUNC_FAILED;
  1232. return rc;
  1233. }
  1234. tmf_task.tmf = TMF_QUERY_TASK;
  1235. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  1236. rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1237. switch (rc) {
  1238. /* The task is still in Lun, release it then */
  1239. case TMF_RESP_FUNC_SUCC:
  1240. /* The task is not in Lun or failed, reset the phy */
  1241. case TMF_RESP_FUNC_FAILED:
  1242. case TMF_RESP_FUNC_COMPLETE:
  1243. break;
  1244. }
  1245. }
  1246. mv_printk("%s:rc= %d\n", __func__, rc);
  1247. return rc;
  1248. }
  1249. /* mandatory SAM-3, still need free task/slot info */
  1250. int mvs_abort_task(struct sas_task *task)
  1251. {
  1252. struct scsi_lun lun;
  1253. struct mvs_tmf_task tmf_task;
  1254. struct domain_device *dev = task->dev;
  1255. struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
  1256. struct mvs_info *mvi;
  1257. int rc = TMF_RESP_FUNC_FAILED;
  1258. unsigned long flags;
  1259. u32 tag;
  1260. if (!mvi_dev) {
  1261. mv_printk("Device has removed\n");
  1262. return TMF_RESP_FUNC_FAILED;
  1263. }
  1264. mvi = mvi_dev->mvi_info;
  1265. spin_lock_irqsave(&task->task_state_lock, flags);
  1266. if (task->task_state_flags & SAS_TASK_STATE_DONE) {
  1267. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1268. rc = TMF_RESP_FUNC_COMPLETE;
  1269. goto out;
  1270. }
  1271. spin_unlock_irqrestore(&task->task_state_lock, flags);
  1272. mvi_dev->dev_status = MVS_DEV_EH;
  1273. if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
  1274. struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
  1275. int_to_scsilun(cmnd->device->lun, &lun);
  1276. rc = mvs_find_tag(mvi, task, &tag);
  1277. if (rc == 0) {
  1278. mv_printk("No such tag in %s\n", __func__);
  1279. rc = TMF_RESP_FUNC_FAILED;
  1280. return rc;
  1281. }
  1282. tmf_task.tmf = TMF_ABORT_TASK;
  1283. tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
  1284. rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
  1285. /* if successful, clear the task and callback forwards.*/
  1286. if (rc == TMF_RESP_FUNC_COMPLETE) {
  1287. u32 slot_no;
  1288. struct mvs_slot_info *slot;
  1289. if (task->lldd_task) {
  1290. slot = task->lldd_task;
  1291. slot_no = (u32) (slot - mvi->slot_info);
  1292. spin_lock_irqsave(&mvi->lock, flags);
  1293. mvs_slot_complete(mvi, slot_no, 1);
  1294. spin_unlock_irqrestore(&mvi->lock, flags);
  1295. }
  1296. }
  1297. } else if (task->task_proto & SAS_PROTOCOL_SATA ||
  1298. task->task_proto & SAS_PROTOCOL_STP) {
  1299. if (SAS_SATA_DEV == dev->dev_type) {
  1300. struct mvs_slot_info *slot = task->lldd_task;
  1301. u32 slot_idx = (u32)(slot - mvi->slot_info);
  1302. mv_dprintk("mvs_abort_task() mvi=%p task=%p "
  1303. "slot=%p slot_idx=x%x\n",
  1304. mvi, task, slot, slot_idx);
  1305. task->task_state_flags |= SAS_TASK_STATE_ABORTED;
  1306. mvs_slot_task_free(mvi, task, slot, slot_idx);
  1307. rc = TMF_RESP_FUNC_COMPLETE;
  1308. goto out;
  1309. }
  1310. }
  1311. out:
  1312. if (rc != TMF_RESP_FUNC_COMPLETE)
  1313. mv_printk("%s:rc= %d\n", __func__, rc);
  1314. return rc;
  1315. }
  1316. int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
  1317. {
  1318. int rc = TMF_RESP_FUNC_FAILED;
  1319. struct mvs_tmf_task tmf_task;
  1320. tmf_task.tmf = TMF_ABORT_TASK_SET;
  1321. rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
  1322. return rc;
  1323. }
  1324. int mvs_clear_aca(struct domain_device *dev, u8 *lun)
  1325. {
  1326. int rc = TMF_RESP_FUNC_FAILED;
  1327. struct mvs_tmf_task tmf_task;
  1328. tmf_task.tmf = TMF_CLEAR_ACA;
  1329. rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
  1330. return rc;
  1331. }
  1332. int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
  1333. {
  1334. int rc = TMF_RESP_FUNC_FAILED;
  1335. struct mvs_tmf_task tmf_task;
  1336. tmf_task.tmf = TMF_CLEAR_TASK_SET;
  1337. rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
  1338. return rc;
  1339. }
  1340. static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
  1341. u32 slot_idx, int err)
  1342. {
  1343. struct mvs_device *mvi_dev = task->dev->lldd_dev;
  1344. struct task_status_struct *tstat = &task->task_status;
  1345. struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
  1346. int stat = SAM_STAT_GOOD;
  1347. resp->frame_len = sizeof(struct dev_to_host_fis);
  1348. memcpy(&resp->ending_fis[0],
  1349. SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
  1350. sizeof(struct dev_to_host_fis));
  1351. tstat->buf_valid_size = sizeof(*resp);
  1352. if (unlikely(err)) {
  1353. if (unlikely(err & CMD_ISS_STPD))
  1354. stat = SAS_OPEN_REJECT;
  1355. else
  1356. stat = SAS_PROTO_RESPONSE;
  1357. }
  1358. return stat;
  1359. }
  1360. void mvs_set_sense(u8 *buffer, int len, int d_sense,
  1361. int key, int asc, int ascq)
  1362. {
  1363. memset(buffer, 0, len);
  1364. if (d_sense) {
  1365. /* Descriptor format */
  1366. if (len < 4) {
  1367. mv_printk("Length %d of sense buffer too small to "
  1368. "fit sense %x:%x:%x", len, key, asc, ascq);
  1369. }
  1370. buffer[0] = 0x72; /* Response Code */
  1371. if (len > 1)
  1372. buffer[1] = key; /* Sense Key */
  1373. if (len > 2)
  1374. buffer[2] = asc; /* ASC */
  1375. if (len > 3)
  1376. buffer[3] = ascq; /* ASCQ */
  1377. } else {
  1378. if (len < 14) {
  1379. mv_printk("Length %d of sense buffer too small to "
  1380. "fit sense %x:%x:%x", len, key, asc, ascq);
  1381. }
  1382. buffer[0] = 0x70; /* Response Code */
  1383. if (len > 2)
  1384. buffer[2] = key; /* Sense Key */
  1385. if (len > 7)
  1386. buffer[7] = 0x0a; /* Additional Sense Length */
  1387. if (len > 12)
  1388. buffer[12] = asc; /* ASC */
  1389. if (len > 13)
  1390. buffer[13] = ascq; /* ASCQ */
  1391. }
  1392. return;
  1393. }
  1394. void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
  1395. u8 key, u8 asc, u8 asc_q)
  1396. {
  1397. iu->datapres = 2;
  1398. iu->response_data_len = 0;
  1399. iu->sense_data_len = 17;
  1400. iu->status = 02;
  1401. mvs_set_sense(iu->sense_data, 17, 0,
  1402. key, asc, asc_q);
  1403. }
  1404. static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
  1405. u32 slot_idx)
  1406. {
  1407. struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
  1408. int stat;
  1409. u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
  1410. u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
  1411. u32 tfs = 0;
  1412. enum mvs_port_type type = PORT_TYPE_SAS;
  1413. if (err_dw0 & CMD_ISS_STPD)
  1414. MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
  1415. MVS_CHIP_DISP->command_active(mvi, slot_idx);
  1416. stat = SAM_STAT_CHECK_CONDITION;
  1417. switch (task->task_proto) {
  1418. case SAS_PROTOCOL_SSP:
  1419. {
  1420. stat = SAS_ABORTED_TASK;
  1421. if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
  1422. struct ssp_response_iu *iu = slot->response +
  1423. sizeof(struct mvs_err_info);
  1424. mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
  1425. sas_ssp_task_response(mvi->dev, task, iu);
  1426. stat = SAM_STAT_CHECK_CONDITION;
  1427. }
  1428. if (err_dw1 & bit(31))
  1429. mv_printk("reuse same slot, retry command.\n");
  1430. break;
  1431. }
  1432. case SAS_PROTOCOL_SMP:
  1433. stat = SAM_STAT_CHECK_CONDITION;
  1434. break;
  1435. case SAS_PROTOCOL_SATA:
  1436. case SAS_PROTOCOL_STP:
  1437. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
  1438. {
  1439. task->ata_task.use_ncq = 0;
  1440. stat = SAS_PROTO_RESPONSE;
  1441. mvs_sata_done(mvi, task, slot_idx, err_dw0);
  1442. }
  1443. break;
  1444. default:
  1445. break;
  1446. }
  1447. return stat;
  1448. }
  1449. int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
  1450. {
  1451. u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
  1452. struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
  1453. struct sas_task *task = slot->task;
  1454. struct mvs_device *mvi_dev = NULL;
  1455. struct task_status_struct *tstat;
  1456. struct domain_device *dev;
  1457. u32 aborted;
  1458. void *to;
  1459. enum exec_status sts;
  1460. if (unlikely(!task || !task->lldd_task || !task->dev))
  1461. return -1;
  1462. tstat = &task->task_status;
  1463. dev = task->dev;
  1464. mvi_dev = dev->lldd_dev;
  1465. spin_lock(&task->task_state_lock);
  1466. task->task_state_flags &=
  1467. ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
  1468. task->task_state_flags |= SAS_TASK_STATE_DONE;
  1469. /* race condition*/
  1470. aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
  1471. spin_unlock(&task->task_state_lock);
  1472. memset(tstat, 0, sizeof(*tstat));
  1473. tstat->resp = SAS_TASK_COMPLETE;
  1474. if (unlikely(aborted)) {
  1475. tstat->stat = SAS_ABORTED_TASK;
  1476. if (mvi_dev && mvi_dev->running_req)
  1477. mvi_dev->running_req--;
  1478. if (sas_protocol_ata(task->task_proto))
  1479. mvs_free_reg_set(mvi, mvi_dev);
  1480. mvs_slot_task_free(mvi, task, slot, slot_idx);
  1481. return -1;
  1482. }
  1483. /* when no device attaching, go ahead and complete by error handling*/
  1484. if (unlikely(!mvi_dev || flags)) {
  1485. if (!mvi_dev)
  1486. mv_dprintk("port has not device.\n");
  1487. tstat->stat = SAS_PHY_DOWN;
  1488. goto out;
  1489. }
  1490. /*
  1491. * error info record present; slot->response is 32 bit aligned but may
  1492. * not be 64 bit aligned, so check for zero in two 32 bit reads
  1493. */
  1494. if (unlikely((rx_desc & RXQ_ERR)
  1495. && (*((u32 *)slot->response)
  1496. || *(((u32 *)slot->response) + 1)))) {
  1497. mv_dprintk("port %d slot %d rx_desc %X has error info"
  1498. "%016llX.\n", slot->port->sas_port.id, slot_idx,
  1499. rx_desc, get_unaligned_le64(slot->response));
  1500. tstat->stat = mvs_slot_err(mvi, task, slot_idx);
  1501. tstat->resp = SAS_TASK_COMPLETE;
  1502. goto out;
  1503. }
  1504. switch (task->task_proto) {
  1505. case SAS_PROTOCOL_SSP:
  1506. /* hw says status == 0, datapres == 0 */
  1507. if (rx_desc & RXQ_GOOD) {
  1508. tstat->stat = SAM_STAT_GOOD;
  1509. tstat->resp = SAS_TASK_COMPLETE;
  1510. }
  1511. /* response frame present */
  1512. else if (rx_desc & RXQ_RSP) {
  1513. struct ssp_response_iu *iu = slot->response +
  1514. sizeof(struct mvs_err_info);
  1515. sas_ssp_task_response(mvi->dev, task, iu);
  1516. } else
  1517. tstat->stat = SAM_STAT_CHECK_CONDITION;
  1518. break;
  1519. case SAS_PROTOCOL_SMP: {
  1520. struct scatterlist *sg_resp = &task->smp_task.smp_resp;
  1521. tstat->stat = SAM_STAT_GOOD;
  1522. to = kmap_atomic(sg_page(sg_resp));
  1523. memcpy(to + sg_resp->offset,
  1524. slot->response + sizeof(struct mvs_err_info),
  1525. sg_dma_len(sg_resp));
  1526. kunmap_atomic(to);
  1527. break;
  1528. }
  1529. case SAS_PROTOCOL_SATA:
  1530. case SAS_PROTOCOL_STP:
  1531. case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
  1532. tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
  1533. break;
  1534. }
  1535. default:
  1536. tstat->stat = SAM_STAT_CHECK_CONDITION;
  1537. break;
  1538. }
  1539. if (!slot->port->port_attached) {
  1540. mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
  1541. tstat->stat = SAS_PHY_DOWN;
  1542. }
  1543. out:
  1544. if (mvi_dev && mvi_dev->running_req) {
  1545. mvi_dev->running_req--;
  1546. if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
  1547. mvs_free_reg_set(mvi, mvi_dev);
  1548. }
  1549. mvs_slot_task_free(mvi, task, slot, slot_idx);
  1550. sts = tstat->stat;
  1551. spin_unlock(&mvi->lock);
  1552. if (task->task_done)
  1553. task->task_done(task);
  1554. spin_lock(&mvi->lock);
  1555. return sts;
  1556. }
  1557. void mvs_do_release_task(struct mvs_info *mvi,
  1558. int phy_no, struct domain_device *dev)
  1559. {
  1560. u32 slot_idx;
  1561. struct mvs_phy *phy;
  1562. struct mvs_port *port;
  1563. struct mvs_slot_info *slot, *slot2;
  1564. phy = &mvi->phy[phy_no];
  1565. port = phy->port;
  1566. if (!port)
  1567. return;
  1568. /* clean cmpl queue in case request is already finished */
  1569. mvs_int_rx(mvi, false);
  1570. list_for_each_entry_safe(slot, slot2, &port->list, entry) {
  1571. struct sas_task *task;
  1572. slot_idx = (u32) (slot - mvi->slot_info);
  1573. task = slot->task;
  1574. if (dev && task->dev != dev)
  1575. continue;
  1576. mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
  1577. slot_idx, slot->slot_tag, task);
  1578. MVS_CHIP_DISP->command_active(mvi, slot_idx);
  1579. mvs_slot_complete(mvi, slot_idx, 1);
  1580. }
  1581. }
  1582. void mvs_release_task(struct mvs_info *mvi,
  1583. struct domain_device *dev)
  1584. {
  1585. int i, phyno[WIDE_PORT_MAX_PHY], num;
  1586. num = mvs_find_dev_phyno(dev, phyno);
  1587. for (i = 0; i < num; i++)
  1588. mvs_do_release_task(mvi, phyno[i], dev);
  1589. }
  1590. static void mvs_phy_disconnected(struct mvs_phy *phy)
  1591. {
  1592. phy->phy_attached = 0;
  1593. phy->att_dev_info = 0;
  1594. phy->att_dev_sas_addr = 0;
  1595. }
  1596. static void mvs_work_queue(struct work_struct *work)
  1597. {
  1598. struct delayed_work *dw = container_of(work, struct delayed_work, work);
  1599. struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
  1600. struct mvs_info *mvi = mwq->mvi;
  1601. unsigned long flags;
  1602. u32 phy_no = (unsigned long) mwq->data;
  1603. struct sas_ha_struct *sas_ha = mvi->sas;
  1604. struct mvs_phy *phy = &mvi->phy[phy_no];
  1605. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  1606. spin_lock_irqsave(&mvi->lock, flags);
  1607. if (mwq->handler & PHY_PLUG_EVENT) {
  1608. if (phy->phy_event & PHY_PLUG_OUT) {
  1609. u32 tmp;
  1610. struct sas_identify_frame *id;
  1611. id = (struct sas_identify_frame *)phy->frame_rcvd;
  1612. tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
  1613. phy->phy_event &= ~PHY_PLUG_OUT;
  1614. if (!(tmp & PHY_READY_MASK)) {
  1615. sas_phy_disconnected(sas_phy);
  1616. mvs_phy_disconnected(phy);
  1617. sas_ha->notify_phy_event(sas_phy,
  1618. PHYE_LOSS_OF_SIGNAL);
  1619. mv_dprintk("phy%d Removed Device\n", phy_no);
  1620. } else {
  1621. MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
  1622. mvs_update_phyinfo(mvi, phy_no, 1);
  1623. mvs_bytes_dmaed(mvi, phy_no);
  1624. mvs_port_notify_formed(sas_phy, 0);
  1625. mv_dprintk("phy%d Attached Device\n", phy_no);
  1626. }
  1627. }
  1628. } else if (mwq->handler & EXP_BRCT_CHG) {
  1629. phy->phy_event &= ~EXP_BRCT_CHG;
  1630. sas_ha->notify_port_event(sas_phy,
  1631. PORTE_BROADCAST_RCVD);
  1632. mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
  1633. }
  1634. list_del(&mwq->entry);
  1635. spin_unlock_irqrestore(&mvi->lock, flags);
  1636. kfree(mwq);
  1637. }
  1638. static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
  1639. {
  1640. struct mvs_wq *mwq;
  1641. int ret = 0;
  1642. mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
  1643. if (mwq) {
  1644. mwq->mvi = mvi;
  1645. mwq->data = data;
  1646. mwq->handler = handler;
  1647. MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
  1648. list_add_tail(&mwq->entry, &mvi->wq_list);
  1649. schedule_delayed_work(&mwq->work_q, HZ * 2);
  1650. } else
  1651. ret = -ENOMEM;
  1652. return ret;
  1653. }
  1654. static void mvs_sig_time_out(unsigned long tphy)
  1655. {
  1656. struct mvs_phy *phy = (struct mvs_phy *)tphy;
  1657. struct mvs_info *mvi = phy->mvi;
  1658. u8 phy_no;
  1659. for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
  1660. if (&mvi->phy[phy_no] == phy) {
  1661. mv_dprintk("Get signature time out, reset phy %d\n",
  1662. phy_no+mvi->id*mvi->chip->n_phy);
  1663. MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
  1664. }
  1665. }
  1666. }
  1667. void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
  1668. {
  1669. u32 tmp;
  1670. struct mvs_phy *phy = &mvi->phy[phy_no];
  1671. phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
  1672. MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
  1673. mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
  1674. MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
  1675. mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
  1676. phy->irq_status);
  1677. /*
  1678. * events is port event now ,
  1679. * we need check the interrupt status which belongs to per port.
  1680. */
  1681. if (phy->irq_status & PHYEV_DCDR_ERR) {
  1682. mv_dprintk("phy %d STP decoding error.\n",
  1683. phy_no + mvi->id*mvi->chip->n_phy);
  1684. }
  1685. if (phy->irq_status & PHYEV_POOF) {
  1686. mdelay(500);
  1687. if (!(phy->phy_event & PHY_PLUG_OUT)) {
  1688. int dev_sata = phy->phy_type & PORT_TYPE_SATA;
  1689. int ready;
  1690. mvs_do_release_task(mvi, phy_no, NULL);
  1691. phy->phy_event |= PHY_PLUG_OUT;
  1692. MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
  1693. mvs_handle_event(mvi,
  1694. (void *)(unsigned long)phy_no,
  1695. PHY_PLUG_EVENT);
  1696. ready = mvs_is_phy_ready(mvi, phy_no);
  1697. if (ready || dev_sata) {
  1698. if (MVS_CHIP_DISP->stp_reset)
  1699. MVS_CHIP_DISP->stp_reset(mvi,
  1700. phy_no);
  1701. else
  1702. MVS_CHIP_DISP->phy_reset(mvi,
  1703. phy_no, MVS_SOFT_RESET);
  1704. return;
  1705. }
  1706. }
  1707. }
  1708. if (phy->irq_status & PHYEV_COMWAKE) {
  1709. tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
  1710. MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
  1711. tmp | PHYEV_SIG_FIS);
  1712. if (phy->timer.function == NULL) {
  1713. phy->timer.data = (unsigned long)phy;
  1714. phy->timer.function = mvs_sig_time_out;
  1715. phy->timer.expires = jiffies + 5*HZ;
  1716. add_timer(&phy->timer);
  1717. }
  1718. }
  1719. if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
  1720. phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
  1721. mv_dprintk("notify plug in on phy[%d]\n", phy_no);
  1722. if (phy->phy_status) {
  1723. mdelay(10);
  1724. MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
  1725. if (phy->phy_type & PORT_TYPE_SATA) {
  1726. tmp = MVS_CHIP_DISP->read_port_irq_mask(
  1727. mvi, phy_no);
  1728. tmp &= ~PHYEV_SIG_FIS;
  1729. MVS_CHIP_DISP->write_port_irq_mask(mvi,
  1730. phy_no, tmp);
  1731. }
  1732. mvs_update_phyinfo(mvi, phy_no, 0);
  1733. if (phy->phy_type & PORT_TYPE_SAS) {
  1734. MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
  1735. mdelay(10);
  1736. }
  1737. mvs_bytes_dmaed(mvi, phy_no);
  1738. /* whether driver is going to handle hot plug */
  1739. if (phy->phy_event & PHY_PLUG_OUT) {
  1740. mvs_port_notify_formed(&phy->sas_phy, 0);
  1741. phy->phy_event &= ~PHY_PLUG_OUT;
  1742. }
  1743. } else {
  1744. mv_dprintk("plugin interrupt but phy%d is gone\n",
  1745. phy_no + mvi->id*mvi->chip->n_phy);
  1746. }
  1747. } else if (phy->irq_status & PHYEV_BROAD_CH) {
  1748. mv_dprintk("phy %d broadcast change.\n",
  1749. phy_no + mvi->id*mvi->chip->n_phy);
  1750. mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
  1751. EXP_BRCT_CHG);
  1752. }
  1753. }
  1754. int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
  1755. {
  1756. u32 rx_prod_idx, rx_desc;
  1757. bool attn = false;
  1758. /* the first dword in the RX ring is special: it contains
  1759. * a mirror of the hardware's RX producer index, so that
  1760. * we don't have to stall the CPU reading that register.
  1761. * The actual RX ring is offset by one dword, due to this.
  1762. */
  1763. rx_prod_idx = mvi->rx_cons;
  1764. mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
  1765. if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
  1766. return 0;
  1767. /* The CMPL_Q may come late, read from register and try again
  1768. * note: if coalescing is enabled,
  1769. * it will need to read from register every time for sure
  1770. */
  1771. if (unlikely(mvi->rx_cons == rx_prod_idx))
  1772. mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
  1773. if (mvi->rx_cons == rx_prod_idx)
  1774. return 0;
  1775. while (mvi->rx_cons != rx_prod_idx) {
  1776. /* increment our internal RX consumer pointer */
  1777. rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
  1778. rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
  1779. if (likely(rx_desc & RXQ_DONE))
  1780. mvs_slot_complete(mvi, rx_desc, 0);
  1781. if (rx_desc & RXQ_ATTN) {
  1782. attn = true;
  1783. } else if (rx_desc & RXQ_ERR) {
  1784. if (!(rx_desc & RXQ_DONE))
  1785. mvs_slot_complete(mvi, rx_desc, 0);
  1786. } else if (rx_desc & RXQ_SLOT_RESET) {
  1787. mvs_slot_free(mvi, rx_desc);
  1788. }
  1789. }
  1790. if (attn && self_clear)
  1791. MVS_CHIP_DISP->int_full(mvi);
  1792. return 0;
  1793. }