hptiop.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 DEFINE_SPINLOCK(hptiop_hba_list_lock);
  44. static LIST_HEAD(hptiop_hba_list);
  45. static int hptiop_cdev_major = -1;
  46. static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 tag);
  47. static void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag);
  48. static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg);
  49. static inline void hptiop_pci_posting_flush(struct hpt_iopmu __iomem *iop)
  50. {
  51. readl(&iop->outbound_intstatus);
  52. }
  53. static int iop_wait_ready(struct hpt_iopmu __iomem *iop, u32 millisec)
  54. {
  55. u32 req = 0;
  56. int i;
  57. for (i = 0; i < millisec; i++) {
  58. req = readl(&iop->inbound_queue);
  59. if (req != IOPMU_QUEUE_EMPTY)
  60. break;
  61. msleep(1);
  62. }
  63. if (req != IOPMU_QUEUE_EMPTY) {
  64. writel(req, &iop->outbound_queue);
  65. hptiop_pci_posting_flush(iop);
  66. return 0;
  67. }
  68. return -1;
  69. }
  70. static void hptiop_request_callback(struct hptiop_hba *hba, u32 tag)
  71. {
  72. if ((tag & IOPMU_QUEUE_MASK_HOST_BITS) == IOPMU_QUEUE_ADDR_HOST_BIT)
  73. return hptiop_host_request_callback(hba,
  74. tag & ~IOPMU_QUEUE_ADDR_HOST_BIT);
  75. else
  76. return hptiop_iop_request_callback(hba, tag);
  77. }
  78. static inline void hptiop_drain_outbound_queue(struct hptiop_hba *hba)
  79. {
  80. u32 req;
  81. while ((req = readl(&hba->iop->outbound_queue)) != IOPMU_QUEUE_EMPTY) {
  82. if (req & IOPMU_QUEUE_MASK_HOST_BITS)
  83. hptiop_request_callback(hba, req);
  84. else {
  85. struct hpt_iop_request_header __iomem * p;
  86. p = (struct hpt_iop_request_header __iomem *)
  87. ((char __iomem *)hba->iop + req);
  88. if (readl(&p->flags) & IOP_REQUEST_FLAG_SYNC_REQUEST) {
  89. if (readl(&p->context))
  90. hptiop_request_callback(hba, req);
  91. else
  92. writel(1, &p->context);
  93. }
  94. else
  95. hptiop_request_callback(hba, req);
  96. }
  97. }
  98. }
  99. static int __iop_intr(struct hptiop_hba *hba)
  100. {
  101. struct hpt_iopmu __iomem *iop = hba->iop;
  102. u32 status;
  103. int ret = 0;
  104. status = readl(&iop->outbound_intstatus);
  105. if (status & IOPMU_OUTBOUND_INT_MSG0) {
  106. u32 msg = readl(&iop->outbound_msgaddr0);
  107. dprintk("received outbound msg %x\n", msg);
  108. writel(IOPMU_OUTBOUND_INT_MSG0, &iop->outbound_intstatus);
  109. hptiop_message_callback(hba, msg);
  110. ret = 1;
  111. }
  112. if (status & IOPMU_OUTBOUND_INT_POSTQUEUE) {
  113. hptiop_drain_outbound_queue(hba);
  114. ret = 1;
  115. }
  116. return ret;
  117. }
  118. static int iop_send_sync_request(struct hptiop_hba *hba,
  119. void __iomem *_req, u32 millisec)
  120. {
  121. struct hpt_iop_request_header __iomem *req = _req;
  122. u32 i;
  123. writel(readl(&req->flags) | IOP_REQUEST_FLAG_SYNC_REQUEST,
  124. &req->flags);
  125. writel(0, &req->context);
  126. writel((unsigned long)req - (unsigned long)hba->iop,
  127. &hba->iop->inbound_queue);
  128. hptiop_pci_posting_flush(hba->iop);
  129. for (i = 0; i < millisec; i++) {
  130. __iop_intr(hba);
  131. if (readl(&req->context))
  132. return 0;
  133. msleep(1);
  134. }
  135. return -1;
  136. }
  137. static int iop_send_sync_msg(struct hptiop_hba *hba, u32 msg, u32 millisec)
  138. {
  139. u32 i;
  140. hba->msg_done = 0;
  141. writel(msg, &hba->iop->inbound_msgaddr0);
  142. hptiop_pci_posting_flush(hba->iop);
  143. for (i = 0; i < millisec; i++) {
  144. spin_lock_irq(hba->host->host_lock);
  145. __iop_intr(hba);
  146. spin_unlock_irq(hba->host->host_lock);
  147. if (hba->msg_done)
  148. break;
  149. msleep(1);
  150. }
  151. return hba->msg_done? 0 : -1;
  152. }
  153. static int iop_get_config(struct hptiop_hba *hba,
  154. struct hpt_iop_request_get_config *config)
  155. {
  156. u32 req32;
  157. struct hpt_iop_request_get_config __iomem *req;
  158. req32 = readl(&hba->iop->inbound_queue);
  159. if (req32 == IOPMU_QUEUE_EMPTY)
  160. return -1;
  161. req = (struct hpt_iop_request_get_config __iomem *)
  162. ((unsigned long)hba->iop + req32);
  163. writel(0, &req->header.flags);
  164. writel(IOP_REQUEST_TYPE_GET_CONFIG, &req->header.type);
  165. writel(sizeof(struct hpt_iop_request_get_config), &req->header.size);
  166. writel(IOP_RESULT_PENDING, &req->header.result);
  167. if (iop_send_sync_request(hba, req, 20000)) {
  168. dprintk("Get config send cmd failed\n");
  169. return -1;
  170. }
  171. memcpy_fromio(config, req, sizeof(*config));
  172. writel(req32, &hba->iop->outbound_queue);
  173. return 0;
  174. }
  175. static int iop_set_config(struct hptiop_hba *hba,
  176. struct hpt_iop_request_set_config *config)
  177. {
  178. u32 req32;
  179. struct hpt_iop_request_set_config __iomem *req;
  180. req32 = readl(&hba->iop->inbound_queue);
  181. if (req32 == IOPMU_QUEUE_EMPTY)
  182. return -1;
  183. req = (struct hpt_iop_request_set_config __iomem *)
  184. ((unsigned long)hba->iop + req32);
  185. memcpy_toio((u8 __iomem *)req + sizeof(struct hpt_iop_request_header),
  186. (u8 *)config + sizeof(struct hpt_iop_request_header),
  187. sizeof(struct hpt_iop_request_set_config) -
  188. sizeof(struct hpt_iop_request_header));
  189. writel(0, &req->header.flags);
  190. writel(IOP_REQUEST_TYPE_SET_CONFIG, &req->header.type);
  191. writel(sizeof(struct hpt_iop_request_set_config), &req->header.size);
  192. writel(IOP_RESULT_PENDING, &req->header.result);
  193. if (iop_send_sync_request(hba, req, 20000)) {
  194. dprintk("Set config send cmd failed\n");
  195. return -1;
  196. }
  197. writel(req32, &hba->iop->outbound_queue);
  198. return 0;
  199. }
  200. static int hptiop_initialize_iop(struct hptiop_hba *hba)
  201. {
  202. struct hpt_iopmu __iomem *iop = hba->iop;
  203. /* enable interrupts */
  204. writel(~(IOPMU_OUTBOUND_INT_POSTQUEUE | IOPMU_OUTBOUND_INT_MSG0),
  205. &iop->outbound_intmask);
  206. hba->initialized = 1;
  207. /* start background tasks */
  208. if (iop_send_sync_msg(hba,
  209. IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
  210. printk(KERN_ERR "scsi%d: fail to start background task\n",
  211. hba->host->host_no);
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static int hptiop_map_pci_bar(struct hptiop_hba *hba)
  217. {
  218. u32 mem_base_phy, length;
  219. void __iomem *mem_base_virt;
  220. struct pci_dev *pcidev = hba->pcidev;
  221. if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_MEM)) {
  222. printk(KERN_ERR "scsi%d: pci resource invalid\n",
  223. hba->host->host_no);
  224. return -1;
  225. }
  226. mem_base_phy = pci_resource_start(pcidev, 0);
  227. length = pci_resource_len(pcidev, 0);
  228. mem_base_virt = ioremap(mem_base_phy, length);
  229. if (!mem_base_virt) {
  230. printk(KERN_ERR "scsi%d: Fail to ioremap memory space\n",
  231. hba->host->host_no);
  232. return -1;
  233. }
  234. hba->iop = mem_base_virt;
  235. dprintk("hptiop_map_pci_bar: iop=%p\n", hba->iop);
  236. return 0;
  237. }
  238. static void hptiop_message_callback(struct hptiop_hba *hba, u32 msg)
  239. {
  240. dprintk("iop message 0x%x\n", msg);
  241. if (!hba->initialized)
  242. return;
  243. if (msg == IOPMU_INBOUND_MSG0_RESET) {
  244. atomic_set(&hba->resetting, 0);
  245. wake_up(&hba->reset_wq);
  246. }
  247. else if (msg <= IOPMU_INBOUND_MSG0_MAX)
  248. hba->msg_done = 1;
  249. }
  250. static inline struct hptiop_request *get_req(struct hptiop_hba *hba)
  251. {
  252. struct hptiop_request *ret;
  253. dprintk("get_req : req=%p\n", hba->req_list);
  254. ret = hba->req_list;
  255. if (ret)
  256. hba->req_list = ret->next;
  257. return ret;
  258. }
  259. static inline void free_req(struct hptiop_hba *hba, struct hptiop_request *req)
  260. {
  261. dprintk("free_req(%d, %p)\n", req->index, req);
  262. req->next = hba->req_list;
  263. hba->req_list = req;
  264. }
  265. static void hptiop_host_request_callback(struct hptiop_hba *hba, u32 tag)
  266. {
  267. struct hpt_iop_request_scsi_command *req;
  268. struct scsi_cmnd *scp;
  269. req = (struct hpt_iop_request_scsi_command *)hba->reqs[tag].req_virt;
  270. dprintk("hptiop_host_request_callback: req=%p, type=%d, "
  271. "result=%d, context=0x%x tag=%d\n",
  272. req, req->header.type, req->header.result,
  273. req->header.context, tag);
  274. BUG_ON(!req->header.result);
  275. BUG_ON(req->header.type != cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND));
  276. scp = hba->reqs[tag].scp;
  277. if (HPT_SCP(scp)->mapped) {
  278. if (scp->use_sg)
  279. pci_unmap_sg(hba->pcidev,
  280. (struct scatterlist *)scp->request_buffer,
  281. scp->use_sg,
  282. scp->sc_data_direction
  283. );
  284. else
  285. pci_unmap_single(hba->pcidev,
  286. HPT_SCP(scp)->dma_handle,
  287. scp->request_bufflen,
  288. scp->sc_data_direction
  289. );
  290. }
  291. switch (le32_to_cpu(req->header.result)) {
  292. case IOP_RESULT_SUCCESS:
  293. scp->result = (DID_OK<<16);
  294. break;
  295. case IOP_RESULT_BAD_TARGET:
  296. scp->result = (DID_BAD_TARGET<<16);
  297. break;
  298. case IOP_RESULT_BUSY:
  299. scp->result = (DID_BUS_BUSY<<16);
  300. break;
  301. case IOP_RESULT_RESET:
  302. scp->result = (DID_RESET<<16);
  303. break;
  304. case IOP_RESULT_FAIL:
  305. scp->result = (DID_ERROR<<16);
  306. break;
  307. case IOP_RESULT_INVALID_REQUEST:
  308. scp->result = (DID_ABORT<<16);
  309. break;
  310. case IOP_RESULT_MODE_SENSE_CHECK_CONDITION:
  311. scp->result = SAM_STAT_CHECK_CONDITION;
  312. memset(&scp->sense_buffer,
  313. 0, sizeof(scp->sense_buffer));
  314. memcpy(&scp->sense_buffer,
  315. &req->sg_list, le32_to_cpu(req->dataxfer_length));
  316. break;
  317. default:
  318. scp->result = ((DRIVER_INVALID|SUGGEST_ABORT)<<24) |
  319. (DID_ABORT<<16);
  320. break;
  321. }
  322. dprintk("scsi_done(%p)\n", scp);
  323. scp->scsi_done(scp);
  324. free_req(hba, &hba->reqs[tag]);
  325. }
  326. void hptiop_iop_request_callback(struct hptiop_hba *hba, u32 tag)
  327. {
  328. struct hpt_iop_request_header __iomem *req;
  329. struct hpt_iop_request_ioctl_command __iomem *p;
  330. struct hpt_ioctl_k *arg;
  331. req = (struct hpt_iop_request_header __iomem *)
  332. ((unsigned long)hba->iop + tag);
  333. dprintk("hptiop_iop_request_callback: req=%p, type=%d, "
  334. "result=%d, context=0x%x tag=%d\n",
  335. req, readl(&req->type), readl(&req->result),
  336. readl(&req->context), tag);
  337. BUG_ON(!readl(&req->result));
  338. BUG_ON(readl(&req->type) != IOP_REQUEST_TYPE_IOCTL_COMMAND);
  339. p = (struct hpt_iop_request_ioctl_command __iomem *)req;
  340. arg = (struct hpt_ioctl_k *)(unsigned long)
  341. (readl(&req->context) |
  342. ((u64)readl(&req->context_hi32)<<32));
  343. if (readl(&req->result) == IOP_RESULT_SUCCESS) {
  344. arg->result = HPT_IOCTL_RESULT_OK;
  345. if (arg->outbuf_size)
  346. memcpy_fromio(arg->outbuf,
  347. &p->buf[(readl(&p->inbuf_size) + 3)& ~3],
  348. arg->outbuf_size);
  349. if (arg->bytes_returned)
  350. *arg->bytes_returned = arg->outbuf_size;
  351. }
  352. else
  353. arg->result = HPT_IOCTL_RESULT_FAILED;
  354. arg->done(arg);
  355. writel(tag, &hba->iop->outbound_queue);
  356. }
  357. static irqreturn_t hptiop_intr(int irq, void *dev_id, struct pt_regs *regs)
  358. {
  359. struct hptiop_hba *hba = dev_id;
  360. int handled;
  361. unsigned long flags;
  362. spin_lock_irqsave(hba->host->host_lock, flags);
  363. handled = __iop_intr(hba);
  364. spin_unlock_irqrestore(hba->host->host_lock, flags);
  365. return handled;
  366. }
  367. static int hptiop_buildsgl(struct scsi_cmnd *scp, struct hpt_iopsg *psg)
  368. {
  369. struct Scsi_Host *host = scp->device->host;
  370. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  371. struct scatterlist *sglist = (struct scatterlist *)scp->request_buffer;
  372. /*
  373. * though we'll not get non-use_sg fields anymore,
  374. * keep use_sg checking anyway
  375. */
  376. if (scp->use_sg) {
  377. int idx;
  378. HPT_SCP(scp)->sgcnt = pci_map_sg(hba->pcidev,
  379. sglist, scp->use_sg,
  380. scp->sc_data_direction);
  381. HPT_SCP(scp)->mapped = 1;
  382. BUG_ON(HPT_SCP(scp)->sgcnt > hba->max_sg_descriptors);
  383. for (idx = 0; idx < HPT_SCP(scp)->sgcnt; idx++) {
  384. psg[idx].pci_address =
  385. cpu_to_le64(sg_dma_address(&sglist[idx]));
  386. psg[idx].size = cpu_to_le32(sg_dma_len(&sglist[idx]));
  387. psg[idx].eot = (idx == HPT_SCP(scp)->sgcnt - 1) ?
  388. cpu_to_le32(1) : 0;
  389. }
  390. return HPT_SCP(scp)->sgcnt;
  391. } else {
  392. HPT_SCP(scp)->dma_handle = pci_map_single(
  393. hba->pcidev,
  394. scp->request_buffer,
  395. scp->request_bufflen,
  396. scp->sc_data_direction
  397. );
  398. HPT_SCP(scp)->mapped = 1;
  399. psg->pci_address = cpu_to_le64(HPT_SCP(scp)->dma_handle);
  400. psg->size = cpu_to_le32(scp->request_bufflen);
  401. psg->eot = cpu_to_le32(1);
  402. return 1;
  403. }
  404. }
  405. static int hptiop_queuecommand(struct scsi_cmnd *scp,
  406. void (*done)(struct scsi_cmnd *))
  407. {
  408. struct Scsi_Host *host = scp->device->host;
  409. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  410. struct hpt_iop_request_scsi_command *req;
  411. int sg_count = 0;
  412. struct hptiop_request *_req;
  413. BUG_ON(!done);
  414. scp->scsi_done = done;
  415. _req = get_req(hba);
  416. if (_req == NULL) {
  417. dprintk("hptiop_queuecmd : no free req\n");
  418. return SCSI_MLQUEUE_HOST_BUSY;
  419. }
  420. _req->scp = scp;
  421. dprintk("hptiop_queuecmd(scp=%p) %d/%d/%d/%d cdb=(%x-%x-%x) "
  422. "req_index=%d, req=%p\n",
  423. scp,
  424. host->host_no, scp->device->channel,
  425. scp->device->id, scp->device->lun,
  426. *((u32 *)&scp->cmnd),
  427. *((u32 *)&scp->cmnd + 1),
  428. *((u32 *)&scp->cmnd + 2),
  429. _req->index, _req->req_virt);
  430. scp->result = 0;
  431. if (scp->device->channel || scp->device->lun ||
  432. scp->device->id > hba->max_devices) {
  433. scp->result = DID_BAD_TARGET << 16;
  434. free_req(hba, _req);
  435. goto cmd_done;
  436. }
  437. req = (struct hpt_iop_request_scsi_command *)_req->req_virt;
  438. /* build S/G table */
  439. if (scp->request_bufflen)
  440. sg_count = hptiop_buildsgl(scp, req->sg_list);
  441. else
  442. HPT_SCP(scp)->mapped = 0;
  443. req->header.flags = cpu_to_le32(IOP_REQUEST_FLAG_OUTPUT_CONTEXT);
  444. req->header.type = cpu_to_le32(IOP_REQUEST_TYPE_SCSI_COMMAND);
  445. req->header.result = cpu_to_le32(IOP_RESULT_PENDING);
  446. req->header.context = cpu_to_le32(IOPMU_QUEUE_ADDR_HOST_BIT |
  447. (u32)_req->index);
  448. req->header.context_hi32 = 0;
  449. req->dataxfer_length = cpu_to_le32(scp->request_bufflen);
  450. req->channel = scp->device->channel;
  451. req->target = scp->device->id;
  452. req->lun = scp->device->lun;
  453. req->header.size = cpu_to_le32(
  454. sizeof(struct hpt_iop_request_scsi_command)
  455. - sizeof(struct hpt_iopsg)
  456. + sg_count * sizeof(struct hpt_iopsg));
  457. memcpy(req->cdb, scp->cmnd, sizeof(req->cdb));
  458. writel(IOPMU_QUEUE_ADDR_HOST_BIT | _req->req_shifted_phy,
  459. &hba->iop->inbound_queue);
  460. return 0;
  461. cmd_done:
  462. dprintk("scsi_done(scp=%p)\n", scp);
  463. scp->scsi_done(scp);
  464. return 0;
  465. }
  466. static const char *hptiop_info(struct Scsi_Host *host)
  467. {
  468. return driver_name_long;
  469. }
  470. static int hptiop_reset_hba(struct hptiop_hba *hba)
  471. {
  472. if (atomic_xchg(&hba->resetting, 1) == 0) {
  473. atomic_inc(&hba->reset_count);
  474. writel(IOPMU_INBOUND_MSG0_RESET,
  475. &hba->iop->outbound_msgaddr0);
  476. hptiop_pci_posting_flush(hba->iop);
  477. }
  478. wait_event_timeout(hba->reset_wq,
  479. atomic_read(&hba->resetting) == 0, 60 * HZ);
  480. if (atomic_read(&hba->resetting)) {
  481. /* IOP is in unkown state, abort reset */
  482. printk(KERN_ERR "scsi%d: reset failed\n", hba->host->host_no);
  483. return -1;
  484. }
  485. if (iop_send_sync_msg(hba,
  486. IOPMU_INBOUND_MSG0_START_BACKGROUND_TASK, 5000)) {
  487. dprintk("scsi%d: fail to start background task\n",
  488. hba->host->host_no);
  489. }
  490. return 0;
  491. }
  492. static int hptiop_reset(struct scsi_cmnd *scp)
  493. {
  494. struct Scsi_Host * host = scp->device->host;
  495. struct hptiop_hba * hba = (struct hptiop_hba *)host->hostdata;
  496. printk(KERN_WARNING "hptiop_reset(%d/%d/%d) scp=%p\n",
  497. scp->device->host->host_no, scp->device->channel,
  498. scp->device->id, scp);
  499. return hptiop_reset_hba(hba)? FAILED : SUCCESS;
  500. }
  501. static int hptiop_adjust_disk_queue_depth(struct scsi_device *sdev,
  502. int queue_depth)
  503. {
  504. if(queue_depth > 256)
  505. queue_depth = 256;
  506. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, queue_depth);
  507. return queue_depth;
  508. }
  509. struct hptiop_getinfo {
  510. char __user *buffer;
  511. loff_t buflength;
  512. loff_t bufoffset;
  513. loff_t buffillen;
  514. loff_t filpos;
  515. };
  516. static void hptiop_copy_mem_info(struct hptiop_getinfo *pinfo,
  517. char *data, int datalen)
  518. {
  519. if (pinfo->filpos < pinfo->bufoffset) {
  520. if (pinfo->filpos + datalen <= pinfo->bufoffset) {
  521. pinfo->filpos += datalen;
  522. return;
  523. } else {
  524. data += (pinfo->bufoffset - pinfo->filpos);
  525. datalen -= (pinfo->bufoffset - pinfo->filpos);
  526. pinfo->filpos = pinfo->bufoffset;
  527. }
  528. }
  529. pinfo->filpos += datalen;
  530. if (pinfo->buffillen == pinfo->buflength)
  531. return;
  532. if (pinfo->buflength - pinfo->buffillen < datalen)
  533. datalen = pinfo->buflength - pinfo->buffillen;
  534. if (copy_to_user(pinfo->buffer + pinfo->buffillen, data, datalen))
  535. return;
  536. pinfo->buffillen += datalen;
  537. }
  538. static int hptiop_copy_info(struct hptiop_getinfo *pinfo, char *fmt, ...)
  539. {
  540. va_list args;
  541. char buf[128];
  542. int len;
  543. va_start(args, fmt);
  544. len = vsnprintf(buf, sizeof(buf), fmt, args);
  545. va_end(args);
  546. hptiop_copy_mem_info(pinfo, buf, len);
  547. return len;
  548. }
  549. static void hptiop_ioctl_done(struct hpt_ioctl_k *arg)
  550. {
  551. arg->done = NULL;
  552. wake_up(&arg->hba->ioctl_wq);
  553. }
  554. static void hptiop_do_ioctl(struct hpt_ioctl_k *arg)
  555. {
  556. struct hptiop_hba *hba = arg->hba;
  557. u32 val;
  558. struct hpt_iop_request_ioctl_command __iomem *req;
  559. int ioctl_retry = 0;
  560. dprintk("scsi%d: hptiop_do_ioctl\n", hba->host->host_no);
  561. /*
  562. * check (in + out) buff size from application.
  563. * outbuf must be dword aligned.
  564. */
  565. if (((arg->inbuf_size + 3) & ~3) + arg->outbuf_size >
  566. hba->max_request_size
  567. - sizeof(struct hpt_iop_request_header)
  568. - 4 * sizeof(u32)) {
  569. dprintk("scsi%d: ioctl buf size (%d/%d) is too large\n",
  570. hba->host->host_no,
  571. arg->inbuf_size, arg->outbuf_size);
  572. arg->result = HPT_IOCTL_RESULT_FAILED;
  573. return;
  574. }
  575. retry:
  576. spin_lock_irq(hba->host->host_lock);
  577. val = readl(&hba->iop->inbound_queue);
  578. if (val == IOPMU_QUEUE_EMPTY) {
  579. spin_unlock_irq(hba->host->host_lock);
  580. dprintk("scsi%d: no free req for ioctl\n", hba->host->host_no);
  581. arg->result = -1;
  582. return;
  583. }
  584. req = (struct hpt_iop_request_ioctl_command __iomem *)
  585. ((unsigned long)hba->iop + val);
  586. writel(HPT_CTL_CODE_LINUX_TO_IOP(arg->ioctl_code),
  587. &req->ioctl_code);
  588. writel(arg->inbuf_size, &req->inbuf_size);
  589. writel(arg->outbuf_size, &req->outbuf_size);
  590. /*
  591. * use the buffer on the IOP local memory first, then copy it
  592. * back to host.
  593. * the caller's request buffer shoudl be little-endian.
  594. */
  595. if (arg->inbuf_size)
  596. memcpy_toio(req->buf, arg->inbuf, arg->inbuf_size);
  597. /* correct the controller ID for IOP */
  598. if ((arg->ioctl_code == HPT_IOCTL_GET_CHANNEL_INFO ||
  599. arg->ioctl_code == HPT_IOCTL_GET_CONTROLLER_INFO_V2 ||
  600. arg->ioctl_code == HPT_IOCTL_GET_CONTROLLER_INFO)
  601. && arg->inbuf_size >= sizeof(u32))
  602. writel(0, req->buf);
  603. writel(IOP_REQUEST_TYPE_IOCTL_COMMAND, &req->header.type);
  604. writel(0, &req->header.flags);
  605. writel(offsetof(struct hpt_iop_request_ioctl_command, buf)
  606. + arg->inbuf_size, &req->header.size);
  607. writel((u32)(unsigned long)arg, &req->header.context);
  608. writel(BITS_PER_LONG > 32 ? (u32)((unsigned long)arg>>32) : 0,
  609. &req->header.context_hi32);
  610. writel(IOP_RESULT_PENDING, &req->header.result);
  611. arg->result = HPT_IOCTL_RESULT_FAILED;
  612. arg->done = hptiop_ioctl_done;
  613. writel(val, &hba->iop->inbound_queue);
  614. hptiop_pci_posting_flush(hba->iop);
  615. spin_unlock_irq(hba->host->host_lock);
  616. wait_event_timeout(hba->ioctl_wq, arg->done == NULL, 60 * HZ);
  617. if (arg->done != NULL) {
  618. hptiop_reset_hba(hba);
  619. if (ioctl_retry++ < 3)
  620. goto retry;
  621. }
  622. dprintk("hpt_iop_ioctl %x result %d\n",
  623. arg->ioctl_code, arg->result);
  624. }
  625. static int __hpt_do_ioctl(struct hptiop_hba *hba, u32 code, void *inbuf,
  626. u32 insize, void *outbuf, u32 outsize)
  627. {
  628. struct hpt_ioctl_k arg;
  629. arg.hba = hba;
  630. arg.ioctl_code = code;
  631. arg.inbuf = inbuf;
  632. arg.outbuf = outbuf;
  633. arg.inbuf_size = insize;
  634. arg.outbuf_size = outsize;
  635. arg.bytes_returned = NULL;
  636. hptiop_do_ioctl(&arg);
  637. return arg.result;
  638. }
  639. static inline int hpt_id_valid(__le32 id)
  640. {
  641. return id != 0 && id != cpu_to_le32(0xffffffff);
  642. }
  643. static int hptiop_get_controller_info(struct hptiop_hba *hba,
  644. struct hpt_controller_info *pinfo)
  645. {
  646. int id = 0;
  647. return __hpt_do_ioctl(hba, HPT_IOCTL_GET_CONTROLLER_INFO,
  648. &id, sizeof(int), pinfo, sizeof(*pinfo));
  649. }
  650. static int hptiop_get_channel_info(struct hptiop_hba *hba, int bus,
  651. struct hpt_channel_info *pinfo)
  652. {
  653. u32 ids[2];
  654. ids[0] = 0;
  655. ids[1] = bus;
  656. return __hpt_do_ioctl(hba, HPT_IOCTL_GET_CHANNEL_INFO,
  657. ids, sizeof(ids), pinfo, sizeof(*pinfo));
  658. }
  659. static int hptiop_get_logical_devices(struct hptiop_hba *hba,
  660. __le32 *pids, int maxcount)
  661. {
  662. int i;
  663. u32 count = maxcount - 1;
  664. if (__hpt_do_ioctl(hba, HPT_IOCTL_GET_LOGICAL_DEVICES,
  665. &count, sizeof(u32),
  666. pids, sizeof(u32) * maxcount))
  667. return -1;
  668. maxcount = le32_to_cpu(pids[0]);
  669. for (i = 0; i < maxcount; i++)
  670. pids[i] = pids[i+1];
  671. return maxcount;
  672. }
  673. static int hptiop_get_device_info_v3(struct hptiop_hba *hba, __le32 id,
  674. struct hpt_logical_device_info_v3 *pinfo)
  675. {
  676. return __hpt_do_ioctl(hba, HPT_IOCTL_GET_DEVICE_INFO_V3,
  677. &id, sizeof(u32),
  678. pinfo, sizeof(*pinfo));
  679. }
  680. static const char *get_array_status(struct hpt_logical_device_info_v3 *devinfo)
  681. {
  682. static char s[64];
  683. u32 flags = le32_to_cpu(devinfo->u.array.flags);
  684. u32 trans_prog = le32_to_cpu(devinfo->u.array.transforming_progress);
  685. u32 reb_prog = le32_to_cpu(devinfo->u.array.rebuilding_progress);
  686. if (flags & ARRAY_FLAG_DISABLED)
  687. return "Disabled";
  688. else if (flags & ARRAY_FLAG_TRANSFORMING)
  689. sprintf(s, "Expanding/Migrating %d.%d%%%s%s",
  690. trans_prog / 100,
  691. trans_prog % 100,
  692. (flags & (ARRAY_FLAG_NEEDBUILDING|ARRAY_FLAG_BROKEN))?
  693. ", Critical" : "",
  694. ((flags & ARRAY_FLAG_NEEDINITIALIZING) &&
  695. !(flags & ARRAY_FLAG_REBUILDING) &&
  696. !(flags & ARRAY_FLAG_INITIALIZING))?
  697. ", Unintialized" : "");
  698. else if ((flags & ARRAY_FLAG_BROKEN) &&
  699. devinfo->u.array.array_type != AT_RAID6)
  700. return "Critical";
  701. else if (flags & ARRAY_FLAG_REBUILDING)
  702. sprintf(s,
  703. (flags & ARRAY_FLAG_NEEDINITIALIZING)?
  704. "%sBackground initializing %d.%d%%" :
  705. "%sRebuilding %d.%d%%",
  706. (flags & ARRAY_FLAG_BROKEN)? "Critical, " : "",
  707. reb_prog / 100,
  708. reb_prog % 100);
  709. else if (flags & ARRAY_FLAG_VERIFYING)
  710. sprintf(s, "%sVerifying %d.%d%%",
  711. (flags & ARRAY_FLAG_BROKEN)? "Critical, " : "",
  712. reb_prog / 100,
  713. reb_prog % 100);
  714. else if (flags & ARRAY_FLAG_INITIALIZING)
  715. sprintf(s, "%sForground initializing %d.%d%%",
  716. (flags & ARRAY_FLAG_BROKEN)? "Critical, " : "",
  717. reb_prog / 100,
  718. reb_prog % 100);
  719. else if (flags & ARRAY_FLAG_NEEDTRANSFORM)
  720. sprintf(s,"%s%s%s", "Need Expanding/Migrating",
  721. (flags & ARRAY_FLAG_BROKEN)? "Critical, " : "",
  722. ((flags & ARRAY_FLAG_NEEDINITIALIZING) &&
  723. !(flags & ARRAY_FLAG_REBUILDING) &&
  724. !(flags & ARRAY_FLAG_INITIALIZING))?
  725. ", Unintialized" : "");
  726. else if (flags & ARRAY_FLAG_NEEDINITIALIZING &&
  727. !(flags & ARRAY_FLAG_REBUILDING) &&
  728. !(flags & ARRAY_FLAG_INITIALIZING))
  729. sprintf(s,"%sUninitialized",
  730. (flags & ARRAY_FLAG_BROKEN)? "Critical, " : "");
  731. else if ((flags & ARRAY_FLAG_NEEDBUILDING) ||
  732. (flags & ARRAY_FLAG_BROKEN))
  733. return "Critical";
  734. else
  735. return "Normal";
  736. return s;
  737. }
  738. static void hptiop_dump_devinfo(struct hptiop_hba *hba,
  739. struct hptiop_getinfo *pinfo, __le32 id, int indent)
  740. {
  741. struct hpt_logical_device_info_v3 devinfo;
  742. int i;
  743. u64 capacity;
  744. for (i = 0; i < indent; i++)
  745. hptiop_copy_info(pinfo, "\t");
  746. if (hptiop_get_device_info_v3(hba, id, &devinfo)) {
  747. hptiop_copy_info(pinfo, "unknown\n");
  748. return;
  749. }
  750. switch (devinfo.type) {
  751. case LDT_DEVICE: {
  752. struct hd_driveid *driveid;
  753. u32 flags = le32_to_cpu(devinfo.u.device.flags);
  754. driveid = (struct hd_driveid *)devinfo.u.device.ident;
  755. /* model[] is 40 chars long, but we just want 20 chars here */
  756. driveid->model[20] = 0;
  757. if (indent)
  758. if (flags & DEVICE_FLAG_DISABLED)
  759. hptiop_copy_info(pinfo,"Missing\n");
  760. else
  761. hptiop_copy_info(pinfo, "CH%d %s\n",
  762. devinfo.u.device.path_id + 1,
  763. driveid->model);
  764. else {
  765. capacity = le64_to_cpu(devinfo.capacity) * 512;
  766. do_div(capacity, 1000000);
  767. hptiop_copy_info(pinfo,
  768. "CH%d %s, %lluMB, %s %s%s%s%s\n",
  769. devinfo.u.device.path_id + 1,
  770. driveid->model,
  771. capacity,
  772. (flags & DEVICE_FLAG_DISABLED)?
  773. "Disabled" : "Normal",
  774. devinfo.u.device.read_ahead_enabled?
  775. "[RA]" : "",
  776. devinfo.u.device.write_cache_enabled?
  777. "[WC]" : "",
  778. devinfo.u.device.TCQ_enabled?
  779. "[TCQ]" : "",
  780. devinfo.u.device.NCQ_enabled?
  781. "[NCQ]" : ""
  782. );
  783. }
  784. break;
  785. }
  786. case LDT_ARRAY:
  787. if (devinfo.target_id != INVALID_TARGET_ID)
  788. hptiop_copy_info(pinfo, "[DISK %d_%d] ",
  789. devinfo.vbus_id, devinfo.target_id);
  790. capacity = le64_to_cpu(devinfo.capacity) * 512;
  791. do_div(capacity, 1000000);
  792. hptiop_copy_info(pinfo, "%s (%s), %lluMB, %s\n",
  793. devinfo.u.array.name,
  794. devinfo.u.array.array_type==AT_RAID0? "RAID0" :
  795. devinfo.u.array.array_type==AT_RAID1? "RAID1" :
  796. devinfo.u.array.array_type==AT_RAID5? "RAID5" :
  797. devinfo.u.array.array_type==AT_RAID6? "RAID6" :
  798. devinfo.u.array.array_type==AT_JBOD? "JBOD" :
  799. "unknown",
  800. capacity,
  801. get_array_status(&devinfo));
  802. for (i = 0; i < devinfo.u.array.ndisk; i++) {
  803. if (hpt_id_valid(devinfo.u.array.members[i])) {
  804. if (cpu_to_le16(1<<i) &
  805. devinfo.u.array.critical_members)
  806. hptiop_copy_info(pinfo, "\t*");
  807. hptiop_dump_devinfo(hba, pinfo,
  808. devinfo.u.array.members[i], indent+1);
  809. }
  810. else
  811. hptiop_copy_info(pinfo, "\tMissing\n");
  812. }
  813. if (id == devinfo.u.array.transform_source) {
  814. hptiop_copy_info(pinfo, "\tExpanding/Migrating to:\n");
  815. hptiop_dump_devinfo(hba, pinfo,
  816. devinfo.u.array.transform_target, indent+1);
  817. }
  818. break;
  819. }
  820. }
  821. static ssize_t hptiop_show_version(struct class_device *class_dev, char *buf)
  822. {
  823. return snprintf(buf, PAGE_SIZE, "%s\n", driver_ver);
  824. }
  825. static ssize_t hptiop_cdev_read(struct file *filp, char __user *buf,
  826. size_t count, loff_t *ppos)
  827. {
  828. struct hptiop_hba *hba = filp->private_data;
  829. struct hptiop_getinfo info;
  830. int i, j, ndev;
  831. struct hpt_controller_info con_info;
  832. struct hpt_channel_info chan_info;
  833. __le32 ids[32];
  834. info.buffer = buf;
  835. info.buflength = count;
  836. info.bufoffset = ppos ? *ppos : 0;
  837. info.filpos = 0;
  838. info.buffillen = 0;
  839. if (hptiop_get_controller_info(hba, &con_info))
  840. return -EIO;
  841. for (i = 0; i < con_info.num_buses; i++) {
  842. if (hptiop_get_channel_info(hba, i, &chan_info) == 0) {
  843. if (hpt_id_valid(chan_info.devices[0]))
  844. hptiop_dump_devinfo(hba, &info,
  845. chan_info.devices[0], 0);
  846. if (hpt_id_valid(chan_info.devices[1]))
  847. hptiop_dump_devinfo(hba, &info,
  848. chan_info.devices[1], 0);
  849. }
  850. }
  851. ndev = hptiop_get_logical_devices(hba, ids,
  852. sizeof(ids) / sizeof(ids[0]));
  853. /*
  854. * if hptiop_get_logical_devices fails, ndev==-1 and it just
  855. * output nothing here
  856. */
  857. for (j = 0; j < ndev; j++)
  858. hptiop_dump_devinfo(hba, &info, ids[j], 0);
  859. if (ppos)
  860. *ppos += info.buffillen;
  861. return info.buffillen;
  862. }
  863. static int hptiop_cdev_ioctl(struct inode *inode, struct file *file,
  864. unsigned int cmd, unsigned long arg)
  865. {
  866. struct hptiop_hba *hba = file->private_data;
  867. struct hpt_ioctl_u ioctl_u;
  868. struct hpt_ioctl_k ioctl_k;
  869. u32 bytes_returned;
  870. int err = -EINVAL;
  871. if (copy_from_user(&ioctl_u,
  872. (void __user *)arg, sizeof(struct hpt_ioctl_u)))
  873. return -EINVAL;
  874. if (ioctl_u.magic != HPT_IOCTL_MAGIC)
  875. return -EINVAL;
  876. ioctl_k.ioctl_code = ioctl_u.ioctl_code;
  877. ioctl_k.inbuf = NULL;
  878. ioctl_k.inbuf_size = ioctl_u.inbuf_size;
  879. ioctl_k.outbuf = NULL;
  880. ioctl_k.outbuf_size = ioctl_u.outbuf_size;
  881. ioctl_k.hba = hba;
  882. ioctl_k.bytes_returned = &bytes_returned;
  883. /* verify user buffer */
  884. if ((ioctl_k.inbuf_size && !access_ok(VERIFY_READ,
  885. ioctl_u.inbuf, ioctl_k.inbuf_size)) ||
  886. (ioctl_k.outbuf_size && !access_ok(VERIFY_WRITE,
  887. ioctl_u.outbuf, ioctl_k.outbuf_size)) ||
  888. (ioctl_u.bytes_returned && !access_ok(VERIFY_WRITE,
  889. ioctl_u.bytes_returned, sizeof(u32))) ||
  890. ioctl_k.inbuf_size + ioctl_k.outbuf_size > 0x10000) {
  891. dprintk("scsi%d: got bad user address\n", hba->host->host_no);
  892. return -EINVAL;
  893. }
  894. /* map buffer to kernel. */
  895. if (ioctl_k.inbuf_size) {
  896. ioctl_k.inbuf = kmalloc(ioctl_k.inbuf_size, GFP_KERNEL);
  897. if (!ioctl_k.inbuf) {
  898. dprintk("scsi%d: fail to alloc inbuf\n",
  899. hba->host->host_no);
  900. err = -ENOMEM;
  901. goto err_exit;
  902. }
  903. if (copy_from_user(ioctl_k.inbuf,
  904. ioctl_u.inbuf, ioctl_k.inbuf_size)) {
  905. goto err_exit;
  906. }
  907. }
  908. if (ioctl_k.outbuf_size) {
  909. ioctl_k.outbuf = kmalloc(ioctl_k.outbuf_size, GFP_KERNEL);
  910. if (!ioctl_k.outbuf) {
  911. dprintk("scsi%d: fail to alloc outbuf\n",
  912. hba->host->host_no);
  913. err = -ENOMEM;
  914. goto err_exit;
  915. }
  916. }
  917. hptiop_do_ioctl(&ioctl_k);
  918. if (ioctl_k.result == HPT_IOCTL_RESULT_OK) {
  919. if (ioctl_k.outbuf_size &&
  920. copy_to_user(ioctl_u.outbuf,
  921. ioctl_k.outbuf, ioctl_k.outbuf_size))
  922. goto err_exit;
  923. if (ioctl_u.bytes_returned &&
  924. copy_to_user(ioctl_u.bytes_returned,
  925. &bytes_returned, sizeof(u32)))
  926. goto err_exit;
  927. err = 0;
  928. }
  929. err_exit:
  930. kfree(ioctl_k.inbuf);
  931. kfree(ioctl_k.outbuf);
  932. return err;
  933. }
  934. static int hptiop_cdev_open(struct inode *inode, struct file *file)
  935. {
  936. struct hptiop_hba *hba;
  937. unsigned i = 0, minor = iminor(inode);
  938. int ret = -ENODEV;
  939. spin_lock(&hptiop_hba_list_lock);
  940. list_for_each_entry(hba, &hptiop_hba_list, link) {
  941. if (i == minor) {
  942. file->private_data = hba;
  943. ret = 0;
  944. goto out;
  945. }
  946. i++;
  947. }
  948. out:
  949. spin_unlock(&hptiop_hba_list_lock);
  950. return ret;
  951. }
  952. static struct file_operations hptiop_cdev_fops = {
  953. .owner = THIS_MODULE,
  954. .read = hptiop_cdev_read,
  955. .ioctl = hptiop_cdev_ioctl,
  956. .open = hptiop_cdev_open,
  957. };
  958. static ssize_t hptiop_show_fw_version(struct class_device *class_dev, char *buf)
  959. {
  960. struct Scsi_Host *host = class_to_shost(class_dev);
  961. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  962. return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
  963. hba->firmware_version >> 24,
  964. (hba->firmware_version >> 16) & 0xff,
  965. (hba->firmware_version >> 8) & 0xff,
  966. hba->firmware_version & 0xff);
  967. }
  968. static struct class_device_attribute hptiop_attr_version = {
  969. .attr = {
  970. .name = "driver-version",
  971. .mode = S_IRUGO,
  972. },
  973. .show = hptiop_show_version,
  974. };
  975. static struct class_device_attribute hptiop_attr_fw_version = {
  976. .attr = {
  977. .name = "firmware-version",
  978. .mode = S_IRUGO,
  979. },
  980. .show = hptiop_show_fw_version,
  981. };
  982. static struct class_device_attribute *hptiop_attrs[] = {
  983. &hptiop_attr_version,
  984. &hptiop_attr_fw_version,
  985. NULL
  986. };
  987. static struct scsi_host_template driver_template = {
  988. .module = THIS_MODULE,
  989. .name = driver_name,
  990. .queuecommand = hptiop_queuecommand,
  991. .eh_device_reset_handler = hptiop_reset,
  992. .eh_bus_reset_handler = hptiop_reset,
  993. .info = hptiop_info,
  994. .unchecked_isa_dma = 0,
  995. .emulated = 0,
  996. .use_clustering = ENABLE_CLUSTERING,
  997. .proc_name = driver_name,
  998. .shost_attrs = hptiop_attrs,
  999. .this_id = -1,
  1000. .change_queue_depth = hptiop_adjust_disk_queue_depth,
  1001. };
  1002. static int __devinit hptiop_probe(struct pci_dev *pcidev,
  1003. const struct pci_device_id *id)
  1004. {
  1005. struct Scsi_Host *host = NULL;
  1006. struct hptiop_hba *hba;
  1007. struct hpt_iop_request_get_config iop_config;
  1008. struct hpt_iop_request_set_config set_config;
  1009. dma_addr_t start_phy;
  1010. void *start_virt;
  1011. u32 offset, i, req_size;
  1012. dprintk("hptiop_probe(%p)\n", pcidev);
  1013. if (pci_enable_device(pcidev)) {
  1014. printk(KERN_ERR "hptiop: fail to enable pci device\n");
  1015. return -ENODEV;
  1016. }
  1017. printk(KERN_INFO "adapter at PCI %d:%d:%d, IRQ %d\n",
  1018. pcidev->bus->number, pcidev->devfn >> 3, pcidev->devfn & 7,
  1019. pcidev->irq);
  1020. pci_set_master(pcidev);
  1021. /* Enable 64bit DMA if possible */
  1022. if (pci_set_dma_mask(pcidev, DMA_64BIT_MASK)) {
  1023. if (pci_set_dma_mask(pcidev, DMA_32BIT_MASK)) {
  1024. printk(KERN_ERR "hptiop: fail to set dma_mask\n");
  1025. goto disable_pci_device;
  1026. }
  1027. }
  1028. if (pci_request_regions(pcidev, driver_name)) {
  1029. printk(KERN_ERR "hptiop: pci_request_regions failed\n");
  1030. goto disable_pci_device;
  1031. }
  1032. host = scsi_host_alloc(&driver_template, sizeof(struct hptiop_hba));
  1033. if (!host) {
  1034. printk(KERN_ERR "hptiop: fail to alloc scsi host\n");
  1035. goto free_pci_regions;
  1036. }
  1037. hba = (struct hptiop_hba *)host->hostdata;
  1038. hba->pcidev = pcidev;
  1039. hba->host = host;
  1040. hba->initialized = 0;
  1041. atomic_set(&hba->resetting, 0);
  1042. atomic_set(&hba->reset_count, 0);
  1043. init_waitqueue_head(&hba->reset_wq);
  1044. init_waitqueue_head(&hba->ioctl_wq);
  1045. host->max_lun = 1;
  1046. host->max_channel = 0;
  1047. host->io_port = 0;
  1048. host->n_io_port = 0;
  1049. host->irq = pcidev->irq;
  1050. if (hptiop_map_pci_bar(hba))
  1051. goto free_scsi_host;
  1052. if (iop_wait_ready(hba->iop, 20000)) {
  1053. printk(KERN_ERR "scsi%d: firmware not ready\n",
  1054. hba->host->host_no);
  1055. goto unmap_pci_bar;
  1056. }
  1057. if (iop_get_config(hba, &iop_config)) {
  1058. printk(KERN_ERR "scsi%d: get config failed\n",
  1059. hba->host->host_no);
  1060. goto unmap_pci_bar;
  1061. }
  1062. hba->max_requests = min(le32_to_cpu(iop_config.max_requests),
  1063. HPTIOP_MAX_REQUESTS);
  1064. hba->max_devices = le32_to_cpu(iop_config.max_devices);
  1065. hba->max_request_size = le32_to_cpu(iop_config.request_size);
  1066. hba->max_sg_descriptors = le32_to_cpu(iop_config.max_sg_count);
  1067. hba->firmware_version = le32_to_cpu(iop_config.firmware_version);
  1068. hba->sdram_size = le32_to_cpu(iop_config.sdram_size);
  1069. host->max_sectors = le32_to_cpu(iop_config.data_transfer_length) >> 9;
  1070. host->max_id = le32_to_cpu(iop_config.max_devices);
  1071. host->sg_tablesize = le32_to_cpu(iop_config.max_sg_count);
  1072. host->can_queue = le32_to_cpu(iop_config.max_requests);
  1073. host->cmd_per_lun = le32_to_cpu(iop_config.max_requests);
  1074. host->max_cmd_len = 16;
  1075. set_config.vbus_id = cpu_to_le32(host->host_no);
  1076. set_config.iop_id = cpu_to_le32(host->host_no);
  1077. if (iop_set_config(hba, &set_config)) {
  1078. printk(KERN_ERR "scsi%d: set config failed\n",
  1079. hba->host->host_no);
  1080. goto unmap_pci_bar;
  1081. }
  1082. if (scsi_add_host(host, &pcidev->dev)) {
  1083. printk(KERN_ERR "scsi%d: scsi_add_host failed\n",
  1084. hba->host->host_no);
  1085. goto unmap_pci_bar;
  1086. }
  1087. pci_set_drvdata(pcidev, host);
  1088. if (request_irq(pcidev->irq, hptiop_intr, IRQF_SHARED,
  1089. driver_name, hba)) {
  1090. printk(KERN_ERR "scsi%d: request irq %d failed\n",
  1091. hba->host->host_no, pcidev->irq);
  1092. goto remove_scsi_host;
  1093. }
  1094. /* Allocate request mem */
  1095. req_size = sizeof(struct hpt_iop_request_scsi_command)
  1096. + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
  1097. if ((req_size& 0x1f) != 0)
  1098. req_size = (req_size + 0x1f) & ~0x1f;
  1099. dprintk("req_size=%d, max_requests=%d\n", req_size, hba->max_requests);
  1100. hba->req_size = req_size;
  1101. start_virt = dma_alloc_coherent(&pcidev->dev,
  1102. hba->req_size*hba->max_requests + 0x20,
  1103. &start_phy, GFP_KERNEL);
  1104. if (!start_virt) {
  1105. printk(KERN_ERR "scsi%d: fail to alloc request mem\n",
  1106. hba->host->host_no);
  1107. goto free_request_irq;
  1108. }
  1109. hba->dma_coherent = start_virt;
  1110. hba->dma_coherent_handle = start_phy;
  1111. if ((start_phy & 0x1f) != 0)
  1112. {
  1113. offset = ((start_phy + 0x1f) & ~0x1f) - start_phy;
  1114. start_phy += offset;
  1115. start_virt += offset;
  1116. }
  1117. hba->req_list = start_virt;
  1118. for (i = 0; i < hba->max_requests; i++) {
  1119. hba->reqs[i].next = NULL;
  1120. hba->reqs[i].req_virt = start_virt;
  1121. hba->reqs[i].req_shifted_phy = start_phy >> 5;
  1122. hba->reqs[i].index = i;
  1123. free_req(hba, &hba->reqs[i]);
  1124. start_virt = (char *)start_virt + hba->req_size;
  1125. start_phy = start_phy + hba->req_size;
  1126. }
  1127. /* Enable Interrupt and start background task */
  1128. if (hptiop_initialize_iop(hba))
  1129. goto free_request_mem;
  1130. spin_lock(&hptiop_hba_list_lock);
  1131. list_add_tail(&hba->link, &hptiop_hba_list);
  1132. spin_unlock(&hptiop_hba_list_lock);
  1133. scsi_scan_host(host);
  1134. dprintk("scsi%d: hptiop_probe successfully\n", hba->host->host_no);
  1135. return 0;
  1136. free_request_mem:
  1137. dma_free_coherent(&hba->pcidev->dev,
  1138. hba->req_size*hba->max_requests + 0x20,
  1139. hba->dma_coherent, hba->dma_coherent_handle);
  1140. free_request_irq:
  1141. free_irq(hba->pcidev->irq, hba);
  1142. remove_scsi_host:
  1143. scsi_remove_host(host);
  1144. unmap_pci_bar:
  1145. iounmap(hba->iop);
  1146. free_pci_regions:
  1147. pci_release_regions(pcidev) ;
  1148. free_scsi_host:
  1149. scsi_host_put(host);
  1150. disable_pci_device:
  1151. pci_disable_device(pcidev);
  1152. dprintk("scsi%d: hptiop_probe fail\n", host->host_no);
  1153. return -ENODEV;
  1154. }
  1155. static void hptiop_shutdown(struct pci_dev *pcidev)
  1156. {
  1157. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  1158. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  1159. struct hpt_iopmu __iomem *iop = hba->iop;
  1160. u32 int_mask;
  1161. dprintk("hptiop_shutdown(%p)\n", hba);
  1162. /* stop the iop */
  1163. if (iop_send_sync_msg(hba, IOPMU_INBOUND_MSG0_SHUTDOWN, 60000))
  1164. printk(KERN_ERR "scsi%d: shutdown the iop timeout\n",
  1165. hba->host->host_no);
  1166. /* disable all outbound interrupts */
  1167. int_mask = readl(&iop->outbound_intmask);
  1168. writel(int_mask |
  1169. IOPMU_OUTBOUND_INT_MSG0 | IOPMU_OUTBOUND_INT_POSTQUEUE,
  1170. &iop->outbound_intmask);
  1171. hptiop_pci_posting_flush(iop);
  1172. }
  1173. static void hptiop_remove(struct pci_dev *pcidev)
  1174. {
  1175. struct Scsi_Host *host = pci_get_drvdata(pcidev);
  1176. struct hptiop_hba *hba = (struct hptiop_hba *)host->hostdata;
  1177. dprintk("scsi%d: hptiop_remove\n", hba->host->host_no);
  1178. scsi_remove_host(host);
  1179. spin_lock(&hptiop_hba_list_lock);
  1180. list_del_init(&hba->link);
  1181. spin_unlock(&hptiop_hba_list_lock);
  1182. hptiop_shutdown(pcidev);
  1183. free_irq(hba->pcidev->irq, hba);
  1184. dma_free_coherent(&hba->pcidev->dev,
  1185. hba->req_size * hba->max_requests + 0x20,
  1186. hba->dma_coherent,
  1187. hba->dma_coherent_handle);
  1188. iounmap(hba->iop);
  1189. pci_release_regions(hba->pcidev);
  1190. pci_set_drvdata(hba->pcidev, NULL);
  1191. pci_disable_device(hba->pcidev);
  1192. scsi_host_put(host);
  1193. }
  1194. static struct pci_device_id hptiop_id_table[] = {
  1195. { PCI_DEVICE(0x1103, 0x3220) },
  1196. { PCI_DEVICE(0x1103, 0x3320) },
  1197. {},
  1198. };
  1199. MODULE_DEVICE_TABLE(pci, hptiop_id_table);
  1200. static struct pci_driver hptiop_pci_driver = {
  1201. .name = driver_name,
  1202. .id_table = hptiop_id_table,
  1203. .probe = hptiop_probe,
  1204. .remove = hptiop_remove,
  1205. .shutdown = hptiop_shutdown,
  1206. };
  1207. static int __init hptiop_module_init(void)
  1208. {
  1209. int error;
  1210. printk(KERN_INFO "%s %s\n", driver_name_long, driver_ver);
  1211. error = pci_register_driver(&hptiop_pci_driver);
  1212. if (error < 0)
  1213. return error;
  1214. hptiop_cdev_major = register_chrdev(0, "hptiop", &hptiop_cdev_fops);
  1215. if (hptiop_cdev_major < 0) {
  1216. printk(KERN_WARNING "unable to register hptiop device.\n");
  1217. return hptiop_cdev_major;
  1218. }
  1219. return 0;
  1220. }
  1221. static void __exit hptiop_module_exit(void)
  1222. {
  1223. dprintk("hptiop_module_exit\n");
  1224. unregister_chrdev(hptiop_cdev_major, "hptiop");
  1225. pci_unregister_driver(&hptiop_pci_driver);
  1226. }
  1227. module_init(hptiop_module_init);
  1228. module_exit(hptiop_module_exit);
  1229. MODULE_LICENSE("GPL");