sx8.c 40 KB

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