hptiop.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * HighPoint RR3xxx controller driver for Linux
  3. * Copyright (C) 2006 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.0 (060426)";
  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_MASK_HOST_BITS) == 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. req = (struct hpt_iop_request_scsi_command *)hba->reqs[tag].req_virt;
  267. dprintk("hptiop_host_request_callback: req=%p, type=%d, "
  268. "result=%d, context=0x%x tag=%d\n",
  269. req, req->header.type, req->header.result,
  270. req->header.context, tag);
  271. BUG_ON(!req->header.result);
  272. BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
  273. scp = hba->reqs[tag].scp;
  274. if (HPT_SCP(scp)->mapped)
  275. scsi_dma_unmap(scp);
  276. switch (le32_to_cpu(req->header.result)) {
  277. case IOP_RESULT_SUCCESS:
  278. scp->result = (DID_OK<<16);
  279. break;
  280. case IOP_RESULT_BAD_TARGET:
  281. scp->result = (DID_BAD_TARGET<<16);
  282. break;
  283. case IOP_RESULT_BUSY:
  284. scp->result = (DID_BUS_BUSY<<16);
  285. break;
  286. case IOP_RESULT_RESET:
  287. scp->result = (DID_RESET<<16);
  288. break;
  289. case IOP_RESULT_FAIL:
  290. scp->result = (DID_ERROR<<16);
  291. break;
  292. case IOP_RESULT_INVALID_REQUEST:
  293. scp->result = (DID_ABORT<<16);
  294. break;
  295. case IOP_RESULT_MODE_SENSE_CHECK_CONDITION:
  296. scp->result = SAM_STAT_CHECK_CONDITION;
  297. memset(&scp->sense_buffer,
  298. 0, sizeof(scp->sense_buffer));
  299. memcpy(&scp->sense_buffer,
  300. &req->sg_list, le32_to_cpu(req->dataxfer_length));
  301. break;
  302. default:
  303. scp->result = ((DRIVER_INVALID|SUGGEST_ABORT)<<24) |
  304. (DID_ABORT<<16);
  305. break;
  306. }
  307. dprintk("scsi_done(%p)\n", scp);
  308. scp->scsi_done(scp);
  309. free_req(hba, &hba->reqs[tag]);
  310. }
  311. void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag)
  312. {
  313. struct hpt_iop_request_header __iomem *req;
  314. struct hpt_iop_request_ioctl_command __iomem *p;
  315. struct hpt_ioctl_k *arg;
  316. req = (struct hpt_iop_request_header __iomem *)
  317. ((unsigned long)hba->iop + tag);
  318. dprintk("hptiop_iop_request_callback: req=%p, type=%d, "
  319. "result=%d, context=0x%x tag=%d\n",
  320. req, readl(&req->type), readl(&req->result),
  321. readl(&req->context), tag);
  322. BUG_ON(!readl(&req->result));
  323. BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
  324. p = (struct hpt_iop_request_ioctl_command __iomem *)req;
  325. arg = (struct hpt_ioctl_k *)(unsigned long)
  326. (readl(&req->context) |
  327. ((u64)readl(&req->context_hi32)<<32));
  328. if (readl(&req->result) == IOP_RESULT_SUCCESS) {
  329. arg->result = HPT_IOCTL_RESULT_OK;
  330. if (arg->outbuf_size)
  331. memcpy_fromio(arg->outbuf,
  332. &p->buf[(readl(&p->inbuf_size) + 3)& ~3],
  333. arg->outbuf_size);
  334. if (arg->bytes_returned)
  335. *arg->bytes_returned = arg->outbuf_size;
  336. }
  337. else
  338. arg->result = HPT_IOCTL_RESULT_FAILED;
  339. arg->done(arg);
  340. writel(tag, &hba->iop->outbound_queue);
  341. }
  342. static irqreturn_t hptiop_intr(int irq, void *dev_id)
  343. {
  344. struct hptiop_hba *hba = dev_id;
  345. int handled;
  346. unsigned long flags;
  347. spin_lock_irqsave(hba->host->host_lock, flags);
  348. handled = __iop_intr(hba);
  349. spin_unlock_irqrestore(hba->host->host_lock, flags);
  350. return handled;
  351. }
  352. static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
  353. {
  354. struct Scsi_Host *host = scp->device->host;
  355. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  356. struct scatterlist *sg;
  357. int idx, nseg;
  358. nseg = scsi_dma_map(scp);
  359. BUG_ON(nseg < 0);
  360. if (!nseg)
  361. return 0;
  362. HPT_SCP(scp)->sgcnt = nseg;
  363. HPT_SCP(scp)->mapped = 1;
  364. BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
  365. scsi_for_each_sg(scp, sg, HPT_SCP(scp)->sgcnt, idx) {
  366. psg[idx].pci_address = cpu_to_le64(sg_dma_address(sg));
  367. psg[idx].size = cpu_to_le32(sg_dma_len(sg));
  368. psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
  369. cpu_to_le32(1) : 0;
  370. }
  371. return HPT_SCP(scp)->sgcnt;
  372. }
  373. static int hptiop_queuecommand(struct scsi_cmnd *scp,
  374. void (*done)(struct scsi_cmnd *))
  375. {
  376. struct Scsi_Host *host = scp->device->host;
  377. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  378. struct hpt_iop_request_scsi_command *req;
  379. int sg_count = 0;
  380. struct hptiop_request *_req;
  381. BUG_ON(!done);
  382. scp->scsi_done = done;
  383. _req = get_req(hba);
  384. if (_req == NULL) {
  385. dprintk("hptiop_queuecmd : no free req\n");
  386. return SCSI_MLQUEUE_HOST_BUSY;
  387. }
  388. _req->scp = scp;
  389. dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%d cdb=(%x-%x-%x) "
  390. "req_index=%d, req=%p\n",
  391. scp,
  392. host->host_no, scp->device->channel,
  393. scp->device->id, scp->device->lun,
  394. *((u32 *)&scp->cmnd),
  395. *((u32 *)&scp->cmnd + 1),
  396. *((u32 *)&scp->cmnd + 2),
  397. _req->index, _req->req_virt);
  398. scp->result = 0;
  399. if (scp->device->channel || scp->device->lun ||
  400. scp->device->id > hba->max_devices) {
  401. scp->result = DID_BAD_TARGET << 16;
  402. free_req(hba, _req);
  403. goto cmd_done;
  404. }
  405. req = (struct hpt_iop_request_scsi_command *)_req->req_virt;
  406. /* build S/G table */
  407. sg_count = hptiop_buildsgl(scp, req->sg_list);
  408. if (!sg_count)
  409. HPT_SCP(scp)->mapped = 0;
  410. req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
  411. req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
  412. req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
  413. req->header.context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
  414. (u32)_req->index);
  415. req->header.context_hi32 = 0;
  416. req->dataxfer_length = cpu_to_le32(scsi_bufflen(scp));
  417. req->channel = scp->device->channel;
  418. req->target = scp->device->id;
  419. req->lun = scp->device->lun;
  420. req->header.size = cpu_to_le32(
  421. sizeof(struct hpt_iop_request_scsi_command)
  422. - sizeof(struct hpt_iopsg)
  423. + sg_count * sizeof(struct hpt_iopsg));
  424. memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
  425. writel(IOPMU_QUEUE_ADDR_HOST_BIT | _req->req_shifted_phy,
  426. &hba->iop->inbound_queue);
  427. return 0;
  428. cmd_done:
  429. dprintk("scsi_done(scp=%p)\n", scp);
  430. scp->scsi_done(scp);
  431. return 0;
  432. }
  433. static const char *hptiop_info(struct Scsi_Host *host)
  434. {
  435. return driver_name_long;
  436. }
  437. static int hptiop_reset_hba(struct hptiop_hba *hba)
  438. {
  439. if (atomic_xchg(&hba->resetting, 1) == 0) {
  440. atomic_inc(&hba->reset_count);
  441. writel(IOPMU_INBOUND_MSG0_RESET,
  442. &hba->iop->inbound_msgaddr0);
  443. hptiop_pci_posting_flush(hba->iop);
  444. }
  445. wait_event_timeout(hba->reset_wq,
  446. atomic_read(&hba->resetting) == 0, 60 * HZ);
  447. if (atomic_read(&hba->resetting)) {
  448. /* IOP is in unkown state, abort reset */
  449. printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
  450. return -1;
  451. }
  452. if (iop_send_sync_msg(hba,
  453. IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
  454. dprintk("scsi%d: fail to start background task\n",
  455. hba->host->host_no);
  456. }
  457. return 0;
  458. }
  459. static int hptiop_reset(struct scsi_cmnd *scp)
  460. {
  461. struct Scsi_Host * host = scp->device->host;
  462. struct hptiop_hba * hba = (struct hptiop_hba *)host->hostdata;
  463. printk(KERN_WARNING "hptiop_reset(%d/%d/%d) scp=%p\n",
  464. scp->device->host->host_no, scp->device->channel,
  465. scp->device->id, scp);
  466. return hptiop_reset_hba(hba)? FAILED : SUCCESS;
  467. }
  468. static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
  469. int queue_depth)
  470. {
  471. if(queue_depth > 256)
  472. queue_depth = 256;
  473. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
  474. return queue_depth;
  475. }
  476. static ssize_t hptiop_show_version(struct class_device *class_dev, char *buf)
  477. {
  478. return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
  479. }
  480. static ssize_t hptiop_show_fw_version(struct class_device *class_dev, char *buf)
  481. {
  482. struct Scsi_Host *host = class_to_shost(class_dev);
  483. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  484. return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
  485. hba->firmware_version >> 24,
  486. (hba->firmware_version >> 16) & 0xff,
  487. (hba->firmware_version >> 8) & 0xff,
  488. hba->firmware_version & 0xff);
  489. }
  490. static struct class_device_attribute hptiop_attr_version = {
  491. .attr = {
  492. .name = "driver-version",
  493. .mode = S_IRUGO,
  494. },
  495. .show = hptiop_show_version,
  496. };
  497. static struct class_device_attribute hptiop_attr_fw_version = {
  498. .attr = {
  499. .name = "firmware-version",
  500. .mode = S_IRUGO,
  501. },
  502. .show = hptiop_show_fw_version,
  503. };
  504. static struct class_device_attribute *hptiop_attrs[] = {
  505. &hptiop_attr_version,
  506. &hptiop_attr_fw_version,
  507. NULL
  508. };
  509. static struct scsi_host_template driver_template = {
  510. .module = THIS_MODULE,
  511. .name = driver_name,
  512. .queuecommand = hptiop_queuecommand,
  513. .eh_device_reset_handler = hptiop_reset,
  514. .eh_bus_reset_handler = hptiop_reset,
  515. .info = hptiop_info,
  516. .unchecked_isa_dma = 0,
  517. .emulated = 0,
  518. .use_clustering = ENABLE_CLUSTERING,
  519. .proc_name = driver_name,
  520. .shost_attrs = hptiop_attrs,
  521. .this_id = -1,
  522. .change_queue_depth = hptiop_adjust_disk_queue_depth,
  523. };
  524. static int __devinit hptiop_probe(struct pci_dev *pcidev,
  525. const struct pci_device_id *id)
  526. {
  527. struct Scsi_Host *host = NULL;
  528. struct hptiop_hba *hba;
  529. struct hpt_iop_request_get_config iop_config;
  530. struct hpt_iop_request_set_config set_config;
  531. dma_addr_t start_phy;
  532. void *start_virt;
  533. u32 offset, i, req_size;
  534. dprintk("hptiop_probe(%p)\n", pcidev);
  535. if (pci_enable_device(pcidev)) {
  536. printk(KERN_ERR "hptiop: fail to enable pci device\n");
  537. return -ENODEV;
  538. }
  539. printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
  540. pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
  541. pcidev->irq);
  542. pci_set_master(pcidev);
  543. /* Enable 64bit DMA if possible */
  544. if (pci_set_dma_mask(pcidev, DMA_64BIT_MASK)) {
  545. if (pci_set_dma_mask(pcidev, DMA_32BIT_MASK)) {
  546. printk(KERN_ERR "hptiop: fail to set dma_mask\n");
  547. goto disable_pci_device;
  548. }
  549. }
  550. if (pci_request_regions(pcidev, driver_name)) {
  551. printk(KERN_ERR "hptiop: pci_request_regions failed\n");
  552. goto disable_pci_device;
  553. }
  554. host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
  555. if (!host) {
  556. printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
  557. goto free_pci_regions;
  558. }
  559. hba = (struct hptiop_hba *)host->hostdata;
  560. hba->pcidev = pcidev;
  561. hba->host = host;
  562. hba->initialized = 0;
  563. atomic_set(&hba->resetting, 0);
  564. atomic_set(&hba->reset_count, 0);
  565. init_waitqueue_head(&hba->reset_wq);
  566. init_waitqueue_head(&hba->ioctl_wq);
  567. host->max_lun = 1;
  568. host->max_channel = 0;
  569. host->io_port = 0;
  570. host->n_io_port = 0;
  571. host->irq = pcidev->irq;
  572. if (hptiop_map_pci_bar(hba))
  573. goto free_scsi_host;
  574. if (iop_wait_ready(hba->iop, 20000)) {
  575. printk(KERN_ERR "scsi%d: firmware not ready\n",
  576. hba->host->host_no);
  577. goto unmap_pci_bar;
  578. }
  579. if (iop_get_config(hba, &iop_config)) {
  580. printk(KERN_ERR "scsi%d: get config failed\n",
  581. hba->host->host_no);
  582. goto unmap_pci_bar;
  583. }
  584. hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
  585. HPTIOP_MAX_REQUESTS);
  586. hba->max_devices = le32_to_cpu(iop_config.max_devices);
  587. hba->max_request_size = le32_to_cpu(iop_config.request_size);
  588. hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
  589. hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
  590. hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
  591. host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
  592. host->max_id = le32_to_cpu(iop_config.max_devices);
  593. host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
  594. host->can_queue = le32_to_cpu(iop_config.max_requests);
  595. host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
  596. host->max_cmd_len = 16;
  597. set_config.vbus_id = cpu_to_le32(host->host_no);
  598. set_config.iop_id = cpu_to_le32(host->host_no);
  599. if (iop_set_config(hba, &set_config)) {
  600. printk(KERN_ERR "scsi%d: set config failed\n",
  601. hba->host->host_no);
  602. goto unmap_pci_bar;
  603. }
  604. pci_set_drvdata(pcidev, host);
  605. if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
  606. driver_name, hba)) {
  607. printk(KERN_ERR "scsi%d: request irq %d failed\n",
  608. hba->host->host_no, pcidev->irq);
  609. goto unmap_pci_bar;
  610. }
  611. /* Allocate request mem */
  612. req_size = sizeof(struct hpt_iop_request_scsi_command)
  613. + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
  614. if ((req_size& 0x1f) != 0)
  615. req_size = (req_size + 0x1f) & ~0x1f;
  616. dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
  617. hba->req_size = req_size;
  618. start_virt = dma_alloc_coherent(&pcidev->dev,
  619. hba->req_size*hba->max_requests + 0x20,
  620. &start_phy, GFP_KERNEL);
  621. if (!start_virt) {
  622. printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
  623. hba->host->host_no);
  624. goto free_request_irq;
  625. }
  626. hba->dma_coherent = start_virt;
  627. hba->dma_coherent_handle = start_phy;
  628. if ((start_phy & 0x1f) != 0)
  629. {
  630. offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
  631. start_phy += offset;
  632. start_virt += offset;
  633. }
  634. hba->req_list = start_virt;
  635. for (i = 0; i < hba->max_requests; i++) {
  636. hba->reqs[i].next = NULL;
  637. hba->reqs[i].req_virt = start_virt;
  638. hba->reqs[i].req_shifted_phy = start_phy >> 5;
  639. hba->reqs[i].index = i;
  640. free_req(hba, &hba->reqs[i]);
  641. start_virt = (char *)start_virt + hba->req_size;
  642. start_phy = start_phy + hba->req_size;
  643. }
  644. /* Enable Interrupt and start background task */
  645. if (hptiop_initialize_iop(hba))
  646. goto free_request_mem;
  647. if (scsi_add_host(host, &pcidev->dev)) {
  648. printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
  649. hba->host->host_no);
  650. goto free_request_mem;
  651. }
  652. scsi_scan_host(host);
  653. dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
  654. return 0;
  655. free_request_mem:
  656. dma_free_coherent(&hba->pcidev->dev,
  657. hba->req_size*hba->max_requests + 0x20,
  658. hba->dma_coherent, hba->dma_coherent_handle);
  659. free_request_irq:
  660. free_irq(hba->pcidev->irq, hba);
  661. unmap_pci_bar:
  662. iounmap(hba->iop);
  663. free_pci_regions:
  664. pci_release_regions(pcidev) ;
  665. free_scsi_host:
  666. scsi_host_put(host);
  667. disable_pci_device:
  668. pci_disable_device(pcidev);
  669. dprintk("scsi%d: hptiop_probe fail\n", host->host_no);
  670. return -ENODEV;
  671. }
  672. static void hptiop_shutdown(struct pci_dev *pcidev)
  673. {
  674. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  675. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  676. struct hpt_iopmu __iomem *iop = hba->iop;
  677. u32 int_mask;
  678. dprintk("hptiop_shutdown(%p)\n", hba);
  679. /* stop the iop */
  680. if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
  681. printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
  682. hba->host->host_no);
  683. /* disable all outbound interrupts */
  684. int_mask = readl(&iop->outbound_intmask);
  685. writel(int_mask |
  686. IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
  687. &iop->outbound_intmask);
  688. hptiop_pci_posting_flush(iop);
  689. }
  690. static void hptiop_remove(struct pci_dev *pcidev)
  691. {
  692. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  693. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  694. dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
  695. scsi_remove_host(host);
  696. hptiop_shutdown(pcidev);
  697. free_irq(hba->pcidev->irq, hba);
  698. dma_free_coherent(&hba->pcidev->dev,
  699. hba->req_size * hba->max_requests + 0x20,
  700. hba->dma_coherent,
  701. hba->dma_coherent_handle);
  702. iounmap(hba->iop);
  703. pci_release_regions(hba->pcidev);
  704. pci_set_drvdata(hba->pcidev, NULL);
  705. pci_disable_device(hba->pcidev);
  706. scsi_host_put(host);
  707. }
  708. static struct pci_device_id hptiop_id_table[] = {
  709. { PCI_DEVICE(0x1103, 0x3220) },
  710. { PCI_DEVICE(0x1103, 0x3320) },
  711. {},
  712. };
  713. MODULE_DEVICE_TABLE(pci, hptiop_id_table);
  714. static struct pci_driver hptiop_pci_driver = {
  715. .name = driver_name,
  716. .id_table = hptiop_id_table,
  717. .probe = hptiop_probe,
  718. .remove = hptiop_remove,
  719. .shutdown = hptiop_shutdown,
  720. };
  721. static int __init hptiop_module_init(void)
  722. {
  723. printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
  724. return pci_register_driver(&hptiop_pci_driver);
  725. }
  726. static void __exit hptiop_module_exit(void)
  727. {
  728. pci_unregister_driver(&hptiop_pci_driver);
  729. }
  730. module_init(hptiop_module_init);
  731. module_exit(hptiop_module_exit);
  732. MODULE_LICENSE("GPL");