hptiop.c 24 KB

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