sx8.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * sx8.c: Driver for Promise SATA SX8 looks-like-I2O hardware
  3. *
  4. * Copyright 2004-2005 Red Hat, Inc.
  5. *
  6. * Author/maintainer: Jeff Garzik <jgarzik@pobox.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/pci.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/compiler.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/bitops.h>
  24. #include <linux/delay.h>
  25. #include <linux/time.h>
  26. #include <linux/hdreg.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/completion.h>
  29. #include <asm/io.h>
  30. #include <asm/uaccess.h>
  31. #if 0
  32. #define CARM_DEBUG
  33. #define CARM_VERBOSE_DEBUG
  34. #else
  35. #undef CARM_DEBUG
  36. #undef CARM_VERBOSE_DEBUG
  37. #endif
  38. #undef CARM_NDEBUG
  39. #define DRV_NAME "sx8"
  40. #define DRV_VERSION "1.0"
  41. #define PFX DRV_NAME ": "
  42. MODULE_AUTHOR("Jeff Garzik");
  43. MODULE_LICENSE("GPL");
  44. MODULE_DESCRIPTION("Promise SATA SX8 block driver");
  45. MODULE_VERSION(DRV_VERSION);
  46. /*
  47. * SX8 hardware has a single message queue for all ATA ports.
  48. * When this driver was written, the hardware (firmware?) would
  49. * corrupt data eventually, if more than one request was outstanding.
  50. * As one can imagine, having 8 ports bottlenecking on a single
  51. * command hurts performance.
  52. *
  53. * Based on user reports, later versions of the hardware (firmware?)
  54. * seem to be able to survive with more than one command queued.
  55. *
  56. * Therefore, we default to the safe option -- 1 command -- but
  57. * allow the user to increase this.
  58. *
  59. * SX8 should be able to support up to ~60 queued commands (CARM_MAX_REQ),
  60. * but problems seem to occur when you exceed ~30, even on newer hardware.
  61. */
  62. static int max_queue = 1;
  63. module_param(max_queue, int, 0444);
  64. MODULE_PARM_DESC(max_queue, "Maximum number of queued commands. (min==1, max==30, safe==1)");
  65. #define NEXT_RESP(idx) ((idx + 1) % RMSG_Q_LEN)
  66. /* 0xf is just arbitrary, non-zero noise; this is sorta like poisoning */
  67. #define TAG_ENCODE(tag) (((tag) << 16) | 0xf)
  68. #define TAG_DECODE(tag) (((tag) >> 16) & 0x1f)
  69. #define TAG_VALID(tag) ((((tag) & 0xf) == 0xf) && (TAG_DECODE(tag) < 32))
  70. /* note: prints function name for you */
  71. #ifdef CARM_DEBUG
  72. #define DPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  73. #ifdef CARM_VERBOSE_DEBUG
  74. #define VPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  75. #else
  76. #define VPRINTK(fmt, args...)
  77. #endif /* CARM_VERBOSE_DEBUG */
  78. #else
  79. #define DPRINTK(fmt, args...)
  80. #define VPRINTK(fmt, args...)
  81. #endif /* CARM_DEBUG */
  82. #ifdef CARM_NDEBUG
  83. #define assert(expr)
  84. #else
  85. #define assert(expr) \
  86. if(unlikely(!(expr))) { \
  87. printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
  88. #expr,__FILE__,__FUNCTION__,__LINE__); \
  89. }
  90. #endif
  91. /* defines only for the constants which don't work well as enums */
  92. struct carm_host;
  93. enum {
  94. /* adapter-wide limits */
  95. CARM_MAX_PORTS = 8,
  96. CARM_SHM_SIZE = (4096 << 7),
  97. CARM_MINORS_PER_MAJOR = 256 / CARM_MAX_PORTS,
  98. CARM_MAX_WAIT_Q = CARM_MAX_PORTS + 1,
  99. /* command message queue limits */
  100. CARM_MAX_REQ = 64, /* max command msgs per host */
  101. CARM_MSG_LOW_WATER = (CARM_MAX_REQ / 4), /* refill mark */
  102. /* S/G limits, host-wide and per-request */
  103. CARM_MAX_REQ_SG = 32, /* max s/g entries per request */
  104. CARM_MAX_HOST_SG = 600, /* max s/g entries per host */
  105. CARM_SG_LOW_WATER = (CARM_MAX_HOST_SG / 4), /* re-fill mark */
  106. /* hardware registers */
  107. CARM_IHQP = 0x1c,
  108. CARM_INT_STAT = 0x10, /* interrupt status */
  109. CARM_INT_MASK = 0x14, /* interrupt mask */
  110. CARM_HMUC = 0x18, /* host message unit control */
  111. RBUF_ADDR_LO = 0x20, /* response msg DMA buf low 32 bits */
  112. RBUF_ADDR_HI = 0x24, /* response msg DMA buf high 32 bits */
  113. RBUF_BYTE_SZ = 0x28,
  114. CARM_RESP_IDX = 0x2c,
  115. CARM_CMS0 = 0x30, /* command message size reg 0 */
  116. CARM_LMUC = 0x48,
  117. CARM_HMPHA = 0x6c,
  118. CARM_INITC = 0xb5,
  119. /* bits in CARM_INT_{STAT,MASK} */
  120. INT_RESERVED = 0xfffffff0,
  121. INT_WATCHDOG = (1 << 3), /* watchdog timer */
  122. INT_Q_OVERFLOW = (1 << 2), /* cmd msg q overflow */
  123. INT_Q_AVAILABLE = (1 << 1), /* cmd msg q has free space */
  124. INT_RESPONSE = (1 << 0), /* response msg available */
  125. INT_ACK_MASK = INT_WATCHDOG | INT_Q_OVERFLOW,
  126. INT_DEF_MASK = INT_RESERVED | INT_Q_OVERFLOW |
  127. INT_RESPONSE,
  128. /* command messages, and related register bits */
  129. CARM_HAVE_RESP = 0x01,
  130. CARM_MSG_READ = 1,
  131. CARM_MSG_WRITE = 2,
  132. CARM_MSG_VERIFY = 3,
  133. CARM_MSG_GET_CAPACITY = 4,
  134. CARM_MSG_FLUSH = 5,
  135. CARM_MSG_IOCTL = 6,
  136. CARM_MSG_ARRAY = 8,
  137. CARM_MSG_MISC = 9,
  138. CARM_CME = (1 << 2),
  139. CARM_RME = (1 << 1),
  140. CARM_WZBC = (1 << 0),
  141. CARM_RMI = (1 << 0),
  142. CARM_Q_FULL = (1 << 3),
  143. CARM_MSG_SIZE = 288,
  144. CARM_Q_LEN = 48,
  145. /* CARM_MSG_IOCTL messages */
  146. CARM_IOC_SCAN_CHAN = 5, /* scan channels for devices */
  147. CARM_IOC_GET_TCQ = 13, /* get tcq/ncq depth */
  148. CARM_IOC_SET_TCQ = 14, /* set tcq/ncq depth */
  149. IOC_SCAN_CHAN_NODEV = 0x1f,
  150. IOC_SCAN_CHAN_OFFSET = 0x40,
  151. /* CARM_MSG_ARRAY messages */
  152. CARM_ARRAY_INFO = 0,
  153. ARRAY_NO_EXIST = (1 << 31),
  154. /* response messages */
  155. RMSG_SZ = 8, /* sizeof(struct carm_response) */
  156. RMSG_Q_LEN = 48, /* resp. msg list length */
  157. RMSG_OK = 1, /* bit indicating msg was successful */
  158. /* length of entire resp. msg buffer */
  159. RBUF_LEN = RMSG_SZ * RMSG_Q_LEN,
  160. PDC_SHM_SIZE = (4096 << 7), /* length of entire h/w buffer */
  161. /* CARM_MSG_MISC messages */
  162. MISC_GET_FW_VER = 2,
  163. MISC_ALLOC_MEM = 3,
  164. MISC_SET_TIME = 5,
  165. /* MISC_GET_FW_VER feature bits */
  166. FW_VER_4PORT = (1 << 2), /* 1=4 ports, 0=8 ports */
  167. FW_VER_NON_RAID = (1 << 1), /* 1=non-RAID firmware, 0=RAID */
  168. FW_VER_ZCR = (1 << 0), /* zero channel RAID (whatever that is) */
  169. /* carm_host flags */
  170. FL_NON_RAID = FW_VER_NON_RAID,
  171. FL_4PORT = FW_VER_4PORT,
  172. FL_FW_VER_MASK = (FW_VER_NON_RAID | FW_VER_4PORT),
  173. FL_DAC = (1 << 16),
  174. FL_DYN_MAJOR = (1 << 17),
  175. };
  176. enum {
  177. CARM_SG_BOUNDARY = 0xffffUL, /* s/g segment boundary */
  178. };
  179. enum scatter_gather_types {
  180. SGT_32BIT = 0,
  181. SGT_64BIT = 1,
  182. };
  183. enum host_states {
  184. HST_INVALID, /* invalid state; never used */
  185. HST_ALLOC_BUF, /* setting up master SHM area */
  186. HST_ERROR, /* we never leave here */
  187. HST_PORT_SCAN, /* start dev scan */
  188. HST_DEV_SCAN_START, /* start per-device probe */
  189. HST_DEV_SCAN, /* continue per-device probe */
  190. HST_DEV_ACTIVATE, /* activate devices we found */
  191. HST_PROBE_FINISHED, /* probe is complete */
  192. HST_PROBE_START, /* initiate probe */
  193. HST_SYNC_TIME, /* tell firmware what time it is */
  194. HST_GET_FW_VER, /* get firmware version, adapter port cnt */
  195. };
  196. #ifdef CARM_DEBUG
  197. static const char *state_name[] = {
  198. "HST_INVALID",
  199. "HST_ALLOC_BUF",
  200. "HST_ERROR",
  201. "HST_PORT_SCAN",
  202. "HST_DEV_SCAN_START",
  203. "HST_DEV_SCAN",
  204. "HST_DEV_ACTIVATE",
  205. "HST_PROBE_FINISHED",
  206. "HST_PROBE_START",
  207. "HST_SYNC_TIME",
  208. "HST_GET_FW_VER",
  209. };
  210. #endif
  211. struct carm_port {
  212. unsigned int port_no;
  213. struct gendisk *disk;
  214. struct carm_host *host;
  215. /* attached device characteristics */
  216. u64 capacity;
  217. char name[41];
  218. u16 dev_geom_head;
  219. u16 dev_geom_sect;
  220. u16 dev_geom_cyl;
  221. };
  222. struct carm_request {
  223. unsigned int tag;
  224. int n_elem;
  225. unsigned int msg_type;
  226. unsigned int msg_subtype;
  227. unsigned int msg_bucket;
  228. struct request *rq;
  229. struct carm_port *port;
  230. struct scatterlist sg[CARM_MAX_REQ_SG];
  231. };
  232. struct carm_host {
  233. unsigned long flags;
  234. void __iomem *mmio;
  235. void *shm;
  236. dma_addr_t shm_dma;
  237. int major;
  238. int id;
  239. char name[32];
  240. spinlock_t lock;
  241. struct pci_dev *pdev;
  242. unsigned int state;
  243. u32 fw_ver;
  244. struct request_queue *oob_q;
  245. unsigned int n_oob;
  246. unsigned int hw_sg_used;
  247. unsigned int resp_idx;
  248. unsigned int wait_q_prod;
  249. unsigned int wait_q_cons;
  250. struct request_queue *wait_q[CARM_MAX_WAIT_Q];
  251. unsigned int n_msgs;
  252. u64 msg_alloc;
  253. struct carm_request req[CARM_MAX_REQ];
  254. void *msg_base;
  255. dma_addr_t msg_dma;
  256. int cur_scan_dev;
  257. unsigned long dev_active;
  258. unsigned long dev_present;
  259. struct carm_port port[CARM_MAX_PORTS];
  260. struct work_struct fsm_task;
  261. struct completion probe_comp;
  262. };
  263. struct carm_response {
  264. __le32 ret_handle;
  265. __le32 status;
  266. } __attribute__((packed));
  267. struct carm_msg_sg {
  268. __le32 start;
  269. __le32 len;
  270. } __attribute__((packed));
  271. struct carm_msg_rw {
  272. u8 type;
  273. u8 id;
  274. u8 sg_count;
  275. u8 sg_type;
  276. __le32 handle;
  277. __le32 lba;
  278. __le16 lba_count;
  279. __le16 lba_high;
  280. struct carm_msg_sg sg[32];
  281. } __attribute__((packed));
  282. struct carm_msg_allocbuf {
  283. u8 type;
  284. u8 subtype;
  285. u8 n_sg;
  286. u8 sg_type;
  287. __le32 handle;
  288. __le32 addr;
  289. __le32 len;
  290. __le32 evt_pool;
  291. __le32 n_evt;
  292. __le32 rbuf_pool;
  293. __le32 n_rbuf;
  294. __le32 msg_pool;
  295. __le32 n_msg;
  296. struct carm_msg_sg sg[8];
  297. } __attribute__((packed));
  298. struct carm_msg_ioctl {
  299. u8 type;
  300. u8 subtype;
  301. u8 array_id;
  302. u8 reserved1;
  303. __le32 handle;
  304. __le32 data_addr;
  305. u32 reserved2;
  306. } __attribute__((packed));
  307. struct carm_msg_sync_time {
  308. u8 type;
  309. u8 subtype;
  310. u16 reserved1;
  311. __le32 handle;
  312. u32 reserved2;
  313. __le32 timestamp;
  314. } __attribute__((packed));
  315. struct carm_msg_get_fw_ver {
  316. u8 type;
  317. u8 subtype;
  318. u16 reserved1;
  319. __le32 handle;
  320. __le32 data_addr;
  321. u32 reserved2;
  322. } __attribute__((packed));
  323. struct carm_fw_ver {
  324. __le32 version;
  325. u8 features;
  326. u8 reserved1;
  327. u16 reserved2;
  328. } __attribute__((packed));
  329. struct carm_array_info {
  330. __le32 size;
  331. __le16 size_hi;
  332. __le16 stripe_size;
  333. __le32 mode;
  334. __le16 stripe_blk_sz;
  335. __le16 reserved1;
  336. __le16 cyl;
  337. __le16 head;
  338. __le16 sect;
  339. u8 array_id;
  340. u8 reserved2;
  341. char name[40];
  342. __le32 array_status;
  343. /* device list continues beyond this point? */
  344. } __attribute__((packed));
  345. static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  346. static void carm_remove_one (struct pci_dev *pdev);
  347. static int carm_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  348. static struct pci_device_id carm_pci_tbl[] = {
  349. { PCI_VENDOR_ID_PROMISE, 0x8000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  350. { PCI_VENDOR_ID_PROMISE, 0x8002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  351. { } /* terminate list */
  352. };
  353. MODULE_DEVICE_TABLE(pci, carm_pci_tbl);
  354. static struct pci_driver carm_driver = {
  355. .name = DRV_NAME,
  356. .id_table = carm_pci_tbl,
  357. .probe = carm_init_one,
  358. .remove = carm_remove_one,
  359. };
  360. static struct block_device_operations carm_bd_ops = {
  361. .owner = THIS_MODULE,
  362. .getgeo = carm_bdev_getgeo,
  363. };
  364. static unsigned int carm_host_id;
  365. static unsigned long carm_major_alloc;
  366. static int carm_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  367. {
  368. struct carm_port *port = bdev->bd_disk->private_data;
  369. geo->heads = (u8) port->dev_geom_head;
  370. geo->sectors = (u8) port->dev_geom_sect;
  371. geo->cylinders = port->dev_geom_cyl;
  372. return 0;
  373. }
  374. static const u32 msg_sizes[] = { 32, 64, 128, CARM_MSG_SIZE };
  375. static inline int carm_lookup_bucket(u32 msg_size)
  376. {
  377. int i;
  378. for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
  379. if (msg_size <= msg_sizes[i])
  380. return i;
  381. return -ENOENT;
  382. }
  383. static void carm_init_buckets(void __iomem *mmio)
  384. {
  385. unsigned int i;
  386. for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
  387. writel(msg_sizes[i], mmio + CARM_CMS0 + (4 * i));
  388. }
  389. static inline void *carm_ref_msg(struct carm_host *host,
  390. unsigned int msg_idx)
  391. {
  392. return host->msg_base + (msg_idx * CARM_MSG_SIZE);
  393. }
  394. static inline dma_addr_t carm_ref_msg_dma(struct carm_host *host,
  395. unsigned int msg_idx)
  396. {
  397. return host->msg_dma + (msg_idx * CARM_MSG_SIZE);
  398. }
  399. static int carm_send_msg(struct carm_host *host,
  400. struct carm_request *crq)
  401. {
  402. void __iomem *mmio = host->mmio;
  403. u32 msg = (u32) carm_ref_msg_dma(host, crq->tag);
  404. u32 cm_bucket = crq->msg_bucket;
  405. u32 tmp;
  406. int rc = 0;
  407. VPRINTK("ENTER\n");
  408. tmp = readl(mmio + CARM_HMUC);
  409. if (tmp & CARM_Q_FULL) {
  410. #if 0
  411. tmp = readl(mmio + CARM_INT_MASK);
  412. tmp |= INT_Q_AVAILABLE;
  413. writel(tmp, mmio + CARM_INT_MASK);
  414. readl(mmio + CARM_INT_MASK); /* flush */
  415. #endif
  416. DPRINTK("host msg queue full\n");
  417. rc = -EBUSY;
  418. } else {
  419. writel(msg | (cm_bucket << 1), mmio + CARM_IHQP);
  420. readl(mmio + CARM_IHQP); /* flush */
  421. }
  422. return rc;
  423. }
  424. static struct carm_request *carm_get_request(struct carm_host *host)
  425. {
  426. unsigned int i;
  427. /* obey global hardware limit on S/G entries */
  428. if (host->hw_sg_used >= (CARM_MAX_HOST_SG - CARM_MAX_REQ_SG))
  429. return NULL;
  430. for (i = 0; i < max_queue; i++)
  431. if ((host->msg_alloc & (1ULL << i)) == 0) {
  432. struct carm_request *crq = &host->req[i];
  433. crq->port = NULL;
  434. crq->n_elem = 0;
  435. host->msg_alloc |= (1ULL << i);
  436. host->n_msgs++;
  437. assert(host->n_msgs <= CARM_MAX_REQ);
  438. sg_init_table(crq->sg, CARM_MAX_REQ_SG);
  439. return crq;
  440. }
  441. DPRINTK("no request available, returning NULL\n");
  442. return NULL;
  443. }
  444. static int carm_put_request(struct carm_host *host, struct carm_request *crq)
  445. {
  446. assert(crq->tag < max_queue);
  447. if (unlikely((host->msg_alloc & (1ULL << crq->tag)) == 0))
  448. return -EINVAL; /* tried to clear a tag that was not active */
  449. assert(host->hw_sg_used >= crq->n_elem);
  450. host->msg_alloc &= ~(1ULL << crq->tag);
  451. host->hw_sg_used -= crq->n_elem;
  452. host->n_msgs--;
  453. return 0;
  454. }
  455. static struct carm_request *carm_get_special(struct carm_host *host)
  456. {
  457. unsigned long flags;
  458. struct carm_request *crq = NULL;
  459. struct request *rq;
  460. int tries = 5000;
  461. while (tries-- > 0) {
  462. spin_lock_irqsave(&host->lock, flags);
  463. crq = carm_get_request(host);
  464. spin_unlock_irqrestore(&host->lock, flags);
  465. if (crq)
  466. break;
  467. msleep(10);
  468. }
  469. if (!crq)
  470. return NULL;
  471. rq = blk_get_request(host->oob_q, WRITE /* bogus */, GFP_KERNEL);
  472. if (!rq) {
  473. spin_lock_irqsave(&host->lock, flags);
  474. carm_put_request(host, crq);
  475. spin_unlock_irqrestore(&host->lock, flags);
  476. return NULL;
  477. }
  478. crq->rq = rq;
  479. return crq;
  480. }
  481. static int carm_array_info (struct carm_host *host, unsigned int array_idx)
  482. {
  483. struct carm_msg_ioctl *ioc;
  484. unsigned int idx;
  485. u32 msg_data;
  486. dma_addr_t msg_dma;
  487. struct carm_request *crq;
  488. int rc;
  489. crq = carm_get_special(host);
  490. if (!crq) {
  491. rc = -ENOMEM;
  492. goto err_out;
  493. }
  494. idx = crq->tag;
  495. ioc = carm_ref_msg(host, idx);
  496. msg_dma = carm_ref_msg_dma(host, idx);
  497. msg_data = (u32) (msg_dma + sizeof(struct carm_array_info));
  498. crq->msg_type = CARM_MSG_ARRAY;
  499. crq->msg_subtype = CARM_ARRAY_INFO;
  500. rc = carm_lookup_bucket(sizeof(struct carm_msg_ioctl) +
  501. sizeof(struct carm_array_info));
  502. BUG_ON(rc < 0);
  503. crq->msg_bucket = (u32) rc;
  504. memset(ioc, 0, sizeof(*ioc));
  505. ioc->type = CARM_MSG_ARRAY;
  506. ioc->subtype = CARM_ARRAY_INFO;
  507. ioc->array_id = (u8) array_idx;
  508. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  509. ioc->data_addr = cpu_to_le32(msg_data);
  510. spin_lock_irq(&host->lock);
  511. assert(host->state == HST_DEV_SCAN_START ||
  512. host->state == HST_DEV_SCAN);
  513. spin_unlock_irq(&host->lock);
  514. DPRINTK("blk_insert_request, tag == %u\n", idx);
  515. blk_insert_request(host->oob_q, crq->rq, 1, crq);
  516. return 0;
  517. err_out:
  518. spin_lock_irq(&host->lock);
  519. host->state = HST_ERROR;
  520. spin_unlock_irq(&host->lock);
  521. return rc;
  522. }
  523. typedef unsigned int (*carm_sspc_t)(struct carm_host *, unsigned int, void *);
  524. static int carm_send_special (struct carm_host *host, carm_sspc_t func)
  525. {
  526. struct carm_request *crq;
  527. struct carm_msg_ioctl *ioc;
  528. void *mem;
  529. unsigned int idx, msg_size;
  530. int rc;
  531. crq = carm_get_special(host);
  532. if (!crq)
  533. return -ENOMEM;
  534. idx = crq->tag;
  535. mem = carm_ref_msg(host, idx);
  536. msg_size = func(host, idx, mem);
  537. ioc = mem;
  538. crq->msg_type = ioc->type;
  539. crq->msg_subtype = ioc->subtype;
  540. rc = carm_lookup_bucket(msg_size);
  541. BUG_ON(rc < 0);
  542. crq->msg_bucket = (u32) rc;
  543. DPRINTK("blk_insert_request, tag == %u\n", idx);
  544. blk_insert_request(host->oob_q, crq->rq, 1, crq);
  545. return 0;
  546. }
  547. static unsigned int carm_fill_sync_time(struct carm_host *host,
  548. unsigned int idx, void *mem)
  549. {
  550. struct timeval tv;
  551. struct carm_msg_sync_time *st = mem;
  552. do_gettimeofday(&tv);
  553. memset(st, 0, sizeof(*st));
  554. st->type = CARM_MSG_MISC;
  555. st->subtype = MISC_SET_TIME;
  556. st->handle = cpu_to_le32(TAG_ENCODE(idx));
  557. st->timestamp = cpu_to_le32(tv.tv_sec);
  558. return sizeof(struct carm_msg_sync_time);
  559. }
  560. static unsigned int carm_fill_alloc_buf(struct carm_host *host,
  561. unsigned int idx, void *mem)
  562. {
  563. struct carm_msg_allocbuf *ab = mem;
  564. memset(ab, 0, sizeof(*ab));
  565. ab->type = CARM_MSG_MISC;
  566. ab->subtype = MISC_ALLOC_MEM;
  567. ab->handle = cpu_to_le32(TAG_ENCODE(idx));
  568. ab->n_sg = 1;
  569. ab->sg_type = SGT_32BIT;
  570. ab->addr = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
  571. ab->len = cpu_to_le32(PDC_SHM_SIZE >> 1);
  572. ab->evt_pool = cpu_to_le32(host->shm_dma + (16 * 1024));
  573. ab->n_evt = cpu_to_le32(1024);
  574. ab->rbuf_pool = cpu_to_le32(host->shm_dma);
  575. ab->n_rbuf = cpu_to_le32(RMSG_Q_LEN);
  576. ab->msg_pool = cpu_to_le32(host->shm_dma + RBUF_LEN);
  577. ab->n_msg = cpu_to_le32(CARM_Q_LEN);
  578. ab->sg[0].start = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
  579. ab->sg[0].len = cpu_to_le32(65536);
  580. return sizeof(struct carm_msg_allocbuf);
  581. }
  582. static unsigned int carm_fill_scan_channels(struct carm_host *host,
  583. unsigned int idx, void *mem)
  584. {
  585. struct carm_msg_ioctl *ioc = mem;
  586. u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) +
  587. IOC_SCAN_CHAN_OFFSET);
  588. memset(ioc, 0, sizeof(*ioc));
  589. ioc->type = CARM_MSG_IOCTL;
  590. ioc->subtype = CARM_IOC_SCAN_CHAN;
  591. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  592. ioc->data_addr = cpu_to_le32(msg_data);
  593. /* fill output data area with "no device" default values */
  594. mem += IOC_SCAN_CHAN_OFFSET;
  595. memset(mem, IOC_SCAN_CHAN_NODEV, CARM_MAX_PORTS);
  596. return IOC_SCAN_CHAN_OFFSET + CARM_MAX_PORTS;
  597. }
  598. static unsigned int carm_fill_get_fw_ver(struct carm_host *host,
  599. unsigned int idx, void *mem)
  600. {
  601. struct carm_msg_get_fw_ver *ioc = mem;
  602. u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) + sizeof(*ioc));
  603. memset(ioc, 0, sizeof(*ioc));
  604. ioc->type = CARM_MSG_MISC;
  605. ioc->subtype = MISC_GET_FW_VER;
  606. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  607. ioc->data_addr = cpu_to_le32(msg_data);
  608. return sizeof(struct carm_msg_get_fw_ver) +
  609. sizeof(struct carm_fw_ver);
  610. }
  611. static inline void carm_end_request_queued(struct carm_host *host,
  612. struct carm_request *crq,
  613. int uptodate)
  614. {
  615. struct request *req = crq->rq;
  616. int rc;
  617. rc = end_that_request_first(req, uptodate, req->hard_nr_sectors);
  618. assert(rc == 0);
  619. end_that_request_last(req, uptodate);
  620. rc = carm_put_request(host, crq);
  621. assert(rc == 0);
  622. }
  623. static inline void carm_push_q (struct carm_host *host, struct request_queue *q)
  624. {
  625. unsigned int idx = host->wait_q_prod % CARM_MAX_WAIT_Q;
  626. blk_stop_queue(q);
  627. VPRINTK("STOPPED QUEUE %p\n", q);
  628. host->wait_q[idx] = q;
  629. host->wait_q_prod++;
  630. BUG_ON(host->wait_q_prod == host->wait_q_cons); /* overrun */
  631. }
  632. static inline struct request_queue *carm_pop_q(struct carm_host *host)
  633. {
  634. unsigned int idx;
  635. if (host->wait_q_prod == host->wait_q_cons)
  636. return NULL;
  637. idx = host->wait_q_cons % CARM_MAX_WAIT_Q;
  638. host->wait_q_cons++;
  639. return host->wait_q[idx];
  640. }
  641. static inline void carm_round_robin(struct carm_host *host)
  642. {
  643. struct request_queue *q = carm_pop_q(host);
  644. if (q) {
  645. blk_start_queue(q);
  646. VPRINTK("STARTED QUEUE %p\n", q);
  647. }
  648. }
  649. static inline void carm_end_rq(struct carm_host *host, struct carm_request *crq,
  650. int is_ok)
  651. {
  652. carm_end_request_queued(host, crq, is_ok);
  653. if (max_queue == 1)
  654. carm_round_robin(host);
  655. else if ((host->n_msgs <= CARM_MSG_LOW_WATER) &&
  656. (host->hw_sg_used <= CARM_SG_LOW_WATER)) {
  657. carm_round_robin(host);
  658. }
  659. }
  660. static void carm_oob_rq_fn(struct request_queue *q)
  661. {
  662. struct carm_host *host = q->queuedata;
  663. struct carm_request *crq;
  664. struct request *rq;
  665. int rc;
  666. while (1) {
  667. DPRINTK("get req\n");
  668. rq = elv_next_request(q);
  669. if (!rq)
  670. break;
  671. blkdev_dequeue_request(rq);
  672. crq = rq->special;
  673. assert(crq != NULL);
  674. assert(crq->rq == rq);
  675. crq->n_elem = 0;
  676. DPRINTK("send req\n");
  677. rc = carm_send_msg(host, crq);
  678. if (rc) {
  679. blk_requeue_request(q, rq);
  680. carm_push_q(host, q);
  681. return; /* call us again later, eventually */
  682. }
  683. }
  684. }
  685. static void carm_rq_fn(struct request_queue *q)
  686. {
  687. struct carm_port *port = q->queuedata;
  688. struct carm_host *host = port->host;
  689. struct carm_msg_rw *msg;
  690. struct carm_request *crq;
  691. struct request *rq;
  692. struct scatterlist *sg;
  693. int writing = 0, pci_dir, i, n_elem, rc;
  694. u32 tmp;
  695. unsigned int msg_size;
  696. queue_one_request:
  697. VPRINTK("get req\n");
  698. rq = elv_next_request(q);
  699. if (!rq)
  700. return;
  701. crq = carm_get_request(host);
  702. if (!crq) {
  703. carm_push_q(host, q);
  704. return; /* call us again later, eventually */
  705. }
  706. crq->rq = rq;
  707. blkdev_dequeue_request(rq);
  708. if (rq_data_dir(rq) == WRITE) {
  709. writing = 1;
  710. pci_dir = PCI_DMA_TODEVICE;
  711. } else {
  712. pci_dir = PCI_DMA_FROMDEVICE;
  713. }
  714. /* get scatterlist from block layer */
  715. sg = &crq->sg[0];
  716. n_elem = blk_rq_map_sg(q, rq, sg);
  717. if (n_elem <= 0) {
  718. carm_end_rq(host, crq, 0);
  719. return; /* request with no s/g entries? */
  720. }
  721. /* map scatterlist to PCI bus addresses */
  722. n_elem = pci_map_sg(host->pdev, sg, n_elem, pci_dir);
  723. if (n_elem <= 0) {
  724. carm_end_rq(host, crq, 0);
  725. return; /* request with no s/g entries? */
  726. }
  727. crq->n_elem = n_elem;
  728. crq->port = port;
  729. host->hw_sg_used += n_elem;
  730. /*
  731. * build read/write message
  732. */
  733. VPRINTK("build msg\n");
  734. msg = (struct carm_msg_rw *) carm_ref_msg(host, crq->tag);
  735. if (writing) {
  736. msg->type = CARM_MSG_WRITE;
  737. crq->msg_type = CARM_MSG_WRITE;
  738. } else {
  739. msg->type = CARM_MSG_READ;
  740. crq->msg_type = CARM_MSG_READ;
  741. }
  742. msg->id = port->port_no;
  743. msg->sg_count = n_elem;
  744. msg->sg_type = SGT_32BIT;
  745. msg->handle = cpu_to_le32(TAG_ENCODE(crq->tag));
  746. msg->lba = cpu_to_le32(rq->sector & 0xffffffff);
  747. tmp = (rq->sector >> 16) >> 16;
  748. msg->lba_high = cpu_to_le16( (u16) tmp );
  749. msg->lba_count = cpu_to_le16(rq->nr_sectors);
  750. msg_size = sizeof(struct carm_msg_rw) - sizeof(msg->sg);
  751. for (i = 0; i < n_elem; i++) {
  752. struct carm_msg_sg *carm_sg = &msg->sg[i];
  753. carm_sg->start = cpu_to_le32(sg_dma_address(&crq->sg[i]));
  754. carm_sg->len = cpu_to_le32(sg_dma_len(&crq->sg[i]));
  755. msg_size += sizeof(struct carm_msg_sg);
  756. }
  757. rc = carm_lookup_bucket(msg_size);
  758. BUG_ON(rc < 0);
  759. crq->msg_bucket = (u32) rc;
  760. /*
  761. * queue read/write message to hardware
  762. */
  763. VPRINTK("send msg, tag == %u\n", crq->tag);
  764. rc = carm_send_msg(host, crq);
  765. if (rc) {
  766. carm_put_request(host, crq);
  767. blk_requeue_request(q, rq);
  768. carm_push_q(host, q);
  769. return; /* call us again later, eventually */
  770. }
  771. goto queue_one_request;
  772. }
  773. static void carm_handle_array_info(struct carm_host *host,
  774. struct carm_request *crq, u8 *mem,
  775. int is_ok)
  776. {
  777. struct carm_port *port;
  778. u8 *msg_data = mem + sizeof(struct carm_array_info);
  779. struct carm_array_info *desc = (struct carm_array_info *) msg_data;
  780. u64 lo, hi;
  781. int cur_port;
  782. size_t slen;
  783. DPRINTK("ENTER\n");
  784. carm_end_rq(host, crq, is_ok);
  785. if (!is_ok)
  786. goto out;
  787. if (le32_to_cpu(desc->array_status) & ARRAY_NO_EXIST)
  788. goto out;
  789. cur_port = host->cur_scan_dev;
  790. /* should never occur */
  791. if ((cur_port < 0) || (cur_port >= CARM_MAX_PORTS)) {
  792. printk(KERN_ERR PFX "BUG: cur_scan_dev==%d, array_id==%d\n",
  793. cur_port, (int) desc->array_id);
  794. goto out;
  795. }
  796. port = &host->port[cur_port];
  797. lo = (u64) le32_to_cpu(desc->size);
  798. hi = (u64) le16_to_cpu(desc->size_hi);
  799. port->capacity = lo | (hi << 32);
  800. port->dev_geom_head = le16_to_cpu(desc->head);
  801. port->dev_geom_sect = le16_to_cpu(desc->sect);
  802. port->dev_geom_cyl = le16_to_cpu(desc->cyl);
  803. host->dev_active |= (1 << cur_port);
  804. strncpy(port->name, desc->name, sizeof(port->name));
  805. port->name[sizeof(port->name) - 1] = 0;
  806. slen = strlen(port->name);
  807. while (slen && (port->name[slen - 1] == ' ')) {
  808. port->name[slen - 1] = 0;
  809. slen--;
  810. }
  811. printk(KERN_INFO DRV_NAME "(%s): port %u device %Lu sectors\n",
  812. pci_name(host->pdev), port->port_no,
  813. (unsigned long long) port->capacity);
  814. printk(KERN_INFO DRV_NAME "(%s): port %u device \"%s\"\n",
  815. pci_name(host->pdev), port->port_no, port->name);
  816. out:
  817. assert(host->state == HST_DEV_SCAN);
  818. schedule_work(&host->fsm_task);
  819. }
  820. static void carm_handle_scan_chan(struct carm_host *host,
  821. struct carm_request *crq, u8 *mem,
  822. int is_ok)
  823. {
  824. u8 *msg_data = mem + IOC_SCAN_CHAN_OFFSET;
  825. unsigned int i, dev_count = 0;
  826. int new_state = HST_DEV_SCAN_START;
  827. DPRINTK("ENTER\n");
  828. carm_end_rq(host, crq, is_ok);
  829. if (!is_ok) {
  830. new_state = HST_ERROR;
  831. goto out;
  832. }
  833. /* TODO: scan and support non-disk devices */
  834. for (i = 0; i < 8; i++)
  835. if (msg_data[i] == 0) { /* direct-access device (disk) */
  836. host->dev_present |= (1 << i);
  837. dev_count++;
  838. }
  839. printk(KERN_INFO DRV_NAME "(%s): found %u interesting devices\n",
  840. pci_name(host->pdev), dev_count);
  841. out:
  842. assert(host->state == HST_PORT_SCAN);
  843. host->state = new_state;
  844. schedule_work(&host->fsm_task);
  845. }
  846. static void carm_handle_generic(struct carm_host *host,
  847. struct carm_request *crq, int is_ok,
  848. int cur_state, int next_state)
  849. {
  850. DPRINTK("ENTER\n");
  851. carm_end_rq(host, crq, is_ok);
  852. assert(host->state == cur_state);
  853. if (is_ok)
  854. host->state = next_state;
  855. else
  856. host->state = HST_ERROR;
  857. schedule_work(&host->fsm_task);
  858. }
  859. static inline void carm_handle_rw(struct carm_host *host,
  860. struct carm_request *crq, int is_ok)
  861. {
  862. int pci_dir;
  863. VPRINTK("ENTER\n");
  864. if (rq_data_dir(crq->rq) == WRITE)
  865. pci_dir = PCI_DMA_TODEVICE;
  866. else
  867. pci_dir = PCI_DMA_FROMDEVICE;
  868. pci_unmap_sg(host->pdev, &crq->sg[0], crq->n_elem, pci_dir);
  869. carm_end_rq(host, crq, is_ok);
  870. }
  871. static inline void carm_handle_resp(struct carm_host *host,
  872. __le32 ret_handle_le, u32 status)
  873. {
  874. u32 handle = le32_to_cpu(ret_handle_le);
  875. unsigned int msg_idx;
  876. struct carm_request *crq;
  877. int is_ok = (status == RMSG_OK);
  878. u8 *mem;
  879. VPRINTK("ENTER, handle == 0x%x\n", handle);
  880. if (unlikely(!TAG_VALID(handle))) {
  881. printk(KERN_ERR DRV_NAME "(%s): BUG: invalid tag 0x%x\n",
  882. pci_name(host->pdev), handle);
  883. return;
  884. }
  885. msg_idx = TAG_DECODE(handle);
  886. VPRINTK("tag == %u\n", msg_idx);
  887. crq = &host->req[msg_idx];
  888. /* fast path */
  889. if (likely(crq->msg_type == CARM_MSG_READ ||
  890. crq->msg_type == CARM_MSG_WRITE)) {
  891. carm_handle_rw(host, crq, is_ok);
  892. return;
  893. }
  894. mem = carm_ref_msg(host, msg_idx);
  895. switch (crq->msg_type) {
  896. case CARM_MSG_IOCTL: {
  897. switch (crq->msg_subtype) {
  898. case CARM_IOC_SCAN_CHAN:
  899. carm_handle_scan_chan(host, crq, mem, is_ok);
  900. break;
  901. default:
  902. /* unknown / invalid response */
  903. goto err_out;
  904. }
  905. break;
  906. }
  907. case CARM_MSG_MISC: {
  908. switch (crq->msg_subtype) {
  909. case MISC_ALLOC_MEM:
  910. carm_handle_generic(host, crq, is_ok,
  911. HST_ALLOC_BUF, HST_SYNC_TIME);
  912. break;
  913. case MISC_SET_TIME:
  914. carm_handle_generic(host, crq, is_ok,
  915. HST_SYNC_TIME, HST_GET_FW_VER);
  916. break;
  917. case MISC_GET_FW_VER: {
  918. struct carm_fw_ver *ver = (struct carm_fw_ver *)
  919. mem + sizeof(struct carm_msg_get_fw_ver);
  920. if (is_ok) {
  921. host->fw_ver = le32_to_cpu(ver->version);
  922. host->flags |= (ver->features & FL_FW_VER_MASK);
  923. }
  924. carm_handle_generic(host, crq, is_ok,
  925. HST_GET_FW_VER, HST_PORT_SCAN);
  926. break;
  927. }
  928. default:
  929. /* unknown / invalid response */
  930. goto err_out;
  931. }
  932. break;
  933. }
  934. case CARM_MSG_ARRAY: {
  935. switch (crq->msg_subtype) {
  936. case CARM_ARRAY_INFO:
  937. carm_handle_array_info(host, crq, mem, is_ok);
  938. break;
  939. default:
  940. /* unknown / invalid response */
  941. goto err_out;
  942. }
  943. break;
  944. }
  945. default:
  946. /* unknown / invalid response */
  947. goto err_out;
  948. }
  949. return;
  950. err_out:
  951. printk(KERN_WARNING DRV_NAME "(%s): BUG: unhandled message type %d/%d\n",
  952. pci_name(host->pdev), crq->msg_type, crq->msg_subtype);
  953. carm_end_rq(host, crq, 0);
  954. }
  955. static inline void carm_handle_responses(struct carm_host *host)
  956. {
  957. void __iomem *mmio = host->mmio;
  958. struct carm_response *resp = (struct carm_response *) host->shm;
  959. unsigned int work = 0;
  960. unsigned int idx = host->resp_idx % RMSG_Q_LEN;
  961. while (1) {
  962. u32 status = le32_to_cpu(resp[idx].status);
  963. if (status == 0xffffffff) {
  964. VPRINTK("ending response on index %u\n", idx);
  965. writel(idx << 3, mmio + CARM_RESP_IDX);
  966. break;
  967. }
  968. /* response to a message we sent */
  969. else if ((status & (1 << 31)) == 0) {
  970. VPRINTK("handling msg response on index %u\n", idx);
  971. carm_handle_resp(host, resp[idx].ret_handle, status);
  972. resp[idx].status = cpu_to_le32(0xffffffff);
  973. }
  974. /* asynchronous events the hardware throws our way */
  975. else if ((status & 0xff000000) == (1 << 31)) {
  976. u8 *evt_type_ptr = (u8 *) &resp[idx];
  977. u8 evt_type = *evt_type_ptr;
  978. printk(KERN_WARNING DRV_NAME "(%s): unhandled event type %d\n",
  979. pci_name(host->pdev), (int) evt_type);
  980. resp[idx].status = cpu_to_le32(0xffffffff);
  981. }
  982. idx = NEXT_RESP(idx);
  983. work++;
  984. }
  985. VPRINTK("EXIT, work==%u\n", work);
  986. host->resp_idx += work;
  987. }
  988. static irqreturn_t carm_interrupt(int irq, void *__host)
  989. {
  990. struct carm_host *host = __host;
  991. void __iomem *mmio;
  992. u32 mask;
  993. int handled = 0;
  994. unsigned long flags;
  995. if (!host) {
  996. VPRINTK("no host\n");
  997. return IRQ_NONE;
  998. }
  999. spin_lock_irqsave(&host->lock, flags);
  1000. mmio = host->mmio;
  1001. /* reading should also clear interrupts */
  1002. mask = readl(mmio + CARM_INT_STAT);
  1003. if (mask == 0 || mask == 0xffffffff) {
  1004. VPRINTK("no work, mask == 0x%x\n", mask);
  1005. goto out;
  1006. }
  1007. if (mask & INT_ACK_MASK)
  1008. writel(mask, mmio + CARM_INT_STAT);
  1009. if (unlikely(host->state == HST_INVALID)) {
  1010. VPRINTK("not initialized yet, mask = 0x%x\n", mask);
  1011. goto out;
  1012. }
  1013. if (mask & CARM_HAVE_RESP) {
  1014. handled = 1;
  1015. carm_handle_responses(host);
  1016. }
  1017. out:
  1018. spin_unlock_irqrestore(&host->lock, flags);
  1019. VPRINTK("EXIT\n");
  1020. return IRQ_RETVAL(handled);
  1021. }
  1022. static void carm_fsm_task (struct work_struct *work)
  1023. {
  1024. struct carm_host *host =
  1025. container_of(work, struct carm_host, fsm_task);
  1026. unsigned long flags;
  1027. unsigned int state;
  1028. int rc, i, next_dev;
  1029. int reschedule = 0;
  1030. int new_state = HST_INVALID;
  1031. spin_lock_irqsave(&host->lock, flags);
  1032. state = host->state;
  1033. spin_unlock_irqrestore(&host->lock, flags);
  1034. DPRINTK("ENTER, state == %s\n", state_name[state]);
  1035. switch (state) {
  1036. case HST_PROBE_START:
  1037. new_state = HST_ALLOC_BUF;
  1038. reschedule = 1;
  1039. break;
  1040. case HST_ALLOC_BUF:
  1041. rc = carm_send_special(host, carm_fill_alloc_buf);
  1042. if (rc) {
  1043. new_state = HST_ERROR;
  1044. reschedule = 1;
  1045. }
  1046. break;
  1047. case HST_SYNC_TIME:
  1048. rc = carm_send_special(host, carm_fill_sync_time);
  1049. if (rc) {
  1050. new_state = HST_ERROR;
  1051. reschedule = 1;
  1052. }
  1053. break;
  1054. case HST_GET_FW_VER:
  1055. rc = carm_send_special(host, carm_fill_get_fw_ver);
  1056. if (rc) {
  1057. new_state = HST_ERROR;
  1058. reschedule = 1;
  1059. }
  1060. break;
  1061. case HST_PORT_SCAN:
  1062. rc = carm_send_special(host, carm_fill_scan_channels);
  1063. if (rc) {
  1064. new_state = HST_ERROR;
  1065. reschedule = 1;
  1066. }
  1067. break;
  1068. case HST_DEV_SCAN_START:
  1069. host->cur_scan_dev = -1;
  1070. new_state = HST_DEV_SCAN;
  1071. reschedule = 1;
  1072. break;
  1073. case HST_DEV_SCAN:
  1074. next_dev = -1;
  1075. for (i = host->cur_scan_dev + 1; i < CARM_MAX_PORTS; i++)
  1076. if (host->dev_present & (1 << i)) {
  1077. next_dev = i;
  1078. break;
  1079. }
  1080. if (next_dev >= 0) {
  1081. host->cur_scan_dev = next_dev;
  1082. rc = carm_array_info(host, next_dev);
  1083. if (rc) {
  1084. new_state = HST_ERROR;
  1085. reschedule = 1;
  1086. }
  1087. } else {
  1088. new_state = HST_DEV_ACTIVATE;
  1089. reschedule = 1;
  1090. }
  1091. break;
  1092. case HST_DEV_ACTIVATE: {
  1093. int activated = 0;
  1094. for (i = 0; i < CARM_MAX_PORTS; i++)
  1095. if (host->dev_active & (1 << i)) {
  1096. struct carm_port *port = &host->port[i];
  1097. struct gendisk *disk = port->disk;
  1098. set_capacity(disk, port->capacity);
  1099. add_disk(disk);
  1100. activated++;
  1101. }
  1102. printk(KERN_INFO DRV_NAME "(%s): %d ports activated\n",
  1103. pci_name(host->pdev), activated);
  1104. new_state = HST_PROBE_FINISHED;
  1105. reschedule = 1;
  1106. break;
  1107. }
  1108. case HST_PROBE_FINISHED:
  1109. complete(&host->probe_comp);
  1110. break;
  1111. case HST_ERROR:
  1112. /* FIXME: TODO */
  1113. break;
  1114. default:
  1115. /* should never occur */
  1116. printk(KERN_ERR PFX "BUG: unknown state %d\n", state);
  1117. assert(0);
  1118. break;
  1119. }
  1120. if (new_state != HST_INVALID) {
  1121. spin_lock_irqsave(&host->lock, flags);
  1122. host->state = new_state;
  1123. spin_unlock_irqrestore(&host->lock, flags);
  1124. }
  1125. if (reschedule)
  1126. schedule_work(&host->fsm_task);
  1127. }
  1128. static int carm_init_wait(void __iomem *mmio, u32 bits, unsigned int test_bit)
  1129. {
  1130. unsigned int i;
  1131. for (i = 0; i < 50000; i++) {
  1132. u32 tmp = readl(mmio + CARM_LMUC);
  1133. udelay(100);
  1134. if (test_bit) {
  1135. if ((tmp & bits) == bits)
  1136. return 0;
  1137. } else {
  1138. if ((tmp & bits) == 0)
  1139. return 0;
  1140. }
  1141. cond_resched();
  1142. }
  1143. printk(KERN_ERR PFX "carm_init_wait timeout, bits == 0x%x, test_bit == %s\n",
  1144. bits, test_bit ? "yes" : "no");
  1145. return -EBUSY;
  1146. }
  1147. static void carm_init_responses(struct carm_host *host)
  1148. {
  1149. void __iomem *mmio = host->mmio;
  1150. unsigned int i;
  1151. struct carm_response *resp = (struct carm_response *) host->shm;
  1152. for (i = 0; i < RMSG_Q_LEN; i++)
  1153. resp[i].status = cpu_to_le32(0xffffffff);
  1154. writel(0, mmio + CARM_RESP_IDX);
  1155. }
  1156. static int carm_init_host(struct carm_host *host)
  1157. {
  1158. void __iomem *mmio = host->mmio;
  1159. u32 tmp;
  1160. u8 tmp8;
  1161. int rc;
  1162. DPRINTK("ENTER\n");
  1163. writel(0, mmio + CARM_INT_MASK);
  1164. tmp8 = readb(mmio + CARM_INITC);
  1165. if (tmp8 & 0x01) {
  1166. tmp8 &= ~0x01;
  1167. writeb(tmp8, mmio + CARM_INITC);
  1168. readb(mmio + CARM_INITC); /* flush */
  1169. DPRINTK("snooze...\n");
  1170. msleep(5000);
  1171. }
  1172. tmp = readl(mmio + CARM_HMUC);
  1173. if (tmp & CARM_CME) {
  1174. DPRINTK("CME bit present, waiting\n");
  1175. rc = carm_init_wait(mmio, CARM_CME, 1);
  1176. if (rc) {
  1177. DPRINTK("EXIT, carm_init_wait 1 failed\n");
  1178. return rc;
  1179. }
  1180. }
  1181. if (tmp & CARM_RME) {
  1182. DPRINTK("RME bit present, waiting\n");
  1183. rc = carm_init_wait(mmio, CARM_RME, 1);
  1184. if (rc) {
  1185. DPRINTK("EXIT, carm_init_wait 2 failed\n");
  1186. return rc;
  1187. }
  1188. }
  1189. tmp &= ~(CARM_RME | CARM_CME);
  1190. writel(tmp, mmio + CARM_HMUC);
  1191. readl(mmio + CARM_HMUC); /* flush */
  1192. rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 0);
  1193. if (rc) {
  1194. DPRINTK("EXIT, carm_init_wait 3 failed\n");
  1195. return rc;
  1196. }
  1197. carm_init_buckets(mmio);
  1198. writel(host->shm_dma & 0xffffffff, mmio + RBUF_ADDR_LO);
  1199. writel((host->shm_dma >> 16) >> 16, mmio + RBUF_ADDR_HI);
  1200. writel(RBUF_LEN, mmio + RBUF_BYTE_SZ);
  1201. tmp = readl(mmio + CARM_HMUC);
  1202. tmp |= (CARM_RME | CARM_CME | CARM_WZBC);
  1203. writel(tmp, mmio + CARM_HMUC);
  1204. readl(mmio + CARM_HMUC); /* flush */
  1205. rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 1);
  1206. if (rc) {
  1207. DPRINTK("EXIT, carm_init_wait 4 failed\n");
  1208. return rc;
  1209. }
  1210. writel(0, mmio + CARM_HMPHA);
  1211. writel(INT_DEF_MASK, mmio + CARM_INT_MASK);
  1212. carm_init_responses(host);
  1213. /* start initialization, probing state machine */
  1214. spin_lock_irq(&host->lock);
  1215. assert(host->state == HST_INVALID);
  1216. host->state = HST_PROBE_START;
  1217. spin_unlock_irq(&host->lock);
  1218. schedule_work(&host->fsm_task);
  1219. DPRINTK("EXIT\n");
  1220. return 0;
  1221. }
  1222. static int carm_init_disks(struct carm_host *host)
  1223. {
  1224. unsigned int i;
  1225. int rc = 0;
  1226. for (i = 0; i < CARM_MAX_PORTS; i++) {
  1227. struct gendisk *disk;
  1228. struct request_queue *q;
  1229. struct carm_port *port;
  1230. port = &host->port[i];
  1231. port->host = host;
  1232. port->port_no = i;
  1233. disk = alloc_disk(CARM_MINORS_PER_MAJOR);
  1234. if (!disk) {
  1235. rc = -ENOMEM;
  1236. break;
  1237. }
  1238. port->disk = disk;
  1239. sprintf(disk->disk_name, DRV_NAME "/%u",
  1240. (unsigned int) (host->id * CARM_MAX_PORTS) + i);
  1241. disk->major = host->major;
  1242. disk->first_minor = i * CARM_MINORS_PER_MAJOR;
  1243. disk->fops = &carm_bd_ops;
  1244. disk->private_data = port;
  1245. q = blk_init_queue(carm_rq_fn, &host->lock);
  1246. if (!q) {
  1247. rc = -ENOMEM;
  1248. break;
  1249. }
  1250. disk->queue = q;
  1251. blk_queue_max_hw_segments(q, CARM_MAX_REQ_SG);
  1252. blk_queue_max_phys_segments(q, CARM_MAX_REQ_SG);
  1253. blk_queue_segment_boundary(q, CARM_SG_BOUNDARY);
  1254. q->queuedata = port;
  1255. }
  1256. return rc;
  1257. }
  1258. static void carm_free_disks(struct carm_host *host)
  1259. {
  1260. unsigned int i;
  1261. for (i = 0; i < CARM_MAX_PORTS; i++) {
  1262. struct gendisk *disk = host->port[i].disk;
  1263. if (disk) {
  1264. struct request_queue *q = disk->queue;
  1265. if (disk->flags & GENHD_FL_UP)
  1266. del_gendisk(disk);
  1267. if (q)
  1268. blk_cleanup_queue(q);
  1269. put_disk(disk);
  1270. }
  1271. }
  1272. }
  1273. static int carm_init_shm(struct carm_host *host)
  1274. {
  1275. host->shm = pci_alloc_consistent(host->pdev, CARM_SHM_SIZE,
  1276. &host->shm_dma);
  1277. if (!host->shm)
  1278. return -ENOMEM;
  1279. host->msg_base = host->shm + RBUF_LEN;
  1280. host->msg_dma = host->shm_dma + RBUF_LEN;
  1281. memset(host->shm, 0xff, RBUF_LEN);
  1282. memset(host->msg_base, 0, PDC_SHM_SIZE - RBUF_LEN);
  1283. return 0;
  1284. }
  1285. static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1286. {
  1287. static unsigned int printed_version;
  1288. struct carm_host *host;
  1289. unsigned int pci_dac;
  1290. int rc;
  1291. struct request_queue *q;
  1292. unsigned int i;
  1293. if (!printed_version++)
  1294. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  1295. rc = pci_enable_device(pdev);
  1296. if (rc)
  1297. return rc;
  1298. rc = pci_request_regions(pdev, DRV_NAME);
  1299. if (rc)
  1300. goto err_out;
  1301. #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
  1302. rc = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  1303. if (!rc) {
  1304. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1305. if (rc) {
  1306. printk(KERN_ERR DRV_NAME "(%s): consistent DMA mask failure\n",
  1307. pci_name(pdev));
  1308. goto err_out_regions;
  1309. }
  1310. pci_dac = 1;
  1311. } else {
  1312. #endif
  1313. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1314. if (rc) {
  1315. printk(KERN_ERR DRV_NAME "(%s): DMA mask failure\n",
  1316. pci_name(pdev));
  1317. goto err_out_regions;
  1318. }
  1319. pci_dac = 0;
  1320. #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
  1321. }
  1322. #endif
  1323. host = kzalloc(sizeof(*host), GFP_KERNEL);
  1324. if (!host) {
  1325. printk(KERN_ERR DRV_NAME "(%s): memory alloc failure\n",
  1326. pci_name(pdev));
  1327. rc = -ENOMEM;
  1328. goto err_out_regions;
  1329. }
  1330. host->pdev = pdev;
  1331. host->flags = pci_dac ? FL_DAC : 0;
  1332. spin_lock_init(&host->lock);
  1333. INIT_WORK(&host->fsm_task, carm_fsm_task);
  1334. init_completion(&host->probe_comp);
  1335. for (i = 0; i < ARRAY_SIZE(host->req); i++)
  1336. host->req[i].tag = i;
  1337. host->mmio = ioremap(pci_resource_start(pdev, 0),
  1338. pci_resource_len(pdev, 0));
  1339. if (!host->mmio) {
  1340. printk(KERN_ERR DRV_NAME "(%s): MMIO alloc failure\n",
  1341. pci_name(pdev));
  1342. rc = -ENOMEM;
  1343. goto err_out_kfree;
  1344. }
  1345. rc = carm_init_shm(host);
  1346. if (rc) {
  1347. printk(KERN_ERR DRV_NAME "(%s): DMA SHM alloc failure\n",
  1348. pci_name(pdev));
  1349. goto err_out_iounmap;
  1350. }
  1351. q = blk_init_queue(carm_oob_rq_fn, &host->lock);
  1352. if (!q) {
  1353. printk(KERN_ERR DRV_NAME "(%s): OOB queue alloc failure\n",
  1354. pci_name(pdev));
  1355. rc = -ENOMEM;
  1356. goto err_out_pci_free;
  1357. }
  1358. host->oob_q = q;
  1359. q->queuedata = host;
  1360. /*
  1361. * Figure out which major to use: 160, 161, or dynamic
  1362. */
  1363. if (!test_and_set_bit(0, &carm_major_alloc))
  1364. host->major = 160;
  1365. else if (!test_and_set_bit(1, &carm_major_alloc))
  1366. host->major = 161;
  1367. else
  1368. host->flags |= FL_DYN_MAJOR;
  1369. host->id = carm_host_id;
  1370. sprintf(host->name, DRV_NAME "%d", carm_host_id);
  1371. rc = register_blkdev(host->major, host->name);
  1372. if (rc < 0)
  1373. goto err_out_free_majors;
  1374. if (host->flags & FL_DYN_MAJOR)
  1375. host->major = rc;
  1376. rc = carm_init_disks(host);
  1377. if (rc)
  1378. goto err_out_blkdev_disks;
  1379. pci_set_master(pdev);
  1380. rc = request_irq(pdev->irq, carm_interrupt, IRQF_SHARED, DRV_NAME, host);
  1381. if (rc) {
  1382. printk(KERN_ERR DRV_NAME "(%s): irq alloc failure\n",
  1383. pci_name(pdev));
  1384. goto err_out_blkdev_disks;
  1385. }
  1386. rc = carm_init_host(host);
  1387. if (rc)
  1388. goto err_out_free_irq;
  1389. DPRINTK("waiting for probe_comp\n");
  1390. wait_for_completion(&host->probe_comp);
  1391. printk(KERN_INFO "%s: pci %s, ports %d, io %llx, irq %u, major %d\n",
  1392. host->name, pci_name(pdev), (int) CARM_MAX_PORTS,
  1393. (unsigned long long)pci_resource_start(pdev, 0),
  1394. pdev->irq, host->major);
  1395. carm_host_id++;
  1396. pci_set_drvdata(pdev, host);
  1397. return 0;
  1398. err_out_free_irq:
  1399. free_irq(pdev->irq, host);
  1400. err_out_blkdev_disks:
  1401. carm_free_disks(host);
  1402. unregister_blkdev(host->major, host->name);
  1403. err_out_free_majors:
  1404. if (host->major == 160)
  1405. clear_bit(0, &carm_major_alloc);
  1406. else if (host->major == 161)
  1407. clear_bit(1, &carm_major_alloc);
  1408. blk_cleanup_queue(host->oob_q);
  1409. err_out_pci_free:
  1410. pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
  1411. err_out_iounmap:
  1412. iounmap(host->mmio);
  1413. err_out_kfree:
  1414. kfree(host);
  1415. err_out_regions:
  1416. pci_release_regions(pdev);
  1417. err_out:
  1418. pci_disable_device(pdev);
  1419. return rc;
  1420. }
  1421. static void carm_remove_one (struct pci_dev *pdev)
  1422. {
  1423. struct carm_host *host = pci_get_drvdata(pdev);
  1424. if (!host) {
  1425. printk(KERN_ERR PFX "BUG: no host data for PCI(%s)\n",
  1426. pci_name(pdev));
  1427. return;
  1428. }
  1429. free_irq(pdev->irq, host);
  1430. carm_free_disks(host);
  1431. unregister_blkdev(host->major, host->name);
  1432. if (host->major == 160)
  1433. clear_bit(0, &carm_major_alloc);
  1434. else if (host->major == 161)
  1435. clear_bit(1, &carm_major_alloc);
  1436. blk_cleanup_queue(host->oob_q);
  1437. pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
  1438. iounmap(host->mmio);
  1439. kfree(host);
  1440. pci_release_regions(pdev);
  1441. pci_disable_device(pdev);
  1442. pci_set_drvdata(pdev, NULL);
  1443. }
  1444. static int __init carm_init(void)
  1445. {
  1446. return pci_register_driver(&carm_driver);
  1447. }
  1448. static void __exit carm_exit(void)
  1449. {
  1450. pci_unregister_driver(&carm_driver);
  1451. }
  1452. module_init(carm_init);
  1453. module_exit(carm_exit);