hptiop.c 24 KB

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