mvumi.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. * Marvell UMI driver
  3. *
  4. * Copyright 2011 Marvell. <jyli@marvell.com>
  5. *
  6. * This file is licensed under GPLv2.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; version 2 of the
  11. * License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/device.h>
  28. #include <linux/pci.h>
  29. #include <linux/list.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/io.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_cmnd.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_transport.h>
  39. #include <scsi/scsi_eh.h>
  40. #include <linux/uaccess.h>
  41. #include "mvumi.h"
  42. MODULE_LICENSE("GPL");
  43. MODULE_AUTHOR("jyli@marvell.com");
  44. MODULE_DESCRIPTION("Marvell UMI Driver");
  45. static DEFINE_PCI_DEVICE_TABLE(mvumi_pci_table) = {
  46. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL_2, PCI_DEVICE_ID_MARVELL_MV9143) },
  47. { 0 }
  48. };
  49. MODULE_DEVICE_TABLE(pci, mvumi_pci_table);
  50. static void tag_init(struct mvumi_tag *st, unsigned short size)
  51. {
  52. unsigned short i;
  53. BUG_ON(size != st->size);
  54. st->top = size;
  55. for (i = 0; i < size; i++)
  56. st->stack[i] = size - 1 - i;
  57. }
  58. static unsigned short tag_get_one(struct mvumi_hba *mhba, struct mvumi_tag *st)
  59. {
  60. BUG_ON(st->top <= 0);
  61. return st->stack[--st->top];
  62. }
  63. static void tag_release_one(struct mvumi_hba *mhba, struct mvumi_tag *st,
  64. unsigned short tag)
  65. {
  66. BUG_ON(st->top >= st->size);
  67. st->stack[st->top++] = tag;
  68. }
  69. static bool tag_is_empty(struct mvumi_tag *st)
  70. {
  71. if (st->top == 0)
  72. return 1;
  73. else
  74. return 0;
  75. }
  76. static void mvumi_unmap_pci_addr(struct pci_dev *dev, void **addr_array)
  77. {
  78. int i;
  79. for (i = 0; i < MAX_BASE_ADDRESS; i++)
  80. if ((pci_resource_flags(dev, i) & IORESOURCE_MEM) &&
  81. addr_array[i])
  82. pci_iounmap(dev, addr_array[i]);
  83. }
  84. static int mvumi_map_pci_addr(struct pci_dev *dev, void **addr_array)
  85. {
  86. int i;
  87. for (i = 0; i < MAX_BASE_ADDRESS; i++) {
  88. if (pci_resource_flags(dev, i) & IORESOURCE_MEM) {
  89. addr_array[i] = pci_iomap(dev, i, 0);
  90. if (!addr_array[i]) {
  91. dev_err(&dev->dev, "failed to map Bar[%d]\n",
  92. i);
  93. mvumi_unmap_pci_addr(dev, addr_array);
  94. return -ENOMEM;
  95. }
  96. } else
  97. addr_array[i] = NULL;
  98. dev_dbg(&dev->dev, "Bar %d : %p.\n", i, addr_array[i]);
  99. }
  100. return 0;
  101. }
  102. static struct mvumi_res *mvumi_alloc_mem_resource(struct mvumi_hba *mhba,
  103. enum resource_type type, unsigned int size)
  104. {
  105. struct mvumi_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
  106. if (!res) {
  107. dev_err(&mhba->pdev->dev,
  108. "Failed to allocate memory for resouce manager.\n");
  109. return NULL;
  110. }
  111. switch (type) {
  112. case RESOURCE_CACHED_MEMORY:
  113. res->virt_addr = kzalloc(size, GFP_KERNEL);
  114. if (!res->virt_addr) {
  115. dev_err(&mhba->pdev->dev,
  116. "unable to allocate memory,size = %d.\n", size);
  117. kfree(res);
  118. return NULL;
  119. }
  120. break;
  121. case RESOURCE_UNCACHED_MEMORY:
  122. size = round_up(size, 8);
  123. res->virt_addr = pci_alloc_consistent(mhba->pdev, size,
  124. &res->bus_addr);
  125. if (!res->virt_addr) {
  126. dev_err(&mhba->pdev->dev,
  127. "unable to allocate consistent mem,"
  128. "size = %d.\n", size);
  129. kfree(res);
  130. return NULL;
  131. }
  132. memset(res->virt_addr, 0, size);
  133. break;
  134. default:
  135. dev_err(&mhba->pdev->dev, "unknown resource type %d.\n", type);
  136. kfree(res);
  137. return NULL;
  138. }
  139. res->type = type;
  140. res->size = size;
  141. INIT_LIST_HEAD(&res->entry);
  142. list_add_tail(&res->entry, &mhba->res_list);
  143. return res;
  144. }
  145. static void mvumi_release_mem_resource(struct mvumi_hba *mhba)
  146. {
  147. struct mvumi_res *res, *tmp;
  148. list_for_each_entry_safe(res, tmp, &mhba->res_list, entry) {
  149. switch (res->type) {
  150. case RESOURCE_UNCACHED_MEMORY:
  151. pci_free_consistent(mhba->pdev, res->size,
  152. res->virt_addr, res->bus_addr);
  153. break;
  154. case RESOURCE_CACHED_MEMORY:
  155. kfree(res->virt_addr);
  156. break;
  157. default:
  158. dev_err(&mhba->pdev->dev,
  159. "unknown resource type %d\n", res->type);
  160. break;
  161. }
  162. list_del(&res->entry);
  163. kfree(res);
  164. }
  165. mhba->fw_flag &= ~MVUMI_FW_ALLOC;
  166. }
  167. /**
  168. * mvumi_make_sgl - Prepares SGL
  169. * @mhba: Adapter soft state
  170. * @scmd: SCSI command from the mid-layer
  171. * @sgl_p: SGL to be filled in
  172. * @sg_count return the number of SG elements
  173. *
  174. * If successful, this function returns 0. otherwise, it returns -1.
  175. */
  176. static int mvumi_make_sgl(struct mvumi_hba *mhba, struct scsi_cmnd *scmd,
  177. void *sgl_p, unsigned char *sg_count)
  178. {
  179. struct scatterlist *sg;
  180. struct mvumi_sgl *m_sg = (struct mvumi_sgl *) sgl_p;
  181. unsigned int i;
  182. unsigned int sgnum = scsi_sg_count(scmd);
  183. dma_addr_t busaddr;
  184. if (sgnum) {
  185. sg = scsi_sglist(scmd);
  186. *sg_count = pci_map_sg(mhba->pdev, sg, sgnum,
  187. (int) scmd->sc_data_direction);
  188. if (*sg_count > mhba->max_sge) {
  189. dev_err(&mhba->pdev->dev, "sg count[0x%x] is bigger "
  190. "than max sg[0x%x].\n",
  191. *sg_count, mhba->max_sge);
  192. return -1;
  193. }
  194. for (i = 0; i < *sg_count; i++) {
  195. busaddr = sg_dma_address(&sg[i]);
  196. m_sg->baseaddr_l = cpu_to_le32(lower_32_bits(busaddr));
  197. m_sg->baseaddr_h = cpu_to_le32(upper_32_bits(busaddr));
  198. m_sg->flags = 0;
  199. m_sg->size = cpu_to_le32(sg_dma_len(&sg[i]));
  200. if ((i + 1) == *sg_count)
  201. m_sg->flags |= SGD_EOT;
  202. m_sg++;
  203. }
  204. } else {
  205. scmd->SCp.dma_handle = scsi_bufflen(scmd) ?
  206. pci_map_single(mhba->pdev, scsi_sglist(scmd),
  207. scsi_bufflen(scmd),
  208. (int) scmd->sc_data_direction)
  209. : 0;
  210. busaddr = scmd->SCp.dma_handle;
  211. m_sg->baseaddr_l = cpu_to_le32(lower_32_bits(busaddr));
  212. m_sg->baseaddr_h = cpu_to_le32(upper_32_bits(busaddr));
  213. m_sg->flags = SGD_EOT;
  214. m_sg->size = cpu_to_le32(scsi_bufflen(scmd));
  215. *sg_count = 1;
  216. }
  217. return 0;
  218. }
  219. static int mvumi_internal_cmd_sgl(struct mvumi_hba *mhba, struct mvumi_cmd *cmd,
  220. unsigned int size)
  221. {
  222. struct mvumi_sgl *m_sg;
  223. void *virt_addr;
  224. dma_addr_t phy_addr;
  225. if (size == 0)
  226. return 0;
  227. virt_addr = pci_alloc_consistent(mhba->pdev, size, &phy_addr);
  228. if (!virt_addr)
  229. return -1;
  230. memset(virt_addr, 0, size);
  231. m_sg = (struct mvumi_sgl *) &cmd->frame->payload[0];
  232. cmd->frame->sg_counts = 1;
  233. cmd->data_buf = virt_addr;
  234. m_sg->baseaddr_l = cpu_to_le32(lower_32_bits(phy_addr));
  235. m_sg->baseaddr_h = cpu_to_le32(upper_32_bits(phy_addr));
  236. m_sg->flags = SGD_EOT;
  237. m_sg->size = cpu_to_le32(size);
  238. return 0;
  239. }
  240. static struct mvumi_cmd *mvumi_create_internal_cmd(struct mvumi_hba *mhba,
  241. unsigned int buf_size)
  242. {
  243. struct mvumi_cmd *cmd;
  244. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  245. if (!cmd) {
  246. dev_err(&mhba->pdev->dev, "failed to create a internal cmd\n");
  247. return NULL;
  248. }
  249. INIT_LIST_HEAD(&cmd->queue_pointer);
  250. cmd->frame = kzalloc(mhba->ib_max_size, GFP_KERNEL);
  251. if (!cmd->frame) {
  252. dev_err(&mhba->pdev->dev, "failed to allocate memory for FW"
  253. " frame,size = %d.\n", mhba->ib_max_size);
  254. kfree(cmd);
  255. return NULL;
  256. }
  257. if (buf_size) {
  258. if (mvumi_internal_cmd_sgl(mhba, cmd, buf_size)) {
  259. dev_err(&mhba->pdev->dev, "failed to allocate memory"
  260. " for internal frame\n");
  261. kfree(cmd->frame);
  262. kfree(cmd);
  263. return NULL;
  264. }
  265. } else
  266. cmd->frame->sg_counts = 0;
  267. return cmd;
  268. }
  269. static void mvumi_delete_internal_cmd(struct mvumi_hba *mhba,
  270. struct mvumi_cmd *cmd)
  271. {
  272. struct mvumi_sgl *m_sg;
  273. unsigned int size;
  274. dma_addr_t phy_addr;
  275. if (cmd && cmd->frame) {
  276. if (cmd->frame->sg_counts) {
  277. m_sg = (struct mvumi_sgl *) &cmd->frame->payload[0];
  278. size = m_sg->size;
  279. phy_addr = (dma_addr_t) m_sg->baseaddr_l |
  280. (dma_addr_t) ((m_sg->baseaddr_h << 16) << 16);
  281. pci_free_consistent(mhba->pdev, size, cmd->data_buf,
  282. phy_addr);
  283. }
  284. kfree(cmd->frame);
  285. kfree(cmd);
  286. }
  287. }
  288. /**
  289. * mvumi_get_cmd - Get a command from the free pool
  290. * @mhba: Adapter soft state
  291. *
  292. * Returns a free command from the pool
  293. */
  294. static struct mvumi_cmd *mvumi_get_cmd(struct mvumi_hba *mhba)
  295. {
  296. struct mvumi_cmd *cmd = NULL;
  297. if (likely(!list_empty(&mhba->cmd_pool))) {
  298. cmd = list_entry((&mhba->cmd_pool)->next,
  299. struct mvumi_cmd, queue_pointer);
  300. list_del_init(&cmd->queue_pointer);
  301. } else
  302. dev_warn(&mhba->pdev->dev, "command pool is empty!\n");
  303. return cmd;
  304. }
  305. /**
  306. * mvumi_return_cmd - Return a cmd to free command pool
  307. * @mhba: Adapter soft state
  308. * @cmd: Command packet to be returned to free command pool
  309. */
  310. static inline void mvumi_return_cmd(struct mvumi_hba *mhba,
  311. struct mvumi_cmd *cmd)
  312. {
  313. cmd->scmd = NULL;
  314. list_add_tail(&cmd->queue_pointer, &mhba->cmd_pool);
  315. }
  316. /**
  317. * mvumi_free_cmds - Free all the cmds in the free cmd pool
  318. * @mhba: Adapter soft state
  319. */
  320. static void mvumi_free_cmds(struct mvumi_hba *mhba)
  321. {
  322. struct mvumi_cmd *cmd;
  323. while (!list_empty(&mhba->cmd_pool)) {
  324. cmd = list_first_entry(&mhba->cmd_pool, struct mvumi_cmd,
  325. queue_pointer);
  326. list_del(&cmd->queue_pointer);
  327. kfree(cmd->frame);
  328. kfree(cmd);
  329. }
  330. }
  331. /**
  332. * mvumi_alloc_cmds - Allocates the command packets
  333. * @mhba: Adapter soft state
  334. *
  335. */
  336. static int mvumi_alloc_cmds(struct mvumi_hba *mhba)
  337. {
  338. int i;
  339. struct mvumi_cmd *cmd;
  340. for (i = 0; i < mhba->max_io; i++) {
  341. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  342. if (!cmd)
  343. goto err_exit;
  344. INIT_LIST_HEAD(&cmd->queue_pointer);
  345. list_add_tail(&cmd->queue_pointer, &mhba->cmd_pool);
  346. cmd->frame = kzalloc(mhba->ib_max_size, GFP_KERNEL);
  347. if (!cmd->frame)
  348. goto err_exit;
  349. }
  350. return 0;
  351. err_exit:
  352. dev_err(&mhba->pdev->dev,
  353. "failed to allocate memory for cmd[0x%x].\n", i);
  354. while (!list_empty(&mhba->cmd_pool)) {
  355. cmd = list_first_entry(&mhba->cmd_pool, struct mvumi_cmd,
  356. queue_pointer);
  357. list_del(&cmd->queue_pointer);
  358. kfree(cmd->frame);
  359. kfree(cmd);
  360. }
  361. return -ENOMEM;
  362. }
  363. static int mvumi_get_ib_list_entry(struct mvumi_hba *mhba, void **ib_entry)
  364. {
  365. unsigned int ib_rp_reg, cur_ib_entry;
  366. if (atomic_read(&mhba->fw_outstanding) >= mhba->max_io) {
  367. dev_warn(&mhba->pdev->dev, "firmware io overflow.\n");
  368. return -1;
  369. }
  370. ib_rp_reg = ioread32(mhba->mmio + CLA_INB_READ_POINTER);
  371. if (unlikely(((ib_rp_reg & CL_SLOT_NUM_MASK) ==
  372. (mhba->ib_cur_slot & CL_SLOT_NUM_MASK)) &&
  373. ((ib_rp_reg & CL_POINTER_TOGGLE) !=
  374. (mhba->ib_cur_slot & CL_POINTER_TOGGLE)))) {
  375. dev_warn(&mhba->pdev->dev, "no free slot to use.\n");
  376. return -1;
  377. }
  378. cur_ib_entry = mhba->ib_cur_slot & CL_SLOT_NUM_MASK;
  379. cur_ib_entry++;
  380. if (cur_ib_entry >= mhba->list_num_io) {
  381. cur_ib_entry -= mhba->list_num_io;
  382. mhba->ib_cur_slot ^= CL_POINTER_TOGGLE;
  383. }
  384. mhba->ib_cur_slot &= ~CL_SLOT_NUM_MASK;
  385. mhba->ib_cur_slot |= (cur_ib_entry & CL_SLOT_NUM_MASK);
  386. *ib_entry = mhba->ib_list + cur_ib_entry * mhba->ib_max_size;
  387. atomic_inc(&mhba->fw_outstanding);
  388. return 0;
  389. }
  390. static void mvumi_send_ib_list_entry(struct mvumi_hba *mhba)
  391. {
  392. iowrite32(0xfff, mhba->ib_shadow);
  393. iowrite32(mhba->ib_cur_slot, mhba->mmio + CLA_INB_WRITE_POINTER);
  394. }
  395. static char mvumi_check_ob_frame(struct mvumi_hba *mhba,
  396. unsigned int cur_obf, struct mvumi_rsp_frame *p_outb_frame)
  397. {
  398. unsigned short tag, request_id;
  399. udelay(1);
  400. p_outb_frame = mhba->ob_list + cur_obf * mhba->ob_max_size;
  401. request_id = p_outb_frame->request_id;
  402. tag = p_outb_frame->tag;
  403. if (tag > mhba->tag_pool.size) {
  404. dev_err(&mhba->pdev->dev, "ob frame data error\n");
  405. return -1;
  406. }
  407. if (mhba->tag_cmd[tag] == NULL) {
  408. dev_err(&mhba->pdev->dev, "tag[0x%x] with NO command\n", tag);
  409. return -1;
  410. } else if (mhba->tag_cmd[tag]->request_id != request_id &&
  411. mhba->request_id_enabled) {
  412. dev_err(&mhba->pdev->dev, "request ID from FW:0x%x,"
  413. "cmd request ID:0x%x\n", request_id,
  414. mhba->tag_cmd[tag]->request_id);
  415. return -1;
  416. }
  417. return 0;
  418. }
  419. static void mvumi_receive_ob_list_entry(struct mvumi_hba *mhba)
  420. {
  421. unsigned int ob_write_reg, ob_write_shadow_reg;
  422. unsigned int cur_obf, assign_obf_end, i;
  423. struct mvumi_ob_data *ob_data;
  424. struct mvumi_rsp_frame *p_outb_frame;
  425. do {
  426. ob_write_reg = ioread32(mhba->mmio + CLA_OUTB_COPY_POINTER);
  427. ob_write_shadow_reg = ioread32(mhba->ob_shadow);
  428. } while ((ob_write_reg & CL_SLOT_NUM_MASK) != ob_write_shadow_reg);
  429. cur_obf = mhba->ob_cur_slot & CL_SLOT_NUM_MASK;
  430. assign_obf_end = ob_write_reg & CL_SLOT_NUM_MASK;
  431. if ((ob_write_reg & CL_POINTER_TOGGLE) !=
  432. (mhba->ob_cur_slot & CL_POINTER_TOGGLE)) {
  433. assign_obf_end += mhba->list_num_io;
  434. }
  435. for (i = (assign_obf_end - cur_obf); i != 0; i--) {
  436. cur_obf++;
  437. if (cur_obf >= mhba->list_num_io) {
  438. cur_obf -= mhba->list_num_io;
  439. mhba->ob_cur_slot ^= CL_POINTER_TOGGLE;
  440. }
  441. p_outb_frame = mhba->ob_list + cur_obf * mhba->ob_max_size;
  442. /* Copy pointer may point to entry in outbound list
  443. * before entry has valid data
  444. */
  445. if (unlikely(p_outb_frame->tag > mhba->tag_pool.size ||
  446. mhba->tag_cmd[p_outb_frame->tag] == NULL ||
  447. p_outb_frame->request_id !=
  448. mhba->tag_cmd[p_outb_frame->tag]->request_id))
  449. if (mvumi_check_ob_frame(mhba, cur_obf, p_outb_frame))
  450. continue;
  451. if (!list_empty(&mhba->ob_data_list)) {
  452. ob_data = (struct mvumi_ob_data *)
  453. list_first_entry(&mhba->ob_data_list,
  454. struct mvumi_ob_data, list);
  455. list_del_init(&ob_data->list);
  456. } else {
  457. ob_data = NULL;
  458. if (cur_obf == 0) {
  459. cur_obf = mhba->list_num_io - 1;
  460. mhba->ob_cur_slot ^= CL_POINTER_TOGGLE;
  461. } else
  462. cur_obf -= 1;
  463. break;
  464. }
  465. memcpy(ob_data->data, p_outb_frame, mhba->ob_max_size);
  466. p_outb_frame->tag = 0xff;
  467. list_add_tail(&ob_data->list, &mhba->free_ob_list);
  468. }
  469. mhba->ob_cur_slot &= ~CL_SLOT_NUM_MASK;
  470. mhba->ob_cur_slot |= (cur_obf & CL_SLOT_NUM_MASK);
  471. iowrite32(mhba->ob_cur_slot, mhba->mmio + CLA_OUTB_READ_POINTER);
  472. }
  473. static void mvumi_reset(void *regs)
  474. {
  475. iowrite32(0, regs + CPU_ENPOINTA_MASK_REG);
  476. if (ioread32(regs + CPU_ARM_TO_PCIEA_MSG1) != HANDSHAKE_DONESTATE)
  477. return;
  478. iowrite32(DRBL_SOFT_RESET, regs + CPU_PCIEA_TO_ARM_DRBL_REG);
  479. }
  480. static unsigned char mvumi_start(struct mvumi_hba *mhba);
  481. static int mvumi_wait_for_outstanding(struct mvumi_hba *mhba)
  482. {
  483. mhba->fw_state = FW_STATE_ABORT;
  484. mvumi_reset(mhba->mmio);
  485. if (mvumi_start(mhba))
  486. return FAILED;
  487. else
  488. return SUCCESS;
  489. }
  490. static int mvumi_host_reset(struct scsi_cmnd *scmd)
  491. {
  492. struct mvumi_hba *mhba;
  493. mhba = (struct mvumi_hba *) scmd->device->host->hostdata;
  494. scmd_printk(KERN_NOTICE, scmd, "RESET -%ld cmd=%x retries=%x\n",
  495. scmd->serial_number, scmd->cmnd[0], scmd->retries);
  496. return mvumi_wait_for_outstanding(mhba);
  497. }
  498. static int mvumi_issue_blocked_cmd(struct mvumi_hba *mhba,
  499. struct mvumi_cmd *cmd)
  500. {
  501. unsigned long flags;
  502. cmd->cmd_status = REQ_STATUS_PENDING;
  503. if (atomic_read(&cmd->sync_cmd)) {
  504. dev_err(&mhba->pdev->dev,
  505. "last blocked cmd not finished, sync_cmd = %d\n",
  506. atomic_read(&cmd->sync_cmd));
  507. BUG_ON(1);
  508. return -1;
  509. }
  510. atomic_inc(&cmd->sync_cmd);
  511. spin_lock_irqsave(mhba->shost->host_lock, flags);
  512. mhba->instancet->fire_cmd(mhba, cmd);
  513. spin_unlock_irqrestore(mhba->shost->host_lock, flags);
  514. wait_event_timeout(mhba->int_cmd_wait_q,
  515. (cmd->cmd_status != REQ_STATUS_PENDING),
  516. MVUMI_INTERNAL_CMD_WAIT_TIME * HZ);
  517. /* command timeout */
  518. if (atomic_read(&cmd->sync_cmd)) {
  519. spin_lock_irqsave(mhba->shost->host_lock, flags);
  520. atomic_dec(&cmd->sync_cmd);
  521. if (mhba->tag_cmd[cmd->frame->tag]) {
  522. mhba->tag_cmd[cmd->frame->tag] = 0;
  523. dev_warn(&mhba->pdev->dev, "TIMEOUT:release tag [%d]\n",
  524. cmd->frame->tag);
  525. tag_release_one(mhba, &mhba->tag_pool, cmd->frame->tag);
  526. }
  527. if (!list_empty(&cmd->queue_pointer)) {
  528. dev_warn(&mhba->pdev->dev,
  529. "TIMEOUT:A internal command doesn't send!\n");
  530. list_del_init(&cmd->queue_pointer);
  531. } else
  532. atomic_dec(&mhba->fw_outstanding);
  533. spin_unlock_irqrestore(mhba->shost->host_lock, flags);
  534. }
  535. return 0;
  536. }
  537. static void mvumi_release_fw(struct mvumi_hba *mhba)
  538. {
  539. mvumi_free_cmds(mhba);
  540. mvumi_release_mem_resource(mhba);
  541. mvumi_unmap_pci_addr(mhba->pdev, mhba->base_addr);
  542. kfree(mhba->handshake_page);
  543. pci_release_regions(mhba->pdev);
  544. }
  545. static unsigned char mvumi_flush_cache(struct mvumi_hba *mhba)
  546. {
  547. struct mvumi_cmd *cmd;
  548. struct mvumi_msg_frame *frame;
  549. unsigned char device_id, retry = 0;
  550. unsigned char bitcount = sizeof(unsigned char) * 8;
  551. for (device_id = 0; device_id < mhba->max_target_id; device_id++) {
  552. if (!(mhba->target_map[device_id / bitcount] &
  553. (1 << (device_id % bitcount))))
  554. continue;
  555. get_cmd: cmd = mvumi_create_internal_cmd(mhba, 0);
  556. if (!cmd) {
  557. if (retry++ >= 5) {
  558. dev_err(&mhba->pdev->dev, "failed to get memory"
  559. " for internal flush cache cmd for "
  560. "device %d", device_id);
  561. retry = 0;
  562. continue;
  563. } else
  564. goto get_cmd;
  565. }
  566. cmd->scmd = NULL;
  567. cmd->cmd_status = REQ_STATUS_PENDING;
  568. atomic_set(&cmd->sync_cmd, 0);
  569. frame = cmd->frame;
  570. frame->req_function = CL_FUN_SCSI_CMD;
  571. frame->device_id = device_id;
  572. frame->cmd_flag = CMD_FLAG_NON_DATA;
  573. frame->data_transfer_length = 0;
  574. frame->cdb_length = MAX_COMMAND_SIZE;
  575. memset(frame->cdb, 0, MAX_COMMAND_SIZE);
  576. frame->cdb[0] = SCSI_CMD_MARVELL_SPECIFIC;
  577. frame->cdb[2] = CDB_CORE_SHUTDOWN;
  578. mvumi_issue_blocked_cmd(mhba, cmd);
  579. if (cmd->cmd_status != SAM_STAT_GOOD) {
  580. dev_err(&mhba->pdev->dev,
  581. "device %d flush cache failed, status=0x%x.\n",
  582. device_id, cmd->cmd_status);
  583. }
  584. mvumi_delete_internal_cmd(mhba, cmd);
  585. }
  586. return 0;
  587. }
  588. static unsigned char
  589. mvumi_calculate_checksum(struct mvumi_hs_header *p_header,
  590. unsigned short len)
  591. {
  592. unsigned char *ptr;
  593. unsigned char ret = 0, i;
  594. ptr = (unsigned char *) p_header->frame_content;
  595. for (i = 0; i < len; i++) {
  596. ret ^= *ptr;
  597. ptr++;
  598. }
  599. return ret;
  600. }
  601. void mvumi_hs_build_page(struct mvumi_hba *mhba,
  602. struct mvumi_hs_header *hs_header)
  603. {
  604. struct mvumi_hs_page2 *hs_page2;
  605. struct mvumi_hs_page4 *hs_page4;
  606. struct mvumi_hs_page3 *hs_page3;
  607. struct timeval time;
  608. unsigned int local_time;
  609. switch (hs_header->page_code) {
  610. case HS_PAGE_HOST_INFO:
  611. hs_page2 = (struct mvumi_hs_page2 *) hs_header;
  612. hs_header->frame_length = sizeof(*hs_page2) - 4;
  613. memset(hs_header->frame_content, 0, hs_header->frame_length);
  614. hs_page2->host_type = 3; /* 3 mean linux*/
  615. hs_page2->host_ver.ver_major = VER_MAJOR;
  616. hs_page2->host_ver.ver_minor = VER_MINOR;
  617. hs_page2->host_ver.ver_oem = VER_OEM;
  618. hs_page2->host_ver.ver_build = VER_BUILD;
  619. hs_page2->system_io_bus = 0;
  620. hs_page2->slot_number = 0;
  621. hs_page2->intr_level = 0;
  622. hs_page2->intr_vector = 0;
  623. do_gettimeofday(&time);
  624. local_time = (unsigned int) (time.tv_sec -
  625. (sys_tz.tz_minuteswest * 60));
  626. hs_page2->seconds_since1970 = local_time;
  627. hs_header->checksum = mvumi_calculate_checksum(hs_header,
  628. hs_header->frame_length);
  629. break;
  630. case HS_PAGE_FIRM_CTL:
  631. hs_page3 = (struct mvumi_hs_page3 *) hs_header;
  632. hs_header->frame_length = sizeof(*hs_page3) - 4;
  633. memset(hs_header->frame_content, 0, hs_header->frame_length);
  634. hs_header->checksum = mvumi_calculate_checksum(hs_header,
  635. hs_header->frame_length);
  636. break;
  637. case HS_PAGE_CL_INFO:
  638. hs_page4 = (struct mvumi_hs_page4 *) hs_header;
  639. hs_header->frame_length = sizeof(*hs_page4) - 4;
  640. memset(hs_header->frame_content, 0, hs_header->frame_length);
  641. hs_page4->ib_baseaddr_l = lower_32_bits(mhba->ib_list_phys);
  642. hs_page4->ib_baseaddr_h = upper_32_bits(mhba->ib_list_phys);
  643. hs_page4->ob_baseaddr_l = lower_32_bits(mhba->ob_list_phys);
  644. hs_page4->ob_baseaddr_h = upper_32_bits(mhba->ob_list_phys);
  645. hs_page4->ib_entry_size = mhba->ib_max_size_setting;
  646. hs_page4->ob_entry_size = mhba->ob_max_size_setting;
  647. hs_page4->ob_depth = mhba->list_num_io;
  648. hs_page4->ib_depth = mhba->list_num_io;
  649. hs_header->checksum = mvumi_calculate_checksum(hs_header,
  650. hs_header->frame_length);
  651. break;
  652. default:
  653. dev_err(&mhba->pdev->dev, "cannot build page, code[0x%x]\n",
  654. hs_header->page_code);
  655. break;
  656. }
  657. }
  658. /**
  659. * mvumi_init_data - Initialize requested date for FW
  660. * @mhba: Adapter soft state
  661. */
  662. static int mvumi_init_data(struct mvumi_hba *mhba)
  663. {
  664. struct mvumi_ob_data *ob_pool;
  665. struct mvumi_res *res_mgnt;
  666. unsigned int tmp_size, offset, i;
  667. void *virmem, *v;
  668. dma_addr_t p;
  669. if (mhba->fw_flag & MVUMI_FW_ALLOC)
  670. return 0;
  671. tmp_size = mhba->ib_max_size * mhba->max_io;
  672. tmp_size += 128 + mhba->ob_max_size * mhba->max_io;
  673. tmp_size += 8 + sizeof(u32) + 16;
  674. res_mgnt = mvumi_alloc_mem_resource(mhba,
  675. RESOURCE_UNCACHED_MEMORY, tmp_size);
  676. if (!res_mgnt) {
  677. dev_err(&mhba->pdev->dev,
  678. "failed to allocate memory for inbound list\n");
  679. goto fail_alloc_dma_buf;
  680. }
  681. p = res_mgnt->bus_addr;
  682. v = res_mgnt->virt_addr;
  683. /* ib_list */
  684. offset = round_up(p, 128) - p;
  685. p += offset;
  686. v += offset;
  687. mhba->ib_list = v;
  688. mhba->ib_list_phys = p;
  689. v += mhba->ib_max_size * mhba->max_io;
  690. p += mhba->ib_max_size * mhba->max_io;
  691. /* ib shadow */
  692. offset = round_up(p, 8) - p;
  693. p += offset;
  694. v += offset;
  695. mhba->ib_shadow = v;
  696. mhba->ib_shadow_phys = p;
  697. p += sizeof(u32);
  698. v += sizeof(u32);
  699. /* ob shadow */
  700. offset = round_up(p, 8) - p;
  701. p += offset;
  702. v += offset;
  703. mhba->ob_shadow = v;
  704. mhba->ob_shadow_phys = p;
  705. p += 8;
  706. v += 8;
  707. /* ob list */
  708. offset = round_up(p, 128) - p;
  709. p += offset;
  710. v += offset;
  711. mhba->ob_list = v;
  712. mhba->ob_list_phys = p;
  713. /* ob data pool */
  714. tmp_size = mhba->max_io * (mhba->ob_max_size + sizeof(*ob_pool));
  715. tmp_size = round_up(tmp_size, 8);
  716. res_mgnt = mvumi_alloc_mem_resource(mhba,
  717. RESOURCE_CACHED_MEMORY, tmp_size);
  718. if (!res_mgnt) {
  719. dev_err(&mhba->pdev->dev,
  720. "failed to allocate memory for outbound data buffer\n");
  721. goto fail_alloc_dma_buf;
  722. }
  723. virmem = res_mgnt->virt_addr;
  724. for (i = mhba->max_io; i != 0; i--) {
  725. ob_pool = (struct mvumi_ob_data *) virmem;
  726. list_add_tail(&ob_pool->list, &mhba->ob_data_list);
  727. virmem += mhba->ob_max_size + sizeof(*ob_pool);
  728. }
  729. tmp_size = sizeof(unsigned short) * mhba->max_io +
  730. sizeof(struct mvumi_cmd *) * mhba->max_io;
  731. tmp_size += round_up(mhba->max_target_id, sizeof(unsigned char) * 8) /
  732. (sizeof(unsigned char) * 8);
  733. res_mgnt = mvumi_alloc_mem_resource(mhba,
  734. RESOURCE_CACHED_MEMORY, tmp_size);
  735. if (!res_mgnt) {
  736. dev_err(&mhba->pdev->dev,
  737. "failed to allocate memory for tag and target map\n");
  738. goto fail_alloc_dma_buf;
  739. }
  740. virmem = res_mgnt->virt_addr;
  741. mhba->tag_pool.stack = virmem;
  742. mhba->tag_pool.size = mhba->max_io;
  743. tag_init(&mhba->tag_pool, mhba->max_io);
  744. virmem += sizeof(unsigned short) * mhba->max_io;
  745. mhba->tag_cmd = virmem;
  746. virmem += sizeof(struct mvumi_cmd *) * mhba->max_io;
  747. mhba->target_map = virmem;
  748. mhba->fw_flag |= MVUMI_FW_ALLOC;
  749. return 0;
  750. fail_alloc_dma_buf:
  751. mvumi_release_mem_resource(mhba);
  752. return -1;
  753. }
  754. static int mvumi_hs_process_page(struct mvumi_hba *mhba,
  755. struct mvumi_hs_header *hs_header)
  756. {
  757. struct mvumi_hs_page1 *hs_page1;
  758. unsigned char page_checksum;
  759. page_checksum = mvumi_calculate_checksum(hs_header,
  760. hs_header->frame_length);
  761. if (page_checksum != hs_header->checksum) {
  762. dev_err(&mhba->pdev->dev, "checksum error\n");
  763. return -1;
  764. }
  765. switch (hs_header->page_code) {
  766. case HS_PAGE_FIRM_CAP:
  767. hs_page1 = (struct mvumi_hs_page1 *) hs_header;
  768. mhba->max_io = hs_page1->max_io_support;
  769. mhba->list_num_io = hs_page1->cl_inout_list_depth;
  770. mhba->max_transfer_size = hs_page1->max_transfer_size;
  771. mhba->max_target_id = hs_page1->max_devices_support;
  772. mhba->hba_capability = hs_page1->capability;
  773. mhba->ib_max_size_setting = hs_page1->cl_in_max_entry_size;
  774. mhba->ib_max_size = (1 << hs_page1->cl_in_max_entry_size) << 2;
  775. mhba->ob_max_size_setting = hs_page1->cl_out_max_entry_size;
  776. mhba->ob_max_size = (1 << hs_page1->cl_out_max_entry_size) << 2;
  777. dev_dbg(&mhba->pdev->dev, "FW version:%d\n",
  778. hs_page1->fw_ver.ver_build);
  779. break;
  780. default:
  781. dev_err(&mhba->pdev->dev, "handshake: page code error\n");
  782. return -1;
  783. }
  784. return 0;
  785. }
  786. /**
  787. * mvumi_handshake - Move the FW to READY state
  788. * @mhba: Adapter soft state
  789. *
  790. * During the initialization, FW passes can potentially be in any one of
  791. * several possible states. If the FW in operational, waiting-for-handshake
  792. * states, driver must take steps to bring it to ready state. Otherwise, it
  793. * has to wait for the ready state.
  794. */
  795. static int mvumi_handshake(struct mvumi_hba *mhba)
  796. {
  797. unsigned int hs_state, tmp, hs_fun;
  798. struct mvumi_hs_header *hs_header;
  799. void *regs = mhba->mmio;
  800. if (mhba->fw_state == FW_STATE_STARTING)
  801. hs_state = HS_S_START;
  802. else {
  803. tmp = ioread32(regs + CPU_ARM_TO_PCIEA_MSG0);
  804. hs_state = HS_GET_STATE(tmp);
  805. dev_dbg(&mhba->pdev->dev, "handshake state[0x%x].\n", hs_state);
  806. if (HS_GET_STATUS(tmp) != HS_STATUS_OK) {
  807. mhba->fw_state = FW_STATE_STARTING;
  808. return -1;
  809. }
  810. }
  811. hs_fun = 0;
  812. switch (hs_state) {
  813. case HS_S_START:
  814. mhba->fw_state = FW_STATE_HANDSHAKING;
  815. HS_SET_STATUS(hs_fun, HS_STATUS_OK);
  816. HS_SET_STATE(hs_fun, HS_S_RESET);
  817. iowrite32(HANDSHAKE_SIGNATURE, regs + CPU_PCIEA_TO_ARM_MSG1);
  818. iowrite32(hs_fun, regs + CPU_PCIEA_TO_ARM_MSG0);
  819. iowrite32(DRBL_HANDSHAKE, regs + CPU_PCIEA_TO_ARM_DRBL_REG);
  820. break;
  821. case HS_S_RESET:
  822. iowrite32(lower_32_bits(mhba->handshake_page_phys),
  823. regs + CPU_PCIEA_TO_ARM_MSG1);
  824. iowrite32(upper_32_bits(mhba->handshake_page_phys),
  825. regs + CPU_ARM_TO_PCIEA_MSG1);
  826. HS_SET_STATUS(hs_fun, HS_STATUS_OK);
  827. HS_SET_STATE(hs_fun, HS_S_PAGE_ADDR);
  828. iowrite32(hs_fun, regs + CPU_PCIEA_TO_ARM_MSG0);
  829. iowrite32(DRBL_HANDSHAKE, regs + CPU_PCIEA_TO_ARM_DRBL_REG);
  830. break;
  831. case HS_S_PAGE_ADDR:
  832. case HS_S_QUERY_PAGE:
  833. case HS_S_SEND_PAGE:
  834. hs_header = (struct mvumi_hs_header *) mhba->handshake_page;
  835. if (hs_header->page_code == HS_PAGE_FIRM_CAP) {
  836. mhba->hba_total_pages =
  837. ((struct mvumi_hs_page1 *) hs_header)->total_pages;
  838. if (mhba->hba_total_pages == 0)
  839. mhba->hba_total_pages = HS_PAGE_TOTAL-1;
  840. }
  841. if (hs_state == HS_S_QUERY_PAGE) {
  842. if (mvumi_hs_process_page(mhba, hs_header)) {
  843. HS_SET_STATE(hs_fun, HS_S_ABORT);
  844. return -1;
  845. }
  846. if (mvumi_init_data(mhba)) {
  847. HS_SET_STATE(hs_fun, HS_S_ABORT);
  848. return -1;
  849. }
  850. } else if (hs_state == HS_S_PAGE_ADDR) {
  851. hs_header->page_code = 0;
  852. mhba->hba_total_pages = HS_PAGE_TOTAL-1;
  853. }
  854. if ((hs_header->page_code + 1) <= mhba->hba_total_pages) {
  855. hs_header->page_code++;
  856. if (hs_header->page_code != HS_PAGE_FIRM_CAP) {
  857. mvumi_hs_build_page(mhba, hs_header);
  858. HS_SET_STATE(hs_fun, HS_S_SEND_PAGE);
  859. } else
  860. HS_SET_STATE(hs_fun, HS_S_QUERY_PAGE);
  861. } else
  862. HS_SET_STATE(hs_fun, HS_S_END);
  863. HS_SET_STATUS(hs_fun, HS_STATUS_OK);
  864. iowrite32(hs_fun, regs + CPU_PCIEA_TO_ARM_MSG0);
  865. iowrite32(DRBL_HANDSHAKE, regs + CPU_PCIEA_TO_ARM_DRBL_REG);
  866. break;
  867. case HS_S_END:
  868. /* Set communication list ISR */
  869. tmp = ioread32(regs + CPU_ENPOINTA_MASK_REG);
  870. tmp |= INT_MAP_COMAOUT | INT_MAP_COMAERR;
  871. iowrite32(tmp, regs + CPU_ENPOINTA_MASK_REG);
  872. iowrite32(mhba->list_num_io, mhba->ib_shadow);
  873. /* Set InBound List Avaliable count shadow */
  874. iowrite32(lower_32_bits(mhba->ib_shadow_phys),
  875. regs + CLA_INB_AVAL_COUNT_BASEL);
  876. iowrite32(upper_32_bits(mhba->ib_shadow_phys),
  877. regs + CLA_INB_AVAL_COUNT_BASEH);
  878. /* Set OutBound List Avaliable count shadow */
  879. iowrite32((mhba->list_num_io-1) | CL_POINTER_TOGGLE,
  880. mhba->ob_shadow);
  881. iowrite32(lower_32_bits(mhba->ob_shadow_phys), regs + 0x5B0);
  882. iowrite32(upper_32_bits(mhba->ob_shadow_phys), regs + 0x5B4);
  883. mhba->ib_cur_slot = (mhba->list_num_io - 1) | CL_POINTER_TOGGLE;
  884. mhba->ob_cur_slot = (mhba->list_num_io - 1) | CL_POINTER_TOGGLE;
  885. mhba->fw_state = FW_STATE_STARTED;
  886. break;
  887. default:
  888. dev_err(&mhba->pdev->dev, "unknown handshake state [0x%x].\n",
  889. hs_state);
  890. return -1;
  891. }
  892. return 0;
  893. }
  894. static unsigned char mvumi_handshake_event(struct mvumi_hba *mhba)
  895. {
  896. unsigned int isr_status;
  897. unsigned long before;
  898. before = jiffies;
  899. mvumi_handshake(mhba);
  900. do {
  901. isr_status = mhba->instancet->read_fw_status_reg(mhba->mmio);
  902. if (mhba->fw_state == FW_STATE_STARTED)
  903. return 0;
  904. if (time_after(jiffies, before + FW_MAX_DELAY * HZ)) {
  905. dev_err(&mhba->pdev->dev,
  906. "no handshake response at state 0x%x.\n",
  907. mhba->fw_state);
  908. dev_err(&mhba->pdev->dev,
  909. "isr : global=0x%x,status=0x%x.\n",
  910. mhba->global_isr, isr_status);
  911. return -1;
  912. }
  913. rmb();
  914. usleep_range(1000, 2000);
  915. } while (!(isr_status & DRBL_HANDSHAKE_ISR));
  916. return 0;
  917. }
  918. static unsigned char mvumi_check_handshake(struct mvumi_hba *mhba)
  919. {
  920. void *regs = mhba->mmio;
  921. unsigned int tmp;
  922. unsigned long before;
  923. before = jiffies;
  924. tmp = ioread32(regs + CPU_ARM_TO_PCIEA_MSG1);
  925. while ((tmp != HANDSHAKE_READYSTATE) && (tmp != HANDSHAKE_DONESTATE)) {
  926. if (tmp != HANDSHAKE_READYSTATE)
  927. iowrite32(DRBL_MU_RESET,
  928. regs + CPU_PCIEA_TO_ARM_DRBL_REG);
  929. if (time_after(jiffies, before + FW_MAX_DELAY * HZ)) {
  930. dev_err(&mhba->pdev->dev,
  931. "invalid signature [0x%x].\n", tmp);
  932. return -1;
  933. }
  934. usleep_range(1000, 2000);
  935. rmb();
  936. tmp = ioread32(regs + CPU_ARM_TO_PCIEA_MSG1);
  937. }
  938. mhba->fw_state = FW_STATE_STARTING;
  939. dev_dbg(&mhba->pdev->dev, "start firmware handshake...\n");
  940. do {
  941. if (mvumi_handshake_event(mhba)) {
  942. dev_err(&mhba->pdev->dev,
  943. "handshake failed at state 0x%x.\n",
  944. mhba->fw_state);
  945. return -1;
  946. }
  947. } while (mhba->fw_state != FW_STATE_STARTED);
  948. dev_dbg(&mhba->pdev->dev, "firmware handshake done\n");
  949. return 0;
  950. }
  951. static unsigned char mvumi_start(struct mvumi_hba *mhba)
  952. {
  953. void *regs = mhba->mmio;
  954. unsigned int tmp;
  955. /* clear Door bell */
  956. tmp = ioread32(regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  957. iowrite32(tmp, regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  958. iowrite32(0x3FFFFFFF, regs + CPU_ARM_TO_PCIEA_MASK_REG);
  959. tmp = ioread32(regs + CPU_ENPOINTA_MASK_REG) | INT_MAP_DL_CPU2PCIEA;
  960. iowrite32(tmp, regs + CPU_ENPOINTA_MASK_REG);
  961. if (mvumi_check_handshake(mhba))
  962. return -1;
  963. return 0;
  964. }
  965. /**
  966. * mvumi_complete_cmd - Completes a command
  967. * @mhba: Adapter soft state
  968. * @cmd: Command to be completed
  969. */
  970. static void mvumi_complete_cmd(struct mvumi_hba *mhba, struct mvumi_cmd *cmd,
  971. struct mvumi_rsp_frame *ob_frame)
  972. {
  973. struct scsi_cmnd *scmd = cmd->scmd;
  974. cmd->scmd->SCp.ptr = NULL;
  975. scmd->result = ob_frame->req_status;
  976. switch (ob_frame->req_status) {
  977. case SAM_STAT_GOOD:
  978. scmd->result |= DID_OK << 16;
  979. break;
  980. case SAM_STAT_BUSY:
  981. scmd->result |= DID_BUS_BUSY << 16;
  982. break;
  983. case SAM_STAT_CHECK_CONDITION:
  984. scmd->result |= (DID_OK << 16);
  985. if (ob_frame->rsp_flag & CL_RSP_FLAG_SENSEDATA) {
  986. memcpy(cmd->scmd->sense_buffer, ob_frame->payload,
  987. sizeof(struct mvumi_sense_data));
  988. scmd->result |= (DRIVER_SENSE << 24);
  989. }
  990. break;
  991. default:
  992. scmd->result |= (DRIVER_INVALID << 24) | (DID_ABORT << 16);
  993. break;
  994. }
  995. if (scsi_bufflen(scmd)) {
  996. if (scsi_sg_count(scmd)) {
  997. pci_unmap_sg(mhba->pdev,
  998. scsi_sglist(scmd),
  999. scsi_sg_count(scmd),
  1000. (int) scmd->sc_data_direction);
  1001. } else {
  1002. pci_unmap_single(mhba->pdev,
  1003. scmd->SCp.dma_handle,
  1004. scsi_bufflen(scmd),
  1005. (int) scmd->sc_data_direction);
  1006. scmd->SCp.dma_handle = 0;
  1007. }
  1008. }
  1009. cmd->scmd->scsi_done(scmd);
  1010. mvumi_return_cmd(mhba, cmd);
  1011. }
  1012. static void mvumi_complete_internal_cmd(struct mvumi_hba *mhba,
  1013. struct mvumi_cmd *cmd,
  1014. struct mvumi_rsp_frame *ob_frame)
  1015. {
  1016. if (atomic_read(&cmd->sync_cmd)) {
  1017. cmd->cmd_status = ob_frame->req_status;
  1018. if ((ob_frame->req_status == SAM_STAT_CHECK_CONDITION) &&
  1019. (ob_frame->rsp_flag & CL_RSP_FLAG_SENSEDATA) &&
  1020. cmd->data_buf) {
  1021. memcpy(cmd->data_buf, ob_frame->payload,
  1022. sizeof(struct mvumi_sense_data));
  1023. }
  1024. atomic_dec(&cmd->sync_cmd);
  1025. wake_up(&mhba->int_cmd_wait_q);
  1026. }
  1027. }
  1028. static void mvumi_show_event(struct mvumi_hba *mhba,
  1029. struct mvumi_driver_event *ptr)
  1030. {
  1031. unsigned int i;
  1032. dev_warn(&mhba->pdev->dev,
  1033. "Event[0x%x] id[0x%x] severity[0x%x] device id[0x%x]\n",
  1034. ptr->sequence_no, ptr->event_id, ptr->severity, ptr->device_id);
  1035. if (ptr->param_count) {
  1036. printk(KERN_WARNING "Event param(len 0x%x): ",
  1037. ptr->param_count);
  1038. for (i = 0; i < ptr->param_count; i++)
  1039. printk(KERN_WARNING "0x%x ", ptr->params[i]);
  1040. printk(KERN_WARNING "\n");
  1041. }
  1042. if (ptr->sense_data_length) {
  1043. printk(KERN_WARNING "Event sense data(len 0x%x): ",
  1044. ptr->sense_data_length);
  1045. for (i = 0; i < ptr->sense_data_length; i++)
  1046. printk(KERN_WARNING "0x%x ", ptr->sense_data[i]);
  1047. printk(KERN_WARNING "\n");
  1048. }
  1049. }
  1050. static void mvumi_notification(struct mvumi_hba *mhba, u8 msg, void *buffer)
  1051. {
  1052. if (msg == APICDB1_EVENT_GETEVENT) {
  1053. int i, count;
  1054. struct mvumi_driver_event *param = NULL;
  1055. struct mvumi_event_req *er = buffer;
  1056. count = er->count;
  1057. if (count > MAX_EVENTS_RETURNED) {
  1058. dev_err(&mhba->pdev->dev, "event count[0x%x] is bigger"
  1059. " than max event count[0x%x].\n",
  1060. count, MAX_EVENTS_RETURNED);
  1061. return;
  1062. }
  1063. for (i = 0; i < count; i++) {
  1064. param = &er->events[i];
  1065. mvumi_show_event(mhba, param);
  1066. }
  1067. }
  1068. }
  1069. static int mvumi_get_event(struct mvumi_hba *mhba, unsigned char msg)
  1070. {
  1071. struct mvumi_cmd *cmd;
  1072. struct mvumi_msg_frame *frame;
  1073. cmd = mvumi_create_internal_cmd(mhba, 512);
  1074. if (!cmd)
  1075. return -1;
  1076. cmd->scmd = NULL;
  1077. cmd->cmd_status = REQ_STATUS_PENDING;
  1078. atomic_set(&cmd->sync_cmd, 0);
  1079. frame = cmd->frame;
  1080. frame->device_id = 0;
  1081. frame->cmd_flag = CMD_FLAG_DATA_IN;
  1082. frame->req_function = CL_FUN_SCSI_CMD;
  1083. frame->cdb_length = MAX_COMMAND_SIZE;
  1084. frame->data_transfer_length = sizeof(struct mvumi_event_req);
  1085. memset(frame->cdb, 0, MAX_COMMAND_SIZE);
  1086. frame->cdb[0] = APICDB0_EVENT;
  1087. frame->cdb[1] = msg;
  1088. mvumi_issue_blocked_cmd(mhba, cmd);
  1089. if (cmd->cmd_status != SAM_STAT_GOOD)
  1090. dev_err(&mhba->pdev->dev, "get event failed, status=0x%x.\n",
  1091. cmd->cmd_status);
  1092. else
  1093. mvumi_notification(mhba, cmd->frame->cdb[1], cmd->data_buf);
  1094. mvumi_delete_internal_cmd(mhba, cmd);
  1095. return 0;
  1096. }
  1097. static void mvumi_scan_events(struct work_struct *work)
  1098. {
  1099. struct mvumi_events_wq *mu_ev =
  1100. container_of(work, struct mvumi_events_wq, work_q);
  1101. mvumi_get_event(mu_ev->mhba, mu_ev->event);
  1102. kfree(mu_ev);
  1103. }
  1104. static void mvumi_launch_events(struct mvumi_hba *mhba, u8 msg)
  1105. {
  1106. struct mvumi_events_wq *mu_ev;
  1107. mu_ev = kzalloc(sizeof(*mu_ev), GFP_ATOMIC);
  1108. if (mu_ev) {
  1109. INIT_WORK(&mu_ev->work_q, mvumi_scan_events);
  1110. mu_ev->mhba = mhba;
  1111. mu_ev->event = msg;
  1112. mu_ev->param = NULL;
  1113. schedule_work(&mu_ev->work_q);
  1114. }
  1115. }
  1116. static void mvumi_handle_clob(struct mvumi_hba *mhba)
  1117. {
  1118. struct mvumi_rsp_frame *ob_frame;
  1119. struct mvumi_cmd *cmd;
  1120. struct mvumi_ob_data *pool;
  1121. while (!list_empty(&mhba->free_ob_list)) {
  1122. pool = list_first_entry(&mhba->free_ob_list,
  1123. struct mvumi_ob_data, list);
  1124. list_del_init(&pool->list);
  1125. list_add_tail(&pool->list, &mhba->ob_data_list);
  1126. ob_frame = (struct mvumi_rsp_frame *) &pool->data[0];
  1127. cmd = mhba->tag_cmd[ob_frame->tag];
  1128. atomic_dec(&mhba->fw_outstanding);
  1129. mhba->tag_cmd[ob_frame->tag] = 0;
  1130. tag_release_one(mhba, &mhba->tag_pool, ob_frame->tag);
  1131. if (cmd->scmd)
  1132. mvumi_complete_cmd(mhba, cmd, ob_frame);
  1133. else
  1134. mvumi_complete_internal_cmd(mhba, cmd, ob_frame);
  1135. }
  1136. mhba->instancet->fire_cmd(mhba, NULL);
  1137. }
  1138. static irqreturn_t mvumi_isr_handler(int irq, void *devp)
  1139. {
  1140. struct mvumi_hba *mhba = (struct mvumi_hba *) devp;
  1141. unsigned long flags;
  1142. spin_lock_irqsave(mhba->shost->host_lock, flags);
  1143. if (unlikely(mhba->instancet->clear_intr(mhba) || !mhba->global_isr)) {
  1144. spin_unlock_irqrestore(mhba->shost->host_lock, flags);
  1145. return IRQ_NONE;
  1146. }
  1147. if (mhba->global_isr & INT_MAP_DL_CPU2PCIEA) {
  1148. if (mhba->isr_status & DRBL_HANDSHAKE_ISR) {
  1149. dev_warn(&mhba->pdev->dev, "enter handshake again!\n");
  1150. mvumi_handshake(mhba);
  1151. }
  1152. if (mhba->isr_status & DRBL_EVENT_NOTIFY)
  1153. mvumi_launch_events(mhba, APICDB1_EVENT_GETEVENT);
  1154. }
  1155. if (mhba->global_isr & INT_MAP_COMAOUT)
  1156. mvumi_receive_ob_list_entry(mhba);
  1157. mhba->global_isr = 0;
  1158. mhba->isr_status = 0;
  1159. if (mhba->fw_state == FW_STATE_STARTED)
  1160. mvumi_handle_clob(mhba);
  1161. spin_unlock_irqrestore(mhba->shost->host_lock, flags);
  1162. return IRQ_HANDLED;
  1163. }
  1164. static enum mvumi_qc_result mvumi_send_command(struct mvumi_hba *mhba,
  1165. struct mvumi_cmd *cmd)
  1166. {
  1167. void *ib_entry;
  1168. struct mvumi_msg_frame *ib_frame;
  1169. unsigned int frame_len;
  1170. ib_frame = cmd->frame;
  1171. if (unlikely(mhba->fw_state != FW_STATE_STARTED)) {
  1172. dev_dbg(&mhba->pdev->dev, "firmware not ready.\n");
  1173. return MV_QUEUE_COMMAND_RESULT_NO_RESOURCE;
  1174. }
  1175. if (tag_is_empty(&mhba->tag_pool)) {
  1176. dev_dbg(&mhba->pdev->dev, "no free tag.\n");
  1177. return MV_QUEUE_COMMAND_RESULT_NO_RESOURCE;
  1178. }
  1179. if (mvumi_get_ib_list_entry(mhba, &ib_entry))
  1180. return MV_QUEUE_COMMAND_RESULT_NO_RESOURCE;
  1181. cmd->frame->tag = tag_get_one(mhba, &mhba->tag_pool);
  1182. cmd->frame->request_id = mhba->io_seq++;
  1183. cmd->request_id = cmd->frame->request_id;
  1184. mhba->tag_cmd[cmd->frame->tag] = cmd;
  1185. frame_len = sizeof(*ib_frame) - 4 +
  1186. ib_frame->sg_counts * sizeof(struct mvumi_sgl);
  1187. memcpy(ib_entry, ib_frame, frame_len);
  1188. return MV_QUEUE_COMMAND_RESULT_SENT;
  1189. }
  1190. static void mvumi_fire_cmd(struct mvumi_hba *mhba, struct mvumi_cmd *cmd)
  1191. {
  1192. unsigned short num_of_cl_sent = 0;
  1193. enum mvumi_qc_result result;
  1194. if (cmd)
  1195. list_add_tail(&cmd->queue_pointer, &mhba->waiting_req_list);
  1196. while (!list_empty(&mhba->waiting_req_list)) {
  1197. cmd = list_first_entry(&mhba->waiting_req_list,
  1198. struct mvumi_cmd, queue_pointer);
  1199. list_del_init(&cmd->queue_pointer);
  1200. result = mvumi_send_command(mhba, cmd);
  1201. switch (result) {
  1202. case MV_QUEUE_COMMAND_RESULT_SENT:
  1203. num_of_cl_sent++;
  1204. break;
  1205. case MV_QUEUE_COMMAND_RESULT_NO_RESOURCE:
  1206. list_add(&cmd->queue_pointer, &mhba->waiting_req_list);
  1207. if (num_of_cl_sent > 0)
  1208. mvumi_send_ib_list_entry(mhba);
  1209. return;
  1210. }
  1211. }
  1212. if (num_of_cl_sent > 0)
  1213. mvumi_send_ib_list_entry(mhba);
  1214. }
  1215. /**
  1216. * mvumi_enable_intr - Enables interrupts
  1217. * @regs: FW register set
  1218. */
  1219. static void mvumi_enable_intr(void *regs)
  1220. {
  1221. unsigned int mask;
  1222. iowrite32(0x3FFFFFFF, regs + CPU_ARM_TO_PCIEA_MASK_REG);
  1223. mask = ioread32(regs + CPU_ENPOINTA_MASK_REG);
  1224. mask |= INT_MAP_DL_CPU2PCIEA | INT_MAP_COMAOUT | INT_MAP_COMAERR;
  1225. iowrite32(mask, regs + CPU_ENPOINTA_MASK_REG);
  1226. }
  1227. /**
  1228. * mvumi_disable_intr -Disables interrupt
  1229. * @regs: FW register set
  1230. */
  1231. static void mvumi_disable_intr(void *regs)
  1232. {
  1233. unsigned int mask;
  1234. iowrite32(0, regs + CPU_ARM_TO_PCIEA_MASK_REG);
  1235. mask = ioread32(regs + CPU_ENPOINTA_MASK_REG);
  1236. mask &= ~(INT_MAP_DL_CPU2PCIEA | INT_MAP_COMAOUT | INT_MAP_COMAERR);
  1237. iowrite32(mask, regs + CPU_ENPOINTA_MASK_REG);
  1238. }
  1239. static int mvumi_clear_intr(void *extend)
  1240. {
  1241. struct mvumi_hba *mhba = (struct mvumi_hba *) extend;
  1242. unsigned int status, isr_status = 0, tmp = 0;
  1243. void *regs = mhba->mmio;
  1244. status = ioread32(regs + CPU_MAIN_INT_CAUSE_REG);
  1245. if (!(status & INT_MAP_MU) || status == 0xFFFFFFFF)
  1246. return 1;
  1247. if (unlikely(status & INT_MAP_COMAERR)) {
  1248. tmp = ioread32(regs + CLA_ISR_CAUSE);
  1249. if (tmp & (CLIC_IN_ERR_IRQ | CLIC_OUT_ERR_IRQ))
  1250. iowrite32(tmp & (CLIC_IN_ERR_IRQ | CLIC_OUT_ERR_IRQ),
  1251. regs + CLA_ISR_CAUSE);
  1252. status ^= INT_MAP_COMAERR;
  1253. /* inbound or outbound parity error, command will timeout */
  1254. }
  1255. if (status & INT_MAP_COMAOUT) {
  1256. tmp = ioread32(regs + CLA_ISR_CAUSE);
  1257. if (tmp & CLIC_OUT_IRQ)
  1258. iowrite32(tmp & CLIC_OUT_IRQ, regs + CLA_ISR_CAUSE);
  1259. }
  1260. if (status & INT_MAP_DL_CPU2PCIEA) {
  1261. isr_status = ioread32(regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  1262. if (isr_status)
  1263. iowrite32(isr_status, regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  1264. }
  1265. mhba->global_isr = status;
  1266. mhba->isr_status = isr_status;
  1267. return 0;
  1268. }
  1269. /**
  1270. * mvumi_read_fw_status_reg - returns the current FW status value
  1271. * @regs: FW register set
  1272. */
  1273. static unsigned int mvumi_read_fw_status_reg(void *regs)
  1274. {
  1275. unsigned int status;
  1276. status = ioread32(regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  1277. if (status)
  1278. iowrite32(status, regs + CPU_ARM_TO_PCIEA_DRBL_REG);
  1279. return status;
  1280. }
  1281. static struct mvumi_instance_template mvumi_instance_template = {
  1282. .fire_cmd = mvumi_fire_cmd,
  1283. .enable_intr = mvumi_enable_intr,
  1284. .disable_intr = mvumi_disable_intr,
  1285. .clear_intr = mvumi_clear_intr,
  1286. .read_fw_status_reg = mvumi_read_fw_status_reg,
  1287. };
  1288. static int mvumi_slave_configure(struct scsi_device *sdev)
  1289. {
  1290. struct mvumi_hba *mhba;
  1291. unsigned char bitcount = sizeof(unsigned char) * 8;
  1292. mhba = (struct mvumi_hba *) sdev->host->hostdata;
  1293. if (sdev->id >= mhba->max_target_id)
  1294. return -EINVAL;
  1295. mhba->target_map[sdev->id / bitcount] |= (1 << (sdev->id % bitcount));
  1296. return 0;
  1297. }
  1298. /**
  1299. * mvumi_build_frame - Prepares a direct cdb (DCDB) command
  1300. * @mhba: Adapter soft state
  1301. * @scmd: SCSI command
  1302. * @cmd: Command to be prepared in
  1303. *
  1304. * This function prepares CDB commands. These are typcially pass-through
  1305. * commands to the devices.
  1306. */
  1307. static unsigned char mvumi_build_frame(struct mvumi_hba *mhba,
  1308. struct scsi_cmnd *scmd, struct mvumi_cmd *cmd)
  1309. {
  1310. struct mvumi_msg_frame *pframe;
  1311. cmd->scmd = scmd;
  1312. cmd->cmd_status = REQ_STATUS_PENDING;
  1313. pframe = cmd->frame;
  1314. pframe->device_id = ((unsigned short) scmd->device->id) |
  1315. (((unsigned short) scmd->device->lun) << 8);
  1316. pframe->cmd_flag = 0;
  1317. switch (scmd->sc_data_direction) {
  1318. case DMA_NONE:
  1319. pframe->cmd_flag |= CMD_FLAG_NON_DATA;
  1320. break;
  1321. case DMA_FROM_DEVICE:
  1322. pframe->cmd_flag |= CMD_FLAG_DATA_IN;
  1323. break;
  1324. case DMA_TO_DEVICE:
  1325. pframe->cmd_flag |= CMD_FLAG_DATA_OUT;
  1326. break;
  1327. case DMA_BIDIRECTIONAL:
  1328. default:
  1329. dev_warn(&mhba->pdev->dev, "unexpected data direction[%d] "
  1330. "cmd[0x%x]\n", scmd->sc_data_direction, scmd->cmnd[0]);
  1331. goto error;
  1332. }
  1333. pframe->cdb_length = scmd->cmd_len;
  1334. memcpy(pframe->cdb, scmd->cmnd, pframe->cdb_length);
  1335. pframe->req_function = CL_FUN_SCSI_CMD;
  1336. if (scsi_bufflen(scmd)) {
  1337. if (mvumi_make_sgl(mhba, scmd, &pframe->payload[0],
  1338. &pframe->sg_counts))
  1339. goto error;
  1340. pframe->data_transfer_length = scsi_bufflen(scmd);
  1341. } else {
  1342. pframe->sg_counts = 0;
  1343. pframe->data_transfer_length = 0;
  1344. }
  1345. return 0;
  1346. error:
  1347. scmd->result = (DID_OK << 16) | (DRIVER_SENSE << 24) |
  1348. SAM_STAT_CHECK_CONDITION;
  1349. scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x24,
  1350. 0);
  1351. return -1;
  1352. }
  1353. /**
  1354. * mvumi_queue_command - Queue entry point
  1355. * @scmd: SCSI command to be queued
  1356. * @done: Callback entry point
  1357. */
  1358. static int mvumi_queue_command(struct Scsi_Host *shost,
  1359. struct scsi_cmnd *scmd)
  1360. {
  1361. struct mvumi_cmd *cmd;
  1362. struct mvumi_hba *mhba;
  1363. unsigned long irq_flags;
  1364. spin_lock_irqsave(shost->host_lock, irq_flags);
  1365. scsi_cmd_get_serial(shost, scmd);
  1366. mhba = (struct mvumi_hba *) shost->hostdata;
  1367. scmd->result = 0;
  1368. cmd = mvumi_get_cmd(mhba);
  1369. if (unlikely(!cmd)) {
  1370. spin_unlock_irqrestore(shost->host_lock, irq_flags);
  1371. return SCSI_MLQUEUE_HOST_BUSY;
  1372. }
  1373. if (unlikely(mvumi_build_frame(mhba, scmd, cmd)))
  1374. goto out_return_cmd;
  1375. cmd->scmd = scmd;
  1376. scmd->SCp.ptr = (char *) cmd;
  1377. mhba->instancet->fire_cmd(mhba, cmd);
  1378. spin_unlock_irqrestore(shost->host_lock, irq_flags);
  1379. return 0;
  1380. out_return_cmd:
  1381. mvumi_return_cmd(mhba, cmd);
  1382. scmd->scsi_done(scmd);
  1383. spin_unlock_irqrestore(shost->host_lock, irq_flags);
  1384. return 0;
  1385. }
  1386. static enum blk_eh_timer_return mvumi_timed_out(struct scsi_cmnd *scmd)
  1387. {
  1388. struct mvumi_cmd *cmd = (struct mvumi_cmd *) scmd->SCp.ptr;
  1389. struct Scsi_Host *host = scmd->device->host;
  1390. struct mvumi_hba *mhba = shost_priv(host);
  1391. unsigned long flags;
  1392. spin_lock_irqsave(mhba->shost->host_lock, flags);
  1393. if (mhba->tag_cmd[cmd->frame->tag]) {
  1394. mhba->tag_cmd[cmd->frame->tag] = 0;
  1395. tag_release_one(mhba, &mhba->tag_pool, cmd->frame->tag);
  1396. }
  1397. if (!list_empty(&cmd->queue_pointer))
  1398. list_del_init(&cmd->queue_pointer);
  1399. else
  1400. atomic_dec(&mhba->fw_outstanding);
  1401. scmd->result = (DRIVER_INVALID << 24) | (DID_ABORT << 16);
  1402. scmd->SCp.ptr = NULL;
  1403. if (scsi_bufflen(scmd)) {
  1404. if (scsi_sg_count(scmd)) {
  1405. pci_unmap_sg(mhba->pdev,
  1406. scsi_sglist(scmd),
  1407. scsi_sg_count(scmd),
  1408. (int)scmd->sc_data_direction);
  1409. } else {
  1410. pci_unmap_single(mhba->pdev,
  1411. scmd->SCp.dma_handle,
  1412. scsi_bufflen(scmd),
  1413. (int)scmd->sc_data_direction);
  1414. scmd->SCp.dma_handle = 0;
  1415. }
  1416. }
  1417. mvumi_return_cmd(mhba, cmd);
  1418. spin_unlock_irqrestore(mhba->shost->host_lock, flags);
  1419. return BLK_EH_NOT_HANDLED;
  1420. }
  1421. static int
  1422. mvumi_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1423. sector_t capacity, int geom[])
  1424. {
  1425. int heads, sectors;
  1426. sector_t cylinders;
  1427. unsigned long tmp;
  1428. heads = 64;
  1429. sectors = 32;
  1430. tmp = heads * sectors;
  1431. cylinders = capacity;
  1432. sector_div(cylinders, tmp);
  1433. if (capacity >= 0x200000) {
  1434. heads = 255;
  1435. sectors = 63;
  1436. tmp = heads * sectors;
  1437. cylinders = capacity;
  1438. sector_div(cylinders, tmp);
  1439. }
  1440. geom[0] = heads;
  1441. geom[1] = sectors;
  1442. geom[2] = cylinders;
  1443. return 0;
  1444. }
  1445. static struct scsi_host_template mvumi_template = {
  1446. .module = THIS_MODULE,
  1447. .name = "Marvell Storage Controller",
  1448. .slave_configure = mvumi_slave_configure,
  1449. .queuecommand = mvumi_queue_command,
  1450. .eh_host_reset_handler = mvumi_host_reset,
  1451. .bios_param = mvumi_bios_param,
  1452. .this_id = -1,
  1453. };
  1454. static struct scsi_transport_template mvumi_transport_template = {
  1455. .eh_timed_out = mvumi_timed_out,
  1456. };
  1457. /**
  1458. * mvumi_init_fw - Initializes the FW
  1459. * @mhba: Adapter soft state
  1460. *
  1461. * This is the main function for initializing firmware.
  1462. */
  1463. static int mvumi_init_fw(struct mvumi_hba *mhba)
  1464. {
  1465. int ret = 0;
  1466. if (pci_request_regions(mhba->pdev, MV_DRIVER_NAME)) {
  1467. dev_err(&mhba->pdev->dev, "IO memory region busy!\n");
  1468. return -EBUSY;
  1469. }
  1470. ret = mvumi_map_pci_addr(mhba->pdev, mhba->base_addr);
  1471. if (ret)
  1472. goto fail_ioremap;
  1473. mhba->mmio = mhba->base_addr[0];
  1474. switch (mhba->pdev->device) {
  1475. case PCI_DEVICE_ID_MARVELL_MV9143:
  1476. mhba->instancet = &mvumi_instance_template;
  1477. mhba->io_seq = 0;
  1478. mhba->max_sge = MVUMI_MAX_SG_ENTRY;
  1479. mhba->request_id_enabled = 1;
  1480. break;
  1481. default:
  1482. dev_err(&mhba->pdev->dev, "device 0x%x not supported!\n",
  1483. mhba->pdev->device);
  1484. mhba->instancet = NULL;
  1485. ret = -EINVAL;
  1486. goto fail_alloc_mem;
  1487. }
  1488. dev_dbg(&mhba->pdev->dev, "device id : %04X is found.\n",
  1489. mhba->pdev->device);
  1490. mhba->handshake_page = kzalloc(HSP_MAX_SIZE, GFP_KERNEL);
  1491. if (!mhba->handshake_page) {
  1492. dev_err(&mhba->pdev->dev,
  1493. "failed to allocate memory for handshake\n");
  1494. ret = -ENOMEM;
  1495. goto fail_alloc_mem;
  1496. }
  1497. mhba->handshake_page_phys = virt_to_phys(mhba->handshake_page);
  1498. if (mvumi_start(mhba)) {
  1499. ret = -EINVAL;
  1500. goto fail_ready_state;
  1501. }
  1502. ret = mvumi_alloc_cmds(mhba);
  1503. if (ret)
  1504. goto fail_ready_state;
  1505. return 0;
  1506. fail_ready_state:
  1507. mvumi_release_mem_resource(mhba);
  1508. kfree(mhba->handshake_page);
  1509. fail_alloc_mem:
  1510. mvumi_unmap_pci_addr(mhba->pdev, mhba->base_addr);
  1511. fail_ioremap:
  1512. pci_release_regions(mhba->pdev);
  1513. return ret;
  1514. }
  1515. /**
  1516. * mvumi_io_attach - Attaches this driver to SCSI mid-layer
  1517. * @mhba: Adapter soft state
  1518. */
  1519. static int mvumi_io_attach(struct mvumi_hba *mhba)
  1520. {
  1521. struct Scsi_Host *host = mhba->shost;
  1522. int ret;
  1523. unsigned int max_sg = (mhba->ib_max_size + 4 -
  1524. sizeof(struct mvumi_msg_frame)) / sizeof(struct mvumi_sgl);
  1525. host->irq = mhba->pdev->irq;
  1526. host->unique_id = mhba->unique_id;
  1527. host->can_queue = (mhba->max_io - 1) ? (mhba->max_io - 1) : 1;
  1528. host->sg_tablesize = mhba->max_sge > max_sg ? max_sg : mhba->max_sge;
  1529. host->max_sectors = mhba->max_transfer_size / 512;
  1530. host->cmd_per_lun = (mhba->max_io - 1) ? (mhba->max_io - 1) : 1;
  1531. host->max_id = mhba->max_target_id;
  1532. host->max_cmd_len = MAX_COMMAND_SIZE;
  1533. host->transportt = &mvumi_transport_template;
  1534. ret = scsi_add_host(host, &mhba->pdev->dev);
  1535. if (ret) {
  1536. dev_err(&mhba->pdev->dev, "scsi_add_host failed\n");
  1537. return ret;
  1538. }
  1539. mhba->fw_flag |= MVUMI_FW_ATTACH;
  1540. scsi_scan_host(host);
  1541. return 0;
  1542. }
  1543. /**
  1544. * mvumi_probe_one - PCI hotplug entry point
  1545. * @pdev: PCI device structure
  1546. * @id: PCI ids of supported hotplugged adapter
  1547. */
  1548. static int __devinit mvumi_probe_one(struct pci_dev *pdev,
  1549. const struct pci_device_id *id)
  1550. {
  1551. struct Scsi_Host *host;
  1552. struct mvumi_hba *mhba;
  1553. int ret;
  1554. dev_dbg(&pdev->dev, " %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  1555. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1556. pdev->subsystem_device);
  1557. ret = pci_enable_device(pdev);
  1558. if (ret)
  1559. return ret;
  1560. pci_set_master(pdev);
  1561. if (IS_DMA64) {
  1562. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1563. if (ret) {
  1564. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1565. if (ret)
  1566. goto fail_set_dma_mask;
  1567. }
  1568. } else {
  1569. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1570. if (ret)
  1571. goto fail_set_dma_mask;
  1572. }
  1573. host = scsi_host_alloc(&mvumi_template, sizeof(*mhba));
  1574. if (!host) {
  1575. dev_err(&pdev->dev, "scsi_host_alloc failed\n");
  1576. ret = -ENOMEM;
  1577. goto fail_alloc_instance;
  1578. }
  1579. mhba = shost_priv(host);
  1580. INIT_LIST_HEAD(&mhba->cmd_pool);
  1581. INIT_LIST_HEAD(&mhba->ob_data_list);
  1582. INIT_LIST_HEAD(&mhba->free_ob_list);
  1583. INIT_LIST_HEAD(&mhba->res_list);
  1584. INIT_LIST_HEAD(&mhba->waiting_req_list);
  1585. atomic_set(&mhba->fw_outstanding, 0);
  1586. init_waitqueue_head(&mhba->int_cmd_wait_q);
  1587. mhba->pdev = pdev;
  1588. mhba->shost = host;
  1589. mhba->unique_id = pdev->bus->number << 8 | pdev->devfn;
  1590. ret = mvumi_init_fw(mhba);
  1591. if (ret)
  1592. goto fail_init_fw;
  1593. ret = request_irq(mhba->pdev->irq, mvumi_isr_handler, IRQF_SHARED,
  1594. "mvumi", mhba);
  1595. if (ret) {
  1596. dev_err(&pdev->dev, "failed to register IRQ\n");
  1597. goto fail_init_irq;
  1598. }
  1599. mhba->instancet->enable_intr(mhba->mmio);
  1600. pci_set_drvdata(pdev, mhba);
  1601. ret = mvumi_io_attach(mhba);
  1602. if (ret)
  1603. goto fail_io_attach;
  1604. dev_dbg(&pdev->dev, "probe mvumi driver successfully.\n");
  1605. return 0;
  1606. fail_io_attach:
  1607. pci_set_drvdata(pdev, NULL);
  1608. mhba->instancet->disable_intr(mhba->mmio);
  1609. free_irq(mhba->pdev->irq, mhba);
  1610. fail_init_irq:
  1611. mvumi_release_fw(mhba);
  1612. fail_init_fw:
  1613. scsi_host_put(host);
  1614. fail_alloc_instance:
  1615. fail_set_dma_mask:
  1616. pci_disable_device(pdev);
  1617. return ret;
  1618. }
  1619. static void mvumi_detach_one(struct pci_dev *pdev)
  1620. {
  1621. struct Scsi_Host *host;
  1622. struct mvumi_hba *mhba;
  1623. mhba = pci_get_drvdata(pdev);
  1624. host = mhba->shost;
  1625. scsi_remove_host(mhba->shost);
  1626. mvumi_flush_cache(mhba);
  1627. mhba->instancet->disable_intr(mhba->mmio);
  1628. free_irq(mhba->pdev->irq, mhba);
  1629. mvumi_release_fw(mhba);
  1630. scsi_host_put(host);
  1631. pci_set_drvdata(pdev, NULL);
  1632. pci_disable_device(pdev);
  1633. dev_dbg(&pdev->dev, "driver is removed!\n");
  1634. }
  1635. /**
  1636. * mvumi_shutdown - Shutdown entry point
  1637. * @device: Generic device structure
  1638. */
  1639. static void mvumi_shutdown(struct pci_dev *pdev)
  1640. {
  1641. struct mvumi_hba *mhba = pci_get_drvdata(pdev);
  1642. mvumi_flush_cache(mhba);
  1643. }
  1644. static int mvumi_suspend(struct pci_dev *pdev, pm_message_t state)
  1645. {
  1646. struct mvumi_hba *mhba = NULL;
  1647. mhba = pci_get_drvdata(pdev);
  1648. mvumi_flush_cache(mhba);
  1649. pci_set_drvdata(pdev, mhba);
  1650. mhba->instancet->disable_intr(mhba->mmio);
  1651. free_irq(mhba->pdev->irq, mhba);
  1652. mvumi_unmap_pci_addr(pdev, mhba->base_addr);
  1653. pci_release_regions(pdev);
  1654. pci_save_state(pdev);
  1655. pci_disable_device(pdev);
  1656. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1657. return 0;
  1658. }
  1659. static int mvumi_resume(struct pci_dev *pdev)
  1660. {
  1661. int ret;
  1662. struct mvumi_hba *mhba = NULL;
  1663. mhba = pci_get_drvdata(pdev);
  1664. pci_set_power_state(pdev, PCI_D0);
  1665. pci_enable_wake(pdev, PCI_D0, 0);
  1666. pci_restore_state(pdev);
  1667. ret = pci_enable_device(pdev);
  1668. if (ret) {
  1669. dev_err(&pdev->dev, "enable device failed\n");
  1670. return ret;
  1671. }
  1672. pci_set_master(pdev);
  1673. if (IS_DMA64) {
  1674. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1675. if (ret) {
  1676. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1677. if (ret)
  1678. goto fail;
  1679. }
  1680. } else {
  1681. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1682. if (ret)
  1683. goto fail;
  1684. }
  1685. ret = pci_request_regions(mhba->pdev, MV_DRIVER_NAME);
  1686. if (ret)
  1687. goto fail;
  1688. ret = mvumi_map_pci_addr(mhba->pdev, mhba->base_addr);
  1689. if (ret)
  1690. goto release_regions;
  1691. mhba->mmio = mhba->base_addr[0];
  1692. mvumi_reset(mhba->mmio);
  1693. if (mvumi_start(mhba)) {
  1694. ret = -EINVAL;
  1695. goto unmap_pci_addr;
  1696. }
  1697. ret = request_irq(mhba->pdev->irq, mvumi_isr_handler, IRQF_SHARED,
  1698. "mvumi", mhba);
  1699. if (ret) {
  1700. dev_err(&pdev->dev, "failed to register IRQ\n");
  1701. goto unmap_pci_addr;
  1702. }
  1703. mhba->instancet->enable_intr(mhba->mmio);
  1704. return 0;
  1705. unmap_pci_addr:
  1706. mvumi_unmap_pci_addr(pdev, mhba->base_addr);
  1707. release_regions:
  1708. pci_release_regions(pdev);
  1709. fail:
  1710. pci_disable_device(pdev);
  1711. return ret;
  1712. }
  1713. static struct pci_driver mvumi_pci_driver = {
  1714. .name = MV_DRIVER_NAME,
  1715. .id_table = mvumi_pci_table,
  1716. .probe = mvumi_probe_one,
  1717. .remove = __devexit_p(mvumi_detach_one),
  1718. .shutdown = mvumi_shutdown,
  1719. #ifdef CONFIG_PM
  1720. .suspend = mvumi_suspend,
  1721. .resume = mvumi_resume,
  1722. #endif
  1723. };
  1724. /**
  1725. * mvumi_init - Driver load entry point
  1726. */
  1727. static int __init mvumi_init(void)
  1728. {
  1729. return pci_register_driver(&mvumi_pci_driver);
  1730. }
  1731. /**
  1732. * mvumi_exit - Driver unload entry point
  1733. */
  1734. static void __exit mvumi_exit(void)
  1735. {
  1736. pci_unregister_driver(&mvumi_pci_driver);
  1737. }
  1738. module_init(mvumi_init);
  1739. module_exit(mvumi_exit);