hptiop.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*
  2. * HighPoint RR3xxx controller driver for Linux
  3. * Copyright (C) 2006-2007 HighPoint Technologies, Inc. All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * Please report bugs/comments/suggestions to linux@highpoint-tech.com
  15. *
  16. * For more information, visit http://www.highpoint-tech.com
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/errno.h>
  25. #include <linux/delay.h>
  26. #include <linux/timer.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/hdreg.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/io.h>
  31. #include <asm/div64.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsi_host.h>
  37. #include "hptiop.h"
  38. MODULE_AUTHOR("HighPoint Technologies, Inc.");
  39. MODULE_DESCRIPTION("HighPoint RocketRAID 3xxx SATA Controller Driver");
  40. static char driver_name[] = "hptiop";
  41. static const char driver_name_long[] = "RocketRAID 3xxx SATA Controller driver";
  42. static const char driver_ver[] = "v1.2 (070830)";
  43. static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 tag);
  44. static void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag);
  45. static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg);
  46. static inline void hptiop_pci_posting_flush(struct hpt_iopmu __iomem *iop)
  47. {
  48. readl(&iop->outbound_intstatus);
  49. }
  50. static int iop_wait_ready(struct hpt_iopmu __iomem *iop, u32 millisec)
  51. {
  52. u32 req = 0;
  53. int i;
  54. for (i = 0; i < millisec; i++) {
  55. req = readl(&iop->inbound_queue);
  56. if (req != IOPMU_QUEUE_EMPTY)
  57. break;
  58. msleep(1);
  59. }
  60. if (req != IOPMU_QUEUE_EMPTY) {
  61. writel(req, &iop->outbound_queue);
  62. hptiop_pci_posting_flush(iop);
  63. return 0;
  64. }
  65. return -1;
  66. }
  67. static void hptiop_request_callback(struct hptiop_hba *hba, u32 tag)
  68. {
  69. if (tag & IOPMU_QUEUE_ADDR_HOST_BIT)
  70. return hptiop_host_request_callback(hba,
  71. tag & ~IOPMU_QUEUE_ADDR_HOST_BIT);
  72. else
  73. return hptiop_iop_request_callback(hba, tag);
  74. }
  75. static inline void hptiop_drain_outbound_queue(struct hptiop_hba *hba)
  76. {
  77. u32 req;
  78. while ((req = readl(&hba->iop->outbound_queue)) != IOPMU_QUEUE_EMPTY) {
  79. if (req & IOPMU_QUEUE_MASK_HOST_BITS)
  80. hptiop_request_callback(hba, req);
  81. else {
  82. struct hpt_iop_request_header __iomem * p;
  83. p = (struct hpt_iop_request_header __iomem *)
  84. ((char __iomem *)hba->iop + req);
  85. if (readl(&p->flags) & IOP_REQUEST_FLAG_SYNC_REQUEST) {
  86. if (readl(&p->context))
  87. hptiop_request_callback(hba, req);
  88. else
  89. writel(1, &p->context);
  90. }
  91. else
  92. hptiop_request_callback(hba, req);
  93. }
  94. }
  95. }
  96. static int __iop_intr(struct hptiop_hba *hba)
  97. {
  98. struct hpt_iopmu __iomem *iop = hba->iop;
  99. u32 status;
  100. int ret = 0;
  101. status = readl(&iop->outbound_intstatus);
  102. if (status & IOPMU_OUTBOUND_INT_MSG0) {
  103. u32 msg = readl(&iop->outbound_msgaddr0);
  104. dprintk("received outbound msg %x\n", msg);
  105. writel(IOPMU_OUTBOUND_INT_MSG0, &iop->outbound_intstatus);
  106. hptiop_message_callback(hba, msg);
  107. ret = 1;
  108. }
  109. if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
  110. hptiop_drain_outbound_queue(hba);
  111. ret = 1;
  112. }
  113. return ret;
  114. }
  115. static int iop_send_sync_request(struct hptiop_hba *hba,
  116. void __iomem *_req, u32 millisec)
  117. {
  118. struct hpt_iop_request_header __iomem *req = _req;
  119. u32 i;
  120. writel(readl(&req->flags) | IOP_REQUEST_FLAG_SYNC_REQUEST,
  121. &req->flags);
  122. writel(0, &req->context);
  123. writel((unsigned long)req - (unsigned long)hba->iop,
  124. &hba->iop->inbound_queue);
  125. hptiop_pci_posting_flush(hba->iop);
  126. for (i = 0; i < millisec; i++) {
  127. __iop_intr(hba);
  128. if (readl(&req->context))
  129. return 0;
  130. msleep(1);
  131. }
  132. return -1;
  133. }
  134. static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec)
  135. {
  136. u32 i;
  137. hba->msg_done = 0;
  138. writel(msg, &hba->iop->inbound_msgaddr0);
  139. hptiop_pci_posting_flush(hba->iop);
  140. for (i = 0; i < millisec; i++) {
  141. spin_lock_irq(hba->host->host_lock);
  142. __iop_intr(hba);
  143. spin_unlock_irq(hba->host->host_lock);
  144. if (hba->msg_done)
  145. break;
  146. msleep(1);
  147. }
  148. return hba->msg_done? 0 : -1;
  149. }
  150. static int iop_get_config(struct hptiop_hba *hba,
  151. struct hpt_iop_request_get_config *config)
  152. {
  153. u32 req32;
  154. struct hpt_iop_request_get_config __iomem *req;
  155. req32 = readl(&hba->iop->inbound_queue);
  156. if (req32 == IOPMU_QUEUE_EMPTY)
  157. return -1;
  158. req = (struct hpt_iop_request_get_config __iomem *)
  159. ((unsigned long)hba->iop + req32);
  160. writel(0, &req->header.flags);
  161. writel(IOP_REQUEST_TYPE_GET_CONFIG, &req->header.type);
  162. writel(sizeof(struct hpt_iop_request_get_config), &req->header.size);
  163. writel(IOP_RESULT_PENDING, &req->header.result);
  164. if (iop_send_sync_request(hba, req, 20000)) {
  165. dprintk("Get config send cmd failed\n");
  166. return -1;
  167. }
  168. memcpy_fromio(config, req, sizeof(*config));
  169. writel(req32, &hba->iop->outbound_queue);
  170. return 0;
  171. }
  172. static int iop_set_config(struct hptiop_hba *hba,
  173. struct hpt_iop_request_set_config *config)
  174. {
  175. u32 req32;
  176. struct hpt_iop_request_set_config __iomem *req;
  177. req32 = readl(&hba->iop->inbound_queue);
  178. if (req32 == IOPMU_QUEUE_EMPTY)
  179. return -1;
  180. req = (struct hpt_iop_request_set_config __iomem *)
  181. ((unsigned long)hba->iop + req32);
  182. memcpy_toio((u8 __iomem *)req + sizeof(struct hpt_iop_request_header),
  183. (u8 *)config + sizeof(struct hpt_iop_request_header),
  184. sizeof(struct hpt_iop_request_set_config) -
  185. sizeof(struct hpt_iop_request_header));
  186. writel(0, &req->header.flags);
  187. writel(IOP_REQUEST_TYPE_SET_CONFIG, &req->header.type);
  188. writel(sizeof(struct hpt_iop_request_set_config), &req->header.size);
  189. writel(IOP_RESULT_PENDING, &req->header.result);
  190. if (iop_send_sync_request(hba, req, 20000)) {
  191. dprintk("Set config send cmd failed\n");
  192. return -1;
  193. }
  194. writel(req32, &hba->iop->outbound_queue);
  195. return 0;
  196. }
  197. static int hptiop_initialize_iop(struct hptiop_hba *hba)
  198. {
  199. struct hpt_iopmu __iomem *iop = hba->iop;
  200. /* enable interrupts */
  201. writel(~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0),
  202. &iop->outbound_intmask);
  203. hba->initialized = 1;
  204. /* start background tasks */
  205. if (iop_send_sync_msg(hba,
  206. IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
  207. printk(KERN_ERR "scsi%d: fail to start background task\n",
  208. hba->host->host_no);
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. static int hptiop_map_pci_bar(struct hptiop_hba *hba)
  214. {
  215. u32 mem_base_phy, length;
  216. void __iomem *mem_base_virt;
  217. struct pci_dev *pcidev = hba->pcidev;
  218. if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM)) {
  219. printk(KERN_ERR "scsi%d: pci resource invalid\n",
  220. hba->host->host_no);
  221. return -1;
  222. }
  223. mem_base_phy = pci_resource_start(pcidev, 0);
  224. length = pci_resource_len(pcidev, 0);
  225. mem_base_virt = ioremap(mem_base_phy, length);
  226. if (!mem_base_virt) {
  227. printk(KERN_ERR "scsi%d: Fail to ioremap memory space\n",
  228. hba->host->host_no);
  229. return -1;
  230. }
  231. hba->iop = mem_base_virt;
  232. dprintk("hptiop_map_pci_bar: iop=%p\n", hba->iop);
  233. return 0;
  234. }
  235. static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg)
  236. {
  237. dprintk("iop message 0x%x\n", msg);
  238. if (!hba->initialized)
  239. return;
  240. if (msg == IOPMU_INBOUND_MSG0_RESET) {
  241. atomic_set(&hba->resetting, 0);
  242. wake_up(&hba->reset_wq);
  243. }
  244. else if (msg <= IOPMU_INBOUND_MSG0_MAX)
  245. hba->msg_done = 1;
  246. }
  247. static inline struct hptiop_request *get_req(struct hptiop_hba *hba)
  248. {
  249. struct hptiop_request *ret;
  250. dprintk("get_req : req=%p\n", hba->req_list);
  251. ret = hba->req_list;
  252. if (ret)
  253. hba->req_list = ret->next;
  254. return ret;
  255. }
  256. static inline void free_req(struct hptiop_hba *hba, struct hptiop_request *req)
  257. {
  258. dprintk("free_req(%d, %p)\n", req->index, req);
  259. req->next = hba->req_list;
  260. hba->req_list = req;
  261. }
  262. static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 _tag)
  263. {
  264. struct hpt_iop_request_scsi_command *req;
  265. struct scsi_cmnd *scp;
  266. u32 tag;
  267. if (hba->iopintf_v2) {
  268. tag = _tag & ~ IOPMU_QUEUE_REQUEST_RESULT_BIT;
  269. req = hba->reqs[tag].req_virt;
  270. if (likely(_tag & IOPMU_QUEUE_REQUEST_RESULT_BIT))
  271. req->header.result = IOP_RESULT_SUCCESS;
  272. } else {
  273. tag = _tag;
  274. req = hba->reqs[tag].req_virt;
  275. }
  276. dprintk("hptiop_host_request_callback: req=%p, type=%d, "
  277. "result=%d, context=0x%x tag=%d\n",
  278. req, req->header.type, req->header.result,
  279. req->header.context, tag);
  280. BUG_ON(!req->header.result);
  281. BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
  282. scp = hba->reqs[tag].scp;
  283. if (HPT_SCP(scp)->mapped)
  284. scsi_dma_unmap(scp);
  285. switch (le32_to_cpu(req->header.result)) {
  286. case IOP_RESULT_SUCCESS:
  287. scp->result = (DID_OK<<16);
  288. break;
  289. case IOP_RESULT_BAD_TARGET:
  290. scp->result = (DID_BAD_TARGET<<16);
  291. break;
  292. case IOP_RESULT_BUSY:
  293. scp->result = (DID_BUS_BUSY<<16);
  294. break;
  295. case IOP_RESULT_RESET:
  296. scp->result = (DID_RESET<<16);
  297. break;
  298. case IOP_RESULT_FAIL:
  299. scp->result = (DID_ERROR<<16);
  300. break;
  301. case IOP_RESULT_INVALID_REQUEST:
  302. scp->result = (DID_ABORT<<16);
  303. break;
  304. case IOP_RESULT_MODE_SENSE_CHECK_CONDITION:
  305. scp->result = SAM_STAT_CHECK_CONDITION;
  306. memset(&scp->sense_buffer,
  307. 0, sizeof(scp->sense_buffer));
  308. memcpy(&scp->sense_buffer,
  309. &req->sg_list, le32_to_cpu(req->dataxfer_length));
  310. break;
  311. default:
  312. scp->result = ((DRIVER_INVALID|SUGGEST_ABORT)<<24) |
  313. (DID_ABORT<<16);
  314. break;
  315. }
  316. dprintk("scsi_done(%p)\n", scp);
  317. scp->scsi_done(scp);
  318. free_req(hba, &hba->reqs[tag]);
  319. }
  320. void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag)
  321. {
  322. struct hpt_iop_request_header __iomem *req;
  323. struct hpt_iop_request_ioctl_command __iomem *p;
  324. struct hpt_ioctl_k *arg;
  325. req = (struct hpt_iop_request_header __iomem *)
  326. ((unsigned long)hba->iop + tag);
  327. dprintk("hptiop_iop_request_callback: req=%p, type=%d, "
  328. "result=%d, context=0x%x tag=%d\n",
  329. req, readl(&req->type), readl(&req->result),
  330. readl(&req->context), tag);
  331. BUG_ON(!readl(&req->result));
  332. BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
  333. p = (struct hpt_iop_request_ioctl_command __iomem *)req;
  334. arg = (struct hpt_ioctl_k *)(unsigned long)
  335. (readl(&req->context) |
  336. ((u64)readl(&req->context_hi32)<<32));
  337. if (readl(&req->result) == IOP_RESULT_SUCCESS) {
  338. arg->result = HPT_IOCTL_RESULT_OK;
  339. if (arg->outbuf_size)
  340. memcpy_fromio(arg->outbuf,
  341. &p->buf[(readl(&p->inbuf_size) + 3)& ~3],
  342. arg->outbuf_size);
  343. if (arg->bytes_returned)
  344. *arg->bytes_returned = arg->outbuf_size;
  345. }
  346. else
  347. arg->result = HPT_IOCTL_RESULT_FAILED;
  348. arg->done(arg);
  349. writel(tag, &hba->iop->outbound_queue);
  350. }
  351. static irqreturn_t hptiop_intr(int irq, void *dev_id)
  352. {
  353. struct hptiop_hba *hba = dev_id;
  354. int handled;
  355. unsigned long flags;
  356. spin_lock_irqsave(hba->host->host_lock, flags);
  357. handled = __iop_intr(hba);
  358. spin_unlock_irqrestore(hba->host->host_lock, flags);
  359. return handled;
  360. }
  361. static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
  362. {
  363. struct Scsi_Host *host = scp->device->host;
  364. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  365. struct scatterlist *sg;
  366. int idx, nseg;
  367. nseg = scsi_dma_map(scp);
  368. BUG_ON(nseg < 0);
  369. if (!nseg)
  370. return 0;
  371. HPT_SCP(scp)->sgcnt = nseg;
  372. HPT_SCP(scp)->mapped = 1;
  373. BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
  374. scsi_for_each_sg(scp, sg, HPT_SCP(scp)->sgcnt, idx) {
  375. psg[idx].pci_address = cpu_to_le64(sg_dma_address(sg));
  376. psg[idx].size = cpu_to_le32(sg_dma_len(sg));
  377. psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
  378. cpu_to_le32(1) : 0;
  379. }
  380. return HPT_SCP(scp)->sgcnt;
  381. }
  382. static int hptiop_queuecommand(struct scsi_cmnd *scp,
  383. void (*done)(struct scsi_cmnd *))
  384. {
  385. struct Scsi_Host *host = scp->device->host;
  386. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  387. struct hpt_iop_request_scsi_command *req;
  388. int sg_count = 0;
  389. struct hptiop_request *_req;
  390. BUG_ON(!done);
  391. scp->scsi_done = done;
  392. _req = get_req(hba);
  393. if (_req == NULL) {
  394. dprintk("hptiop_queuecmd : no free req\n");
  395. return SCSI_MLQUEUE_HOST_BUSY;
  396. }
  397. _req->scp = scp;
  398. dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%d cdb=(%x-%x-%x) "
  399. "req_index=%d, req=%p\n",
  400. scp,
  401. host->host_no, scp->device->channel,
  402. scp->device->id, scp->device->lun,
  403. *((u32 *)&scp->cmnd),
  404. *((u32 *)&scp->cmnd + 1),
  405. *((u32 *)&scp->cmnd + 2),
  406. _req->index, _req->req_virt);
  407. scp->result = 0;
  408. if (scp->device->channel || scp->device->lun ||
  409. scp->device->id > hba->max_devices) {
  410. scp->result = DID_BAD_TARGET << 16;
  411. free_req(hba, _req);
  412. goto cmd_done;
  413. }
  414. req = _req->req_virt;
  415. /* build S/G table */
  416. sg_count = hptiop_buildsgl(scp, req->sg_list);
  417. if (!sg_count)
  418. HPT_SCP(scp)->mapped = 0;
  419. req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
  420. req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
  421. req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
  422. req->header.context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
  423. (u32)_req->index);
  424. req->header.context_hi32 = 0;
  425. req->dataxfer_length = cpu_to_le32(scsi_bufflen(scp));
  426. req->channel = scp->device->channel;
  427. req->target = scp->device->id;
  428. req->lun = scp->device->lun;
  429. req->header.size = cpu_to_le32(
  430. sizeof(struct hpt_iop_request_scsi_command)
  431. - sizeof(struct hpt_iopsg)
  432. + sg_count * sizeof(struct hpt_iopsg));
  433. memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
  434. if (hba->iopintf_v2) {
  435. u32 size_bits;
  436. if (req->header.size < 256)
  437. size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT;
  438. else if (req->header.size < 512)
  439. size_bits = IOPMU_QUEUE_ADDR_HOST_BIT;
  440. else
  441. size_bits = IOPMU_QUEUE_REQUEST_SIZE_BIT |
  442. IOPMU_QUEUE_ADDR_HOST_BIT;
  443. writel(_req->req_shifted_phy | size_bits, &hba->iop->inbound_queue);
  444. } else
  445. writel(_req->req_shifted_phy | IOPMU_QUEUE_ADDR_HOST_BIT,
  446. &hba->iop->inbound_queue);
  447. return 0;
  448. cmd_done:
  449. dprintk("scsi_done(scp=%p)\n", scp);
  450. scp->scsi_done(scp);
  451. return 0;
  452. }
  453. static const char *hptiop_info(struct Scsi_Host *host)
  454. {
  455. return driver_name_long;
  456. }
  457. static int hptiop_reset_hba(struct hptiop_hba *hba)
  458. {
  459. if (atomic_xchg(&hba->resetting, 1) == 0) {
  460. atomic_inc(&hba->reset_count);
  461. writel(IOPMU_INBOUND_MSG0_RESET,
  462. &hba->iop->inbound_msgaddr0);
  463. hptiop_pci_posting_flush(hba->iop);
  464. }
  465. wait_event_timeout(hba->reset_wq,
  466. atomic_read(&hba->resetting) == 0, 60 * HZ);
  467. if (atomic_read(&hba->resetting)) {
  468. /* IOP is in unkown state, abort reset */
  469. printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
  470. return -1;
  471. }
  472. if (iop_send_sync_msg(hba,
  473. IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
  474. dprintk("scsi%d: fail to start background task\n",
  475. hba->host->host_no);
  476. }
  477. return 0;
  478. }
  479. static int hptiop_reset(struct scsi_cmnd *scp)
  480. {
  481. struct Scsi_Host * host = scp->device->host;
  482. struct hptiop_hba * hba = (struct hptiop_hba *)host->hostdata;
  483. printk(KERN_WARNING "hptiop_reset(%d/%d/%d) scp=%p\n",
  484. scp->device->host->host_no, scp->device->channel,
  485. scp->device->id, scp);
  486. return hptiop_reset_hba(hba)? FAILED : SUCCESS;
  487. }
  488. static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
  489. int queue_depth)
  490. {
  491. if(queue_depth > 256)
  492. queue_depth = 256;
  493. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
  494. return queue_depth;
  495. }
  496. static ssize_t hptiop_show_version(struct class_device *class_dev, char *buf)
  497. {
  498. return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
  499. }
  500. static ssize_t hptiop_show_fw_version(struct class_device *class_dev, char *buf)
  501. {
  502. struct Scsi_Host *host = class_to_shost(class_dev);
  503. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  504. return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
  505. hba->firmware_version >> 24,
  506. (hba->firmware_version >> 16) & 0xff,
  507. (hba->firmware_version >> 8) & 0xff,
  508. hba->firmware_version & 0xff);
  509. }
  510. static struct class_device_attribute hptiop_attr_version = {
  511. .attr = {
  512. .name = "driver-version",
  513. .mode = S_IRUGO,
  514. },
  515. .show = hptiop_show_version,
  516. };
  517. static struct class_device_attribute hptiop_attr_fw_version = {
  518. .attr = {
  519. .name = "firmware-version",
  520. .mode = S_IRUGO,
  521. },
  522. .show = hptiop_show_fw_version,
  523. };
  524. static struct class_device_attribute *hptiop_attrs[] = {
  525. &hptiop_attr_version,
  526. &hptiop_attr_fw_version,
  527. NULL
  528. };
  529. static struct scsi_host_template driver_template = {
  530. .module = THIS_MODULE,
  531. .name = driver_name,
  532. .queuecommand = hptiop_queuecommand,
  533. .eh_device_reset_handler = hptiop_reset,
  534. .eh_bus_reset_handler = hptiop_reset,
  535. .info = hptiop_info,
  536. .unchecked_isa_dma = 0,
  537. .emulated = 0,
  538. .use_clustering = ENABLE_CLUSTERING,
  539. .use_sg_chaining = ENABLE_SG_CHAINING,
  540. .proc_name = driver_name,
  541. .shost_attrs = hptiop_attrs,
  542. .this_id = -1,
  543. .change_queue_depth = hptiop_adjust_disk_queue_depth,
  544. };
  545. static int __devinit hptiop_probe(struct pci_dev *pcidev,
  546. const struct pci_device_id *id)
  547. {
  548. struct Scsi_Host *host = NULL;
  549. struct hptiop_hba *hba;
  550. struct hpt_iop_request_get_config iop_config;
  551. struct hpt_iop_request_set_config set_config;
  552. dma_addr_t start_phy;
  553. void *start_virt;
  554. u32 offset, i, req_size;
  555. dprintk("hptiop_probe(%p)\n", pcidev);
  556. if (pci_enable_device(pcidev)) {
  557. printk(KERN_ERR "hptiop: fail to enable pci device\n");
  558. return -ENODEV;
  559. }
  560. printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
  561. pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
  562. pcidev->irq);
  563. pci_set_master(pcidev);
  564. /* Enable 64bit DMA if possible */
  565. if (pci_set_dma_mask(pcidev, DMA_64BIT_MASK)) {
  566. if (pci_set_dma_mask(pcidev, DMA_32BIT_MASK)) {
  567. printk(KERN_ERR "hptiop: fail to set dma_mask\n");
  568. goto disable_pci_device;
  569. }
  570. }
  571. if (pci_request_regions(pcidev, driver_name)) {
  572. printk(KERN_ERR "hptiop: pci_request_regions failed\n");
  573. goto disable_pci_device;
  574. }
  575. host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
  576. if (!host) {
  577. printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
  578. goto free_pci_regions;
  579. }
  580. hba = (struct hptiop_hba *)host->hostdata;
  581. hba->pcidev = pcidev;
  582. hba->host = host;
  583. hba->initialized = 0;
  584. hba->iopintf_v2 = 0;
  585. atomic_set(&hba->resetting, 0);
  586. atomic_set(&hba->reset_count, 0);
  587. init_waitqueue_head(&hba->reset_wq);
  588. init_waitqueue_head(&hba->ioctl_wq);
  589. host->max_lun = 1;
  590. host->max_channel = 0;
  591. host->io_port = 0;
  592. host->n_io_port = 0;
  593. host->irq = pcidev->irq;
  594. if (hptiop_map_pci_bar(hba))
  595. goto free_scsi_host;
  596. if (iop_wait_ready(hba->iop, 20000)) {
  597. printk(KERN_ERR "scsi%d: firmware not ready\n",
  598. hba->host->host_no);
  599. goto unmap_pci_bar;
  600. }
  601. if (iop_get_config(hba, &iop_config)) {
  602. printk(KERN_ERR "scsi%d: get config failed\n",
  603. hba->host->host_no);
  604. goto unmap_pci_bar;
  605. }
  606. hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
  607. HPTIOP_MAX_REQUESTS);
  608. hba->max_devices = le32_to_cpu(iop_config.max_devices);
  609. hba->max_request_size = le32_to_cpu(iop_config.request_size);
  610. hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
  611. hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
  612. hba->interface_version = le32_to_cpu(iop_config.interface_version);
  613. hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
  614. if (hba->firmware_version > 0x01020000 ||
  615. hba->interface_version > 0x01020000)
  616. hba->iopintf_v2 = 1;
  617. host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
  618. host->max_id = le32_to_cpu(iop_config.max_devices);
  619. host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
  620. host->can_queue = le32_to_cpu(iop_config.max_requests);
  621. host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
  622. host->max_cmd_len = 16;
  623. req_size = sizeof(struct hpt_iop_request_scsi_command)
  624. + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
  625. if ((req_size & 0x1f) != 0)
  626. req_size = (req_size + 0x1f) & ~0x1f;
  627. memset(&set_config, 0, sizeof(struct hpt_iop_request_set_config));
  628. set_config.iop_id = cpu_to_le32(host->host_no);
  629. set_config.vbus_id = cpu_to_le16(host->host_no);
  630. set_config.max_host_request_size = cpu_to_le16(req_size);
  631. if (iop_set_config(hba, &set_config)) {
  632. printk(KERN_ERR "scsi%d: set config failed\n",
  633. hba->host->host_no);
  634. goto unmap_pci_bar;
  635. }
  636. pci_set_drvdata(pcidev, host);
  637. if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
  638. driver_name, hba)) {
  639. printk(KERN_ERR "scsi%d: request irq %d failed\n",
  640. hba->host->host_no, pcidev->irq);
  641. goto unmap_pci_bar;
  642. }
  643. /* Allocate request mem */
  644. dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
  645. hba->req_size = req_size;
  646. start_virt = dma_alloc_coherent(&pcidev->dev,
  647. hba->req_size*hba->max_requests + 0x20,
  648. &start_phy, GFP_KERNEL);
  649. if (!start_virt) {
  650. printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
  651. hba->host->host_no);
  652. goto free_request_irq;
  653. }
  654. hba->dma_coherent = start_virt;
  655. hba->dma_coherent_handle = start_phy;
  656. if ((start_phy & 0x1f) != 0)
  657. {
  658. offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
  659. start_phy += offset;
  660. start_virt += offset;
  661. }
  662. hba->req_list = start_virt;
  663. for (i = 0; i < hba->max_requests; i++) {
  664. hba->reqs[i].next = NULL;
  665. hba->reqs[i].req_virt = start_virt;
  666. hba->reqs[i].req_shifted_phy = start_phy >> 5;
  667. hba->reqs[i].index = i;
  668. free_req(hba, &hba->reqs[i]);
  669. start_virt = (char *)start_virt + hba->req_size;
  670. start_phy = start_phy + hba->req_size;
  671. }
  672. /* Enable Interrupt and start background task */
  673. if (hptiop_initialize_iop(hba))
  674. goto free_request_mem;
  675. if (scsi_add_host(host, &pcidev->dev)) {
  676. printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
  677. hba->host->host_no);
  678. goto free_request_mem;
  679. }
  680. scsi_scan_host(host);
  681. dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
  682. return 0;
  683. free_request_mem:
  684. dma_free_coherent(&hba->pcidev->dev,
  685. hba->req_size*hba->max_requests + 0x20,
  686. hba->dma_coherent, hba->dma_coherent_handle);
  687. free_request_irq:
  688. free_irq(hba->pcidev->irq, hba);
  689. unmap_pci_bar:
  690. iounmap(hba->iop);
  691. free_pci_regions:
  692. pci_release_regions(pcidev) ;
  693. free_scsi_host:
  694. scsi_host_put(host);
  695. disable_pci_device:
  696. pci_disable_device(pcidev);
  697. dprintk("scsi%d: hptiop_probe fail\n", host->host_no);
  698. return -ENODEV;
  699. }
  700. static void hptiop_shutdown(struct pci_dev *pcidev)
  701. {
  702. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  703. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  704. struct hpt_iopmu __iomem *iop = hba->iop;
  705. u32 int_mask;
  706. dprintk("hptiop_shutdown(%p)\n", hba);
  707. /* stop the iop */
  708. if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
  709. printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
  710. hba->host->host_no);
  711. /* disable all outbound interrupts */
  712. int_mask = readl(&iop->outbound_intmask);
  713. writel(int_mask |
  714. IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
  715. &iop->outbound_intmask);
  716. hptiop_pci_posting_flush(iop);
  717. }
  718. static void hptiop_remove(struct pci_dev *pcidev)
  719. {
  720. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  721. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  722. dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
  723. scsi_remove_host(host);
  724. hptiop_shutdown(pcidev);
  725. free_irq(hba->pcidev->irq, hba);
  726. dma_free_coherent(&hba->pcidev->dev,
  727. hba->req_size * hba->max_requests + 0x20,
  728. hba->dma_coherent,
  729. hba->dma_coherent_handle);
  730. iounmap(hba->iop);
  731. pci_release_regions(hba->pcidev);
  732. pci_set_drvdata(hba->pcidev, NULL);
  733. pci_disable_device(hba->pcidev);
  734. scsi_host_put(host);
  735. }
  736. static struct pci_device_id hptiop_id_table[] = {
  737. { PCI_VDEVICE(TTI, 0x3220) },
  738. { PCI_VDEVICE(TTI, 0x3320) },
  739. { PCI_VDEVICE(TTI, 0x3520) },
  740. { PCI_VDEVICE(TTI, 0x4320) },
  741. {},
  742. };
  743. MODULE_DEVICE_TABLE(pci, hptiop_id_table);
  744. static struct pci_driver hptiop_pci_driver = {
  745. .name = driver_name,
  746. .id_table = hptiop_id_table,
  747. .probe = hptiop_probe,
  748. .remove = hptiop_remove,
  749. .shutdown = hptiop_shutdown,
  750. };
  751. static int __init hptiop_module_init(void)
  752. {
  753. printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
  754. return pci_register_driver(&hptiop_pci_driver);
  755. }
  756. static void __exit hptiop_module_exit(void)
  757. {
  758. pci_unregister_driver(&hptiop_pci_driver);
  759. }
  760. module_init(hptiop_module_init);
  761. module_exit(hptiop_module_exit);
  762. MODULE_LICENSE("GPL");