sx8.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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 <linux/scatterlist.h>
  30. #include <asm/io.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, __func__, ## args)
  74. #ifdef CARM_VERBOSE_DEBUG
  75. #define VPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __func__, ## 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__, __func__, __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. struct request_queue *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. struct request_queue *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 completion probe_comp;
  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_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  349. static const struct pci_device_id carm_pci_tbl[] = {
  350. { PCI_VENDOR_ID_PROMISE, 0x8000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  351. { PCI_VENDOR_ID_PROMISE, 0x8002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  352. { } /* terminate list */
  353. };
  354. MODULE_DEVICE_TABLE(pci, carm_pci_tbl);
  355. static struct pci_driver carm_driver = {
  356. .name = DRV_NAME,
  357. .id_table = carm_pci_tbl,
  358. .probe = carm_init_one,
  359. .remove = carm_remove_one,
  360. };
  361. static const struct block_device_operations carm_bd_ops = {
  362. .owner = THIS_MODULE,
  363. .getgeo = carm_bdev_getgeo,
  364. };
  365. static unsigned int carm_host_id;
  366. static unsigned long carm_major_alloc;
  367. static int carm_bdev_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  368. {
  369. struct carm_port *port = bdev->bd_disk->private_data;
  370. geo->heads = (u8) port->dev_geom_head;
  371. geo->sectors = (u8) port->dev_geom_sect;
  372. geo->cylinders = port->dev_geom_cyl;
  373. return 0;
  374. }
  375. static const u32 msg_sizes[] = { 32, 64, 128, CARM_MSG_SIZE };
  376. static inline int carm_lookup_bucket(u32 msg_size)
  377. {
  378. int i;
  379. for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
  380. if (msg_size <= msg_sizes[i])
  381. return i;
  382. return -ENOENT;
  383. }
  384. static void carm_init_buckets(void __iomem *mmio)
  385. {
  386. unsigned int i;
  387. for (i = 0; i < ARRAY_SIZE(msg_sizes); i++)
  388. writel(msg_sizes[i], mmio + CARM_CMS0 + (4 * i));
  389. }
  390. static inline void *carm_ref_msg(struct carm_host *host,
  391. unsigned int msg_idx)
  392. {
  393. return host->msg_base + (msg_idx * CARM_MSG_SIZE);
  394. }
  395. static inline dma_addr_t carm_ref_msg_dma(struct carm_host *host,
  396. unsigned int msg_idx)
  397. {
  398. return host->msg_dma + (msg_idx * CARM_MSG_SIZE);
  399. }
  400. static int carm_send_msg(struct carm_host *host,
  401. struct carm_request *crq)
  402. {
  403. void __iomem *mmio = host->mmio;
  404. u32 msg = (u32) carm_ref_msg_dma(host, crq->tag);
  405. u32 cm_bucket = crq->msg_bucket;
  406. u32 tmp;
  407. int rc = 0;
  408. VPRINTK("ENTER\n");
  409. tmp = readl(mmio + CARM_HMUC);
  410. if (tmp & CARM_Q_FULL) {
  411. #if 0
  412. tmp = readl(mmio + CARM_INT_MASK);
  413. tmp |= INT_Q_AVAILABLE;
  414. writel(tmp, mmio + CARM_INT_MASK);
  415. readl(mmio + CARM_INT_MASK); /* flush */
  416. #endif
  417. DPRINTK("host msg queue full\n");
  418. rc = -EBUSY;
  419. } else {
  420. writel(msg | (cm_bucket << 1), mmio + CARM_IHQP);
  421. readl(mmio + CARM_IHQP); /* flush */
  422. }
  423. return rc;
  424. }
  425. static struct carm_request *carm_get_request(struct carm_host *host)
  426. {
  427. unsigned int i;
  428. /* obey global hardware limit on S/G entries */
  429. if (host->hw_sg_used >= (CARM_MAX_HOST_SG - CARM_MAX_REQ_SG))
  430. return NULL;
  431. for (i = 0; i < max_queue; i++)
  432. if ((host->msg_alloc & (1ULL << i)) == 0) {
  433. struct carm_request *crq = &host->req[i];
  434. crq->port = NULL;
  435. crq->n_elem = 0;
  436. host->msg_alloc |= (1ULL << i);
  437. host->n_msgs++;
  438. assert(host->n_msgs <= CARM_MAX_REQ);
  439. sg_init_table(crq->sg, CARM_MAX_REQ_SG);
  440. return crq;
  441. }
  442. DPRINTK("no request available, returning NULL\n");
  443. return NULL;
  444. }
  445. static int carm_put_request(struct carm_host *host, struct carm_request *crq)
  446. {
  447. assert(crq->tag < max_queue);
  448. if (unlikely((host->msg_alloc & (1ULL << crq->tag)) == 0))
  449. return -EINVAL; /* tried to clear a tag that was not active */
  450. assert(host->hw_sg_used >= crq->n_elem);
  451. host->msg_alloc &= ~(1ULL << crq->tag);
  452. host->hw_sg_used -= crq->n_elem;
  453. host->n_msgs--;
  454. return 0;
  455. }
  456. static struct carm_request *carm_get_special(struct carm_host *host)
  457. {
  458. unsigned long flags;
  459. struct carm_request *crq = NULL;
  460. struct request *rq;
  461. int tries = 5000;
  462. while (tries-- > 0) {
  463. spin_lock_irqsave(&host->lock, flags);
  464. crq = carm_get_request(host);
  465. spin_unlock_irqrestore(&host->lock, flags);
  466. if (crq)
  467. break;
  468. msleep(10);
  469. }
  470. if (!crq)
  471. return NULL;
  472. rq = blk_get_request(host->oob_q, WRITE /* bogus */, GFP_KERNEL);
  473. if (IS_ERR(rq)) {
  474. spin_lock_irqsave(&host->lock, flags);
  475. carm_put_request(host, crq);
  476. spin_unlock_irqrestore(&host->lock, flags);
  477. return NULL;
  478. }
  479. crq->rq = rq;
  480. return crq;
  481. }
  482. static int carm_array_info (struct carm_host *host, unsigned int array_idx)
  483. {
  484. struct carm_msg_ioctl *ioc;
  485. unsigned int idx;
  486. u32 msg_data;
  487. dma_addr_t msg_dma;
  488. struct carm_request *crq;
  489. int rc;
  490. crq = carm_get_special(host);
  491. if (!crq) {
  492. rc = -ENOMEM;
  493. goto err_out;
  494. }
  495. idx = crq->tag;
  496. ioc = carm_ref_msg(host, idx);
  497. msg_dma = carm_ref_msg_dma(host, idx);
  498. msg_data = (u32) (msg_dma + sizeof(struct carm_array_info));
  499. crq->msg_type = CARM_MSG_ARRAY;
  500. crq->msg_subtype = CARM_ARRAY_INFO;
  501. rc = carm_lookup_bucket(sizeof(struct carm_msg_ioctl) +
  502. sizeof(struct carm_array_info));
  503. BUG_ON(rc < 0);
  504. crq->msg_bucket = (u32) rc;
  505. memset(ioc, 0, sizeof(*ioc));
  506. ioc->type = CARM_MSG_ARRAY;
  507. ioc->subtype = CARM_ARRAY_INFO;
  508. ioc->array_id = (u8) array_idx;
  509. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  510. ioc->data_addr = cpu_to_le32(msg_data);
  511. spin_lock_irq(&host->lock);
  512. assert(host->state == HST_DEV_SCAN_START ||
  513. host->state == HST_DEV_SCAN);
  514. spin_unlock_irq(&host->lock);
  515. DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
  516. crq->rq->cmd_type = REQ_TYPE_DRV_PRIV;
  517. crq->rq->special = crq;
  518. blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
  519. return 0;
  520. err_out:
  521. spin_lock_irq(&host->lock);
  522. host->state = HST_ERROR;
  523. spin_unlock_irq(&host->lock);
  524. return rc;
  525. }
  526. typedef unsigned int (*carm_sspc_t)(struct carm_host *, unsigned int, void *);
  527. static int carm_send_special (struct carm_host *host, carm_sspc_t func)
  528. {
  529. struct carm_request *crq;
  530. struct carm_msg_ioctl *ioc;
  531. void *mem;
  532. unsigned int idx, msg_size;
  533. int rc;
  534. crq = carm_get_special(host);
  535. if (!crq)
  536. return -ENOMEM;
  537. idx = crq->tag;
  538. mem = carm_ref_msg(host, idx);
  539. msg_size = func(host, idx, mem);
  540. ioc = mem;
  541. crq->msg_type = ioc->type;
  542. crq->msg_subtype = ioc->subtype;
  543. rc = carm_lookup_bucket(msg_size);
  544. BUG_ON(rc < 0);
  545. crq->msg_bucket = (u32) rc;
  546. DPRINTK("blk_execute_rq_nowait, tag == %u\n", idx);
  547. crq->rq->cmd_type = REQ_TYPE_DRV_PRIV;
  548. crq->rq->special = crq;
  549. blk_execute_rq_nowait(host->oob_q, NULL, crq->rq, true, NULL);
  550. return 0;
  551. }
  552. static unsigned int carm_fill_sync_time(struct carm_host *host,
  553. unsigned int idx, void *mem)
  554. {
  555. struct timeval tv;
  556. struct carm_msg_sync_time *st = mem;
  557. do_gettimeofday(&tv);
  558. memset(st, 0, sizeof(*st));
  559. st->type = CARM_MSG_MISC;
  560. st->subtype = MISC_SET_TIME;
  561. st->handle = cpu_to_le32(TAG_ENCODE(idx));
  562. st->timestamp = cpu_to_le32(tv.tv_sec);
  563. return sizeof(struct carm_msg_sync_time);
  564. }
  565. static unsigned int carm_fill_alloc_buf(struct carm_host *host,
  566. unsigned int idx, void *mem)
  567. {
  568. struct carm_msg_allocbuf *ab = mem;
  569. memset(ab, 0, sizeof(*ab));
  570. ab->type = CARM_MSG_MISC;
  571. ab->subtype = MISC_ALLOC_MEM;
  572. ab->handle = cpu_to_le32(TAG_ENCODE(idx));
  573. ab->n_sg = 1;
  574. ab->sg_type = SGT_32BIT;
  575. ab->addr = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
  576. ab->len = cpu_to_le32(PDC_SHM_SIZE >> 1);
  577. ab->evt_pool = cpu_to_le32(host->shm_dma + (16 * 1024));
  578. ab->n_evt = cpu_to_le32(1024);
  579. ab->rbuf_pool = cpu_to_le32(host->shm_dma);
  580. ab->n_rbuf = cpu_to_le32(RMSG_Q_LEN);
  581. ab->msg_pool = cpu_to_le32(host->shm_dma + RBUF_LEN);
  582. ab->n_msg = cpu_to_le32(CARM_Q_LEN);
  583. ab->sg[0].start = cpu_to_le32(host->shm_dma + (PDC_SHM_SIZE >> 1));
  584. ab->sg[0].len = cpu_to_le32(65536);
  585. return sizeof(struct carm_msg_allocbuf);
  586. }
  587. static unsigned int carm_fill_scan_channels(struct carm_host *host,
  588. unsigned int idx, void *mem)
  589. {
  590. struct carm_msg_ioctl *ioc = mem;
  591. u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) +
  592. IOC_SCAN_CHAN_OFFSET);
  593. memset(ioc, 0, sizeof(*ioc));
  594. ioc->type = CARM_MSG_IOCTL;
  595. ioc->subtype = CARM_IOC_SCAN_CHAN;
  596. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  597. ioc->data_addr = cpu_to_le32(msg_data);
  598. /* fill output data area with "no device" default values */
  599. mem += IOC_SCAN_CHAN_OFFSET;
  600. memset(mem, IOC_SCAN_CHAN_NODEV, CARM_MAX_PORTS);
  601. return IOC_SCAN_CHAN_OFFSET + CARM_MAX_PORTS;
  602. }
  603. static unsigned int carm_fill_get_fw_ver(struct carm_host *host,
  604. unsigned int idx, void *mem)
  605. {
  606. struct carm_msg_get_fw_ver *ioc = mem;
  607. u32 msg_data = (u32) (carm_ref_msg_dma(host, idx) + sizeof(*ioc));
  608. memset(ioc, 0, sizeof(*ioc));
  609. ioc->type = CARM_MSG_MISC;
  610. ioc->subtype = MISC_GET_FW_VER;
  611. ioc->handle = cpu_to_le32(TAG_ENCODE(idx));
  612. ioc->data_addr = cpu_to_le32(msg_data);
  613. return sizeof(struct carm_msg_get_fw_ver) +
  614. sizeof(struct carm_fw_ver);
  615. }
  616. static inline void carm_end_request_queued(struct carm_host *host,
  617. struct carm_request *crq,
  618. int error)
  619. {
  620. struct request *req = crq->rq;
  621. int rc;
  622. __blk_end_request_all(req, error);
  623. rc = carm_put_request(host, crq);
  624. assert(rc == 0);
  625. }
  626. static inline void carm_push_q (struct carm_host *host, struct request_queue *q)
  627. {
  628. unsigned int idx = host->wait_q_prod % CARM_MAX_WAIT_Q;
  629. blk_stop_queue(q);
  630. VPRINTK("STOPPED QUEUE %p\n", q);
  631. host->wait_q[idx] = q;
  632. host->wait_q_prod++;
  633. BUG_ON(host->wait_q_prod == host->wait_q_cons); /* overrun */
  634. }
  635. static inline struct request_queue *carm_pop_q(struct carm_host *host)
  636. {
  637. unsigned int idx;
  638. if (host->wait_q_prod == host->wait_q_cons)
  639. return NULL;
  640. idx = host->wait_q_cons % CARM_MAX_WAIT_Q;
  641. host->wait_q_cons++;
  642. return host->wait_q[idx];
  643. }
  644. static inline void carm_round_robin(struct carm_host *host)
  645. {
  646. struct request_queue *q = carm_pop_q(host);
  647. if (q) {
  648. blk_start_queue(q);
  649. VPRINTK("STARTED QUEUE %p\n", q);
  650. }
  651. }
  652. static inline void carm_end_rq(struct carm_host *host, struct carm_request *crq,
  653. int error)
  654. {
  655. carm_end_request_queued(host, crq, error);
  656. if (max_queue == 1)
  657. carm_round_robin(host);
  658. else if ((host->n_msgs <= CARM_MSG_LOW_WATER) &&
  659. (host->hw_sg_used <= CARM_SG_LOW_WATER)) {
  660. carm_round_robin(host);
  661. }
  662. }
  663. static void carm_oob_rq_fn(struct request_queue *q)
  664. {
  665. struct carm_host *host = q->queuedata;
  666. struct carm_request *crq;
  667. struct request *rq;
  668. int rc;
  669. while (1) {
  670. DPRINTK("get req\n");
  671. rq = blk_fetch_request(q);
  672. if (!rq)
  673. break;
  674. crq = rq->special;
  675. assert(crq != NULL);
  676. assert(crq->rq == rq);
  677. crq->n_elem = 0;
  678. DPRINTK("send req\n");
  679. rc = carm_send_msg(host, crq);
  680. if (rc) {
  681. blk_requeue_request(q, rq);
  682. carm_push_q(host, q);
  683. return; /* call us again later, eventually */
  684. }
  685. }
  686. }
  687. static void carm_rq_fn(struct request_queue *q)
  688. {
  689. struct carm_port *port = q->queuedata;
  690. struct carm_host *host = port->host;
  691. struct carm_msg_rw *msg;
  692. struct carm_request *crq;
  693. struct request *rq;
  694. struct scatterlist *sg;
  695. int writing = 0, pci_dir, i, n_elem, rc;
  696. u32 tmp;
  697. unsigned int msg_size;
  698. queue_one_request:
  699. VPRINTK("get req\n");
  700. rq = blk_peek_request(q);
  701. if (!rq)
  702. return;
  703. crq = carm_get_request(host);
  704. if (!crq) {
  705. carm_push_q(host, q);
  706. return; /* call us again later, eventually */
  707. }
  708. crq->rq = rq;
  709. blk_start_request(rq);
  710. if (rq_data_dir(rq) == WRITE) {
  711. writing = 1;
  712. pci_dir = PCI_DMA_TODEVICE;
  713. } else {
  714. pci_dir = PCI_DMA_FROMDEVICE;
  715. }
  716. /* get scatterlist from block layer */
  717. sg = &crq->sg[0];
  718. n_elem = blk_rq_map_sg(q, rq, sg);
  719. if (n_elem <= 0) {
  720. carm_end_rq(host, crq, -EIO);
  721. return; /* request with no s/g entries? */
  722. }
  723. /* map scatterlist to PCI bus addresses */
  724. n_elem = pci_map_sg(host->pdev, sg, n_elem, pci_dir);
  725. if (n_elem <= 0) {
  726. carm_end_rq(host, crq, -EIO);
  727. return; /* request with no s/g entries? */
  728. }
  729. crq->n_elem = n_elem;
  730. crq->port = port;
  731. host->hw_sg_used += n_elem;
  732. /*
  733. * build read/write message
  734. */
  735. VPRINTK("build msg\n");
  736. msg = (struct carm_msg_rw *) carm_ref_msg(host, crq->tag);
  737. if (writing) {
  738. msg->type = CARM_MSG_WRITE;
  739. crq->msg_type = CARM_MSG_WRITE;
  740. } else {
  741. msg->type = CARM_MSG_READ;
  742. crq->msg_type = CARM_MSG_READ;
  743. }
  744. msg->id = port->port_no;
  745. msg->sg_count = n_elem;
  746. msg->sg_type = SGT_32BIT;
  747. msg->handle = cpu_to_le32(TAG_ENCODE(crq->tag));
  748. msg->lba = cpu_to_le32(blk_rq_pos(rq) & 0xffffffff);
  749. tmp = (blk_rq_pos(rq) >> 16) >> 16;
  750. msg->lba_high = cpu_to_le16( (u16) tmp );
  751. msg->lba_count = cpu_to_le16(blk_rq_sectors(rq));
  752. msg_size = sizeof(struct carm_msg_rw) - sizeof(msg->sg);
  753. for (i = 0; i < n_elem; i++) {
  754. struct carm_msg_sg *carm_sg = &msg->sg[i];
  755. carm_sg->start = cpu_to_le32(sg_dma_address(&crq->sg[i]));
  756. carm_sg->len = cpu_to_le32(sg_dma_len(&crq->sg[i]));
  757. msg_size += sizeof(struct carm_msg_sg);
  758. }
  759. rc = carm_lookup_bucket(msg_size);
  760. BUG_ON(rc < 0);
  761. crq->msg_bucket = (u32) rc;
  762. /*
  763. * queue read/write message to hardware
  764. */
  765. VPRINTK("send msg, tag == %u\n", crq->tag);
  766. rc = carm_send_msg(host, crq);
  767. if (rc) {
  768. carm_put_request(host, crq);
  769. blk_requeue_request(q, rq);
  770. carm_push_q(host, q);
  771. return; /* call us again later, eventually */
  772. }
  773. goto queue_one_request;
  774. }
  775. static void carm_handle_array_info(struct carm_host *host,
  776. struct carm_request *crq, u8 *mem,
  777. int error)
  778. {
  779. struct carm_port *port;
  780. u8 *msg_data = mem + sizeof(struct carm_array_info);
  781. struct carm_array_info *desc = (struct carm_array_info *) msg_data;
  782. u64 lo, hi;
  783. int cur_port;
  784. size_t slen;
  785. DPRINTK("ENTER\n");
  786. carm_end_rq(host, crq, error);
  787. if (error)
  788. goto out;
  789. if (le32_to_cpu(desc->array_status) & ARRAY_NO_EXIST)
  790. goto out;
  791. cur_port = host->cur_scan_dev;
  792. /* should never occur */
  793. if ((cur_port < 0) || (cur_port >= CARM_MAX_PORTS)) {
  794. printk(KERN_ERR PFX "BUG: cur_scan_dev==%d, array_id==%d\n",
  795. cur_port, (int) desc->array_id);
  796. goto out;
  797. }
  798. port = &host->port[cur_port];
  799. lo = (u64) le32_to_cpu(desc->size);
  800. hi = (u64) le16_to_cpu(desc->size_hi);
  801. port->capacity = lo | (hi << 32);
  802. port->dev_geom_head = le16_to_cpu(desc->head);
  803. port->dev_geom_sect = le16_to_cpu(desc->sect);
  804. port->dev_geom_cyl = le16_to_cpu(desc->cyl);
  805. host->dev_active |= (1 << cur_port);
  806. strncpy(port->name, desc->name, sizeof(port->name));
  807. port->name[sizeof(port->name) - 1] = 0;
  808. slen = strlen(port->name);
  809. while (slen && (port->name[slen - 1] == ' ')) {
  810. port->name[slen - 1] = 0;
  811. slen--;
  812. }
  813. printk(KERN_INFO DRV_NAME "(%s): port %u device %Lu sectors\n",
  814. pci_name(host->pdev), port->port_no,
  815. (unsigned long long) port->capacity);
  816. printk(KERN_INFO DRV_NAME "(%s): port %u device \"%s\"\n",
  817. pci_name(host->pdev), port->port_no, port->name);
  818. out:
  819. assert(host->state == HST_DEV_SCAN);
  820. schedule_work(&host->fsm_task);
  821. }
  822. static void carm_handle_scan_chan(struct carm_host *host,
  823. struct carm_request *crq, u8 *mem,
  824. int error)
  825. {
  826. u8 *msg_data = mem + IOC_SCAN_CHAN_OFFSET;
  827. unsigned int i, dev_count = 0;
  828. int new_state = HST_DEV_SCAN_START;
  829. DPRINTK("ENTER\n");
  830. carm_end_rq(host, crq, error);
  831. if (error) {
  832. new_state = HST_ERROR;
  833. goto out;
  834. }
  835. /* TODO: scan and support non-disk devices */
  836. for (i = 0; i < 8; i++)
  837. if (msg_data[i] == 0) { /* direct-access device (disk) */
  838. host->dev_present |= (1 << i);
  839. dev_count++;
  840. }
  841. printk(KERN_INFO DRV_NAME "(%s): found %u interesting devices\n",
  842. pci_name(host->pdev), dev_count);
  843. out:
  844. assert(host->state == HST_PORT_SCAN);
  845. host->state = new_state;
  846. schedule_work(&host->fsm_task);
  847. }
  848. static void carm_handle_generic(struct carm_host *host,
  849. struct carm_request *crq, int error,
  850. int cur_state, int next_state)
  851. {
  852. DPRINTK("ENTER\n");
  853. carm_end_rq(host, crq, error);
  854. assert(host->state == cur_state);
  855. if (error)
  856. host->state = HST_ERROR;
  857. else
  858. host->state = next_state;
  859. schedule_work(&host->fsm_task);
  860. }
  861. static inline void carm_handle_rw(struct carm_host *host,
  862. struct carm_request *crq, int error)
  863. {
  864. int pci_dir;
  865. VPRINTK("ENTER\n");
  866. if (rq_data_dir(crq->rq) == WRITE)
  867. pci_dir = PCI_DMA_TODEVICE;
  868. else
  869. pci_dir = PCI_DMA_FROMDEVICE;
  870. pci_unmap_sg(host->pdev, &crq->sg[0], crq->n_elem, pci_dir);
  871. carm_end_rq(host, crq, error);
  872. }
  873. static inline void carm_handle_resp(struct carm_host *host,
  874. __le32 ret_handle_le, u32 status)
  875. {
  876. u32 handle = le32_to_cpu(ret_handle_le);
  877. unsigned int msg_idx;
  878. struct carm_request *crq;
  879. int error = (status == RMSG_OK) ? 0 : -EIO;
  880. u8 *mem;
  881. VPRINTK("ENTER, handle == 0x%x\n", handle);
  882. if (unlikely(!TAG_VALID(handle))) {
  883. printk(KERN_ERR DRV_NAME "(%s): BUG: invalid tag 0x%x\n",
  884. pci_name(host->pdev), handle);
  885. return;
  886. }
  887. msg_idx = TAG_DECODE(handle);
  888. VPRINTK("tag == %u\n", msg_idx);
  889. crq = &host->req[msg_idx];
  890. /* fast path */
  891. if (likely(crq->msg_type == CARM_MSG_READ ||
  892. crq->msg_type == CARM_MSG_WRITE)) {
  893. carm_handle_rw(host, crq, error);
  894. return;
  895. }
  896. mem = carm_ref_msg(host, msg_idx);
  897. switch (crq->msg_type) {
  898. case CARM_MSG_IOCTL: {
  899. switch (crq->msg_subtype) {
  900. case CARM_IOC_SCAN_CHAN:
  901. carm_handle_scan_chan(host, crq, mem, error);
  902. break;
  903. default:
  904. /* unknown / invalid response */
  905. goto err_out;
  906. }
  907. break;
  908. }
  909. case CARM_MSG_MISC: {
  910. switch (crq->msg_subtype) {
  911. case MISC_ALLOC_MEM:
  912. carm_handle_generic(host, crq, error,
  913. HST_ALLOC_BUF, HST_SYNC_TIME);
  914. break;
  915. case MISC_SET_TIME:
  916. carm_handle_generic(host, crq, error,
  917. HST_SYNC_TIME, HST_GET_FW_VER);
  918. break;
  919. case MISC_GET_FW_VER: {
  920. struct carm_fw_ver *ver = (struct carm_fw_ver *)
  921. (mem + sizeof(struct carm_msg_get_fw_ver));
  922. if (!error) {
  923. host->fw_ver = le32_to_cpu(ver->version);
  924. host->flags |= (ver->features & FL_FW_VER_MASK);
  925. }
  926. carm_handle_generic(host, crq, error,
  927. HST_GET_FW_VER, HST_PORT_SCAN);
  928. break;
  929. }
  930. default:
  931. /* unknown / invalid response */
  932. goto err_out;
  933. }
  934. break;
  935. }
  936. case CARM_MSG_ARRAY: {
  937. switch (crq->msg_subtype) {
  938. case CARM_ARRAY_INFO:
  939. carm_handle_array_info(host, crq, mem, error);
  940. break;
  941. default:
  942. /* unknown / invalid response */
  943. goto err_out;
  944. }
  945. break;
  946. }
  947. default:
  948. /* unknown / invalid response */
  949. goto err_out;
  950. }
  951. return;
  952. err_out:
  953. printk(KERN_WARNING DRV_NAME "(%s): BUG: unhandled message type %d/%d\n",
  954. pci_name(host->pdev), crq->msg_type, crq->msg_subtype);
  955. carm_end_rq(host, crq, -EIO);
  956. }
  957. static inline void carm_handle_responses(struct carm_host *host)
  958. {
  959. void __iomem *mmio = host->mmio;
  960. struct carm_response *resp = (struct carm_response *) host->shm;
  961. unsigned int work = 0;
  962. unsigned int idx = host->resp_idx % RMSG_Q_LEN;
  963. while (1) {
  964. u32 status = le32_to_cpu(resp[idx].status);
  965. if (status == 0xffffffff) {
  966. VPRINTK("ending response on index %u\n", idx);
  967. writel(idx << 3, mmio + CARM_RESP_IDX);
  968. break;
  969. }
  970. /* response to a message we sent */
  971. else if ((status & (1 << 31)) == 0) {
  972. VPRINTK("handling msg response on index %u\n", idx);
  973. carm_handle_resp(host, resp[idx].ret_handle, status);
  974. resp[idx].status = cpu_to_le32(0xffffffff);
  975. }
  976. /* asynchronous events the hardware throws our way */
  977. else if ((status & 0xff000000) == (1 << 31)) {
  978. u8 *evt_type_ptr = (u8 *) &resp[idx];
  979. u8 evt_type = *evt_type_ptr;
  980. printk(KERN_WARNING DRV_NAME "(%s): unhandled event type %d\n",
  981. pci_name(host->pdev), (int) evt_type);
  982. resp[idx].status = cpu_to_le32(0xffffffff);
  983. }
  984. idx = NEXT_RESP(idx);
  985. work++;
  986. }
  987. VPRINTK("EXIT, work==%u\n", work);
  988. host->resp_idx += work;
  989. }
  990. static irqreturn_t carm_interrupt(int irq, void *__host)
  991. {
  992. struct carm_host *host = __host;
  993. void __iomem *mmio;
  994. u32 mask;
  995. int handled = 0;
  996. unsigned long flags;
  997. if (!host) {
  998. VPRINTK("no host\n");
  999. return IRQ_NONE;
  1000. }
  1001. spin_lock_irqsave(&host->lock, flags);
  1002. mmio = host->mmio;
  1003. /* reading should also clear interrupts */
  1004. mask = readl(mmio + CARM_INT_STAT);
  1005. if (mask == 0 || mask == 0xffffffff) {
  1006. VPRINTK("no work, mask == 0x%x\n", mask);
  1007. goto out;
  1008. }
  1009. if (mask & INT_ACK_MASK)
  1010. writel(mask, mmio + CARM_INT_STAT);
  1011. if (unlikely(host->state == HST_INVALID)) {
  1012. VPRINTK("not initialized yet, mask = 0x%x\n", mask);
  1013. goto out;
  1014. }
  1015. if (mask & CARM_HAVE_RESP) {
  1016. handled = 1;
  1017. carm_handle_responses(host);
  1018. }
  1019. out:
  1020. spin_unlock_irqrestore(&host->lock, flags);
  1021. VPRINTK("EXIT\n");
  1022. return IRQ_RETVAL(handled);
  1023. }
  1024. static void carm_fsm_task (struct work_struct *work)
  1025. {
  1026. struct carm_host *host =
  1027. container_of(work, struct carm_host, fsm_task);
  1028. unsigned long flags;
  1029. unsigned int state;
  1030. int rc, i, next_dev;
  1031. int reschedule = 0;
  1032. int new_state = HST_INVALID;
  1033. spin_lock_irqsave(&host->lock, flags);
  1034. state = host->state;
  1035. spin_unlock_irqrestore(&host->lock, flags);
  1036. DPRINTK("ENTER, state == %s\n", state_name[state]);
  1037. switch (state) {
  1038. case HST_PROBE_START:
  1039. new_state = HST_ALLOC_BUF;
  1040. reschedule = 1;
  1041. break;
  1042. case HST_ALLOC_BUF:
  1043. rc = carm_send_special(host, carm_fill_alloc_buf);
  1044. if (rc) {
  1045. new_state = HST_ERROR;
  1046. reschedule = 1;
  1047. }
  1048. break;
  1049. case HST_SYNC_TIME:
  1050. rc = carm_send_special(host, carm_fill_sync_time);
  1051. if (rc) {
  1052. new_state = HST_ERROR;
  1053. reschedule = 1;
  1054. }
  1055. break;
  1056. case HST_GET_FW_VER:
  1057. rc = carm_send_special(host, carm_fill_get_fw_ver);
  1058. if (rc) {
  1059. new_state = HST_ERROR;
  1060. reschedule = 1;
  1061. }
  1062. break;
  1063. case HST_PORT_SCAN:
  1064. rc = carm_send_special(host, carm_fill_scan_channels);
  1065. if (rc) {
  1066. new_state = HST_ERROR;
  1067. reschedule = 1;
  1068. }
  1069. break;
  1070. case HST_DEV_SCAN_START:
  1071. host->cur_scan_dev = -1;
  1072. new_state = HST_DEV_SCAN;
  1073. reschedule = 1;
  1074. break;
  1075. case HST_DEV_SCAN:
  1076. next_dev = -1;
  1077. for (i = host->cur_scan_dev + 1; i < CARM_MAX_PORTS; i++)
  1078. if (host->dev_present & (1 << i)) {
  1079. next_dev = i;
  1080. break;
  1081. }
  1082. if (next_dev >= 0) {
  1083. host->cur_scan_dev = next_dev;
  1084. rc = carm_array_info(host, next_dev);
  1085. if (rc) {
  1086. new_state = HST_ERROR;
  1087. reschedule = 1;
  1088. }
  1089. } else {
  1090. new_state = HST_DEV_ACTIVATE;
  1091. reschedule = 1;
  1092. }
  1093. break;
  1094. case HST_DEV_ACTIVATE: {
  1095. int activated = 0;
  1096. for (i = 0; i < CARM_MAX_PORTS; i++)
  1097. if (host->dev_active & (1 << i)) {
  1098. struct carm_port *port = &host->port[i];
  1099. struct gendisk *disk = port->disk;
  1100. set_capacity(disk, port->capacity);
  1101. add_disk(disk);
  1102. activated++;
  1103. }
  1104. printk(KERN_INFO DRV_NAME "(%s): %d ports activated\n",
  1105. pci_name(host->pdev), activated);
  1106. new_state = HST_PROBE_FINISHED;
  1107. reschedule = 1;
  1108. break;
  1109. }
  1110. case HST_PROBE_FINISHED:
  1111. complete(&host->probe_comp);
  1112. break;
  1113. case HST_ERROR:
  1114. /* FIXME: TODO */
  1115. break;
  1116. default:
  1117. /* should never occur */
  1118. printk(KERN_ERR PFX "BUG: unknown state %d\n", state);
  1119. assert(0);
  1120. break;
  1121. }
  1122. if (new_state != HST_INVALID) {
  1123. spin_lock_irqsave(&host->lock, flags);
  1124. host->state = new_state;
  1125. spin_unlock_irqrestore(&host->lock, flags);
  1126. }
  1127. if (reschedule)
  1128. schedule_work(&host->fsm_task);
  1129. }
  1130. static int carm_init_wait(void __iomem *mmio, u32 bits, unsigned int test_bit)
  1131. {
  1132. unsigned int i;
  1133. for (i = 0; i < 50000; i++) {
  1134. u32 tmp = readl(mmio + CARM_LMUC);
  1135. udelay(100);
  1136. if (test_bit) {
  1137. if ((tmp & bits) == bits)
  1138. return 0;
  1139. } else {
  1140. if ((tmp & bits) == 0)
  1141. return 0;
  1142. }
  1143. cond_resched();
  1144. }
  1145. printk(KERN_ERR PFX "carm_init_wait timeout, bits == 0x%x, test_bit == %s\n",
  1146. bits, test_bit ? "yes" : "no");
  1147. return -EBUSY;
  1148. }
  1149. static void carm_init_responses(struct carm_host *host)
  1150. {
  1151. void __iomem *mmio = host->mmio;
  1152. unsigned int i;
  1153. struct carm_response *resp = (struct carm_response *) host->shm;
  1154. for (i = 0; i < RMSG_Q_LEN; i++)
  1155. resp[i].status = cpu_to_le32(0xffffffff);
  1156. writel(0, mmio + CARM_RESP_IDX);
  1157. }
  1158. static int carm_init_host(struct carm_host *host)
  1159. {
  1160. void __iomem *mmio = host->mmio;
  1161. u32 tmp;
  1162. u8 tmp8;
  1163. int rc;
  1164. DPRINTK("ENTER\n");
  1165. writel(0, mmio + CARM_INT_MASK);
  1166. tmp8 = readb(mmio + CARM_INITC);
  1167. if (tmp8 & 0x01) {
  1168. tmp8 &= ~0x01;
  1169. writeb(tmp8, mmio + CARM_INITC);
  1170. readb(mmio + CARM_INITC); /* flush */
  1171. DPRINTK("snooze...\n");
  1172. msleep(5000);
  1173. }
  1174. tmp = readl(mmio + CARM_HMUC);
  1175. if (tmp & CARM_CME) {
  1176. DPRINTK("CME bit present, waiting\n");
  1177. rc = carm_init_wait(mmio, CARM_CME, 1);
  1178. if (rc) {
  1179. DPRINTK("EXIT, carm_init_wait 1 failed\n");
  1180. return rc;
  1181. }
  1182. }
  1183. if (tmp & CARM_RME) {
  1184. DPRINTK("RME bit present, waiting\n");
  1185. rc = carm_init_wait(mmio, CARM_RME, 1);
  1186. if (rc) {
  1187. DPRINTK("EXIT, carm_init_wait 2 failed\n");
  1188. return rc;
  1189. }
  1190. }
  1191. tmp &= ~(CARM_RME | CARM_CME);
  1192. writel(tmp, mmio + CARM_HMUC);
  1193. readl(mmio + CARM_HMUC); /* flush */
  1194. rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 0);
  1195. if (rc) {
  1196. DPRINTK("EXIT, carm_init_wait 3 failed\n");
  1197. return rc;
  1198. }
  1199. carm_init_buckets(mmio);
  1200. writel(host->shm_dma & 0xffffffff, mmio + RBUF_ADDR_LO);
  1201. writel((host->shm_dma >> 16) >> 16, mmio + RBUF_ADDR_HI);
  1202. writel(RBUF_LEN, mmio + RBUF_BYTE_SZ);
  1203. tmp = readl(mmio + CARM_HMUC);
  1204. tmp |= (CARM_RME | CARM_CME | CARM_WZBC);
  1205. writel(tmp, mmio + CARM_HMUC);
  1206. readl(mmio + CARM_HMUC); /* flush */
  1207. rc = carm_init_wait(mmio, CARM_RME | CARM_CME, 1);
  1208. if (rc) {
  1209. DPRINTK("EXIT, carm_init_wait 4 failed\n");
  1210. return rc;
  1211. }
  1212. writel(0, mmio + CARM_HMPHA);
  1213. writel(INT_DEF_MASK, mmio + CARM_INT_MASK);
  1214. carm_init_responses(host);
  1215. /* start initialization, probing state machine */
  1216. spin_lock_irq(&host->lock);
  1217. assert(host->state == HST_INVALID);
  1218. host->state = HST_PROBE_START;
  1219. spin_unlock_irq(&host->lock);
  1220. schedule_work(&host->fsm_task);
  1221. DPRINTK("EXIT\n");
  1222. return 0;
  1223. }
  1224. static int carm_init_disks(struct carm_host *host)
  1225. {
  1226. unsigned int i;
  1227. int rc = 0;
  1228. for (i = 0; i < CARM_MAX_PORTS; i++) {
  1229. struct gendisk *disk;
  1230. struct request_queue *q;
  1231. struct carm_port *port;
  1232. port = &host->port[i];
  1233. port->host = host;
  1234. port->port_no = i;
  1235. disk = alloc_disk(CARM_MINORS_PER_MAJOR);
  1236. if (!disk) {
  1237. rc = -ENOMEM;
  1238. break;
  1239. }
  1240. port->disk = disk;
  1241. sprintf(disk->disk_name, DRV_NAME "/%u",
  1242. (unsigned int) (host->id * CARM_MAX_PORTS) + i);
  1243. disk->major = host->major;
  1244. disk->first_minor = i * CARM_MINORS_PER_MAJOR;
  1245. disk->fops = &carm_bd_ops;
  1246. disk->private_data = port;
  1247. q = blk_init_queue(carm_rq_fn, &host->lock);
  1248. if (!q) {
  1249. rc = -ENOMEM;
  1250. break;
  1251. }
  1252. disk->queue = q;
  1253. blk_queue_max_segments(q, CARM_MAX_REQ_SG);
  1254. blk_queue_segment_boundary(q, CARM_SG_BOUNDARY);
  1255. q->queuedata = port;
  1256. }
  1257. return rc;
  1258. }
  1259. static void carm_free_disks(struct carm_host *host)
  1260. {
  1261. unsigned int i;
  1262. for (i = 0; i < CARM_MAX_PORTS; i++) {
  1263. struct gendisk *disk = host->port[i].disk;
  1264. if (disk) {
  1265. struct request_queue *q = disk->queue;
  1266. if (disk->flags & GENHD_FL_UP)
  1267. del_gendisk(disk);
  1268. if (q)
  1269. blk_cleanup_queue(q);
  1270. put_disk(disk);
  1271. }
  1272. }
  1273. }
  1274. static int carm_init_shm(struct carm_host *host)
  1275. {
  1276. host->shm = pci_alloc_consistent(host->pdev, CARM_SHM_SIZE,
  1277. &host->shm_dma);
  1278. if (!host->shm)
  1279. return -ENOMEM;
  1280. host->msg_base = host->shm + RBUF_LEN;
  1281. host->msg_dma = host->shm_dma + RBUF_LEN;
  1282. memset(host->shm, 0xff, RBUF_LEN);
  1283. memset(host->msg_base, 0, PDC_SHM_SIZE - RBUF_LEN);
  1284. return 0;
  1285. }
  1286. static int carm_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1287. {
  1288. struct carm_host *host;
  1289. unsigned int pci_dac;
  1290. int rc;
  1291. struct request_queue *q;
  1292. unsigned int i;
  1293. printk_once(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  1294. rc = pci_enable_device(pdev);
  1295. if (rc)
  1296. return rc;
  1297. rc = pci_request_regions(pdev, DRV_NAME);
  1298. if (rc)
  1299. goto err_out;
  1300. #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
  1301. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1302. if (!rc) {
  1303. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1304. if (rc) {
  1305. printk(KERN_ERR DRV_NAME "(%s): consistent DMA mask failure\n",
  1306. pci_name(pdev));
  1307. goto err_out_regions;
  1308. }
  1309. pci_dac = 1;
  1310. } else {
  1311. #endif
  1312. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1313. if (rc) {
  1314. printk(KERN_ERR DRV_NAME "(%s): DMA mask failure\n",
  1315. pci_name(pdev));
  1316. goto err_out_regions;
  1317. }
  1318. pci_dac = 0;
  1319. #ifdef IF_64BIT_DMA_IS_POSSIBLE /* grrrr... */
  1320. }
  1321. #endif
  1322. host = kzalloc(sizeof(*host), GFP_KERNEL);
  1323. if (!host) {
  1324. printk(KERN_ERR DRV_NAME "(%s): memory alloc failure\n",
  1325. pci_name(pdev));
  1326. rc = -ENOMEM;
  1327. goto err_out_regions;
  1328. }
  1329. host->pdev = pdev;
  1330. host->flags = pci_dac ? FL_DAC : 0;
  1331. spin_lock_init(&host->lock);
  1332. INIT_WORK(&host->fsm_task, carm_fsm_task);
  1333. init_completion(&host->probe_comp);
  1334. for (i = 0; i < ARRAY_SIZE(host->req); i++)
  1335. host->req[i].tag = i;
  1336. host->mmio = ioremap(pci_resource_start(pdev, 0),
  1337. pci_resource_len(pdev, 0));
  1338. if (!host->mmio) {
  1339. printk(KERN_ERR DRV_NAME "(%s): MMIO alloc failure\n",
  1340. pci_name(pdev));
  1341. rc = -ENOMEM;
  1342. goto err_out_kfree;
  1343. }
  1344. rc = carm_init_shm(host);
  1345. if (rc) {
  1346. printk(KERN_ERR DRV_NAME "(%s): DMA SHM alloc failure\n",
  1347. pci_name(pdev));
  1348. goto err_out_iounmap;
  1349. }
  1350. q = blk_init_queue(carm_oob_rq_fn, &host->lock);
  1351. if (!q) {
  1352. printk(KERN_ERR DRV_NAME "(%s): OOB queue alloc failure\n",
  1353. pci_name(pdev));
  1354. rc = -ENOMEM;
  1355. goto err_out_pci_free;
  1356. }
  1357. host->oob_q = q;
  1358. q->queuedata = host;
  1359. /*
  1360. * Figure out which major to use: 160, 161, or dynamic
  1361. */
  1362. if (!test_and_set_bit(0, &carm_major_alloc))
  1363. host->major = 160;
  1364. else if (!test_and_set_bit(1, &carm_major_alloc))
  1365. host->major = 161;
  1366. else
  1367. host->flags |= FL_DYN_MAJOR;
  1368. host->id = carm_host_id;
  1369. sprintf(host->name, DRV_NAME "%d", carm_host_id);
  1370. rc = register_blkdev(host->major, host->name);
  1371. if (rc < 0)
  1372. goto err_out_free_majors;
  1373. if (host->flags & FL_DYN_MAJOR)
  1374. host->major = rc;
  1375. rc = carm_init_disks(host);
  1376. if (rc)
  1377. goto err_out_blkdev_disks;
  1378. pci_set_master(pdev);
  1379. rc = request_irq(pdev->irq, carm_interrupt, IRQF_SHARED, DRV_NAME, host);
  1380. if (rc) {
  1381. printk(KERN_ERR DRV_NAME "(%s): irq alloc failure\n",
  1382. pci_name(pdev));
  1383. goto err_out_blkdev_disks;
  1384. }
  1385. rc = carm_init_host(host);
  1386. if (rc)
  1387. goto err_out_free_irq;
  1388. DPRINTK("waiting for probe_comp\n");
  1389. wait_for_completion(&host->probe_comp);
  1390. printk(KERN_INFO "%s: pci %s, ports %d, io %llx, irq %u, major %d\n",
  1391. host->name, pci_name(pdev), (int) CARM_MAX_PORTS,
  1392. (unsigned long long)pci_resource_start(pdev, 0),
  1393. pdev->irq, host->major);
  1394. carm_host_id++;
  1395. pci_set_drvdata(pdev, host);
  1396. return 0;
  1397. err_out_free_irq:
  1398. free_irq(pdev->irq, host);
  1399. err_out_blkdev_disks:
  1400. carm_free_disks(host);
  1401. unregister_blkdev(host->major, host->name);
  1402. err_out_free_majors:
  1403. if (host->major == 160)
  1404. clear_bit(0, &carm_major_alloc);
  1405. else if (host->major == 161)
  1406. clear_bit(1, &carm_major_alloc);
  1407. blk_cleanup_queue(host->oob_q);
  1408. err_out_pci_free:
  1409. pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
  1410. err_out_iounmap:
  1411. iounmap(host->mmio);
  1412. err_out_kfree:
  1413. kfree(host);
  1414. err_out_regions:
  1415. pci_release_regions(pdev);
  1416. err_out:
  1417. pci_disable_device(pdev);
  1418. return rc;
  1419. }
  1420. static void carm_remove_one (struct pci_dev *pdev)
  1421. {
  1422. struct carm_host *host = pci_get_drvdata(pdev);
  1423. if (!host) {
  1424. printk(KERN_ERR PFX "BUG: no host data for PCI(%s)\n",
  1425. pci_name(pdev));
  1426. return;
  1427. }
  1428. free_irq(pdev->irq, host);
  1429. carm_free_disks(host);
  1430. unregister_blkdev(host->major, host->name);
  1431. if (host->major == 160)
  1432. clear_bit(0, &carm_major_alloc);
  1433. else if (host->major == 161)
  1434. clear_bit(1, &carm_major_alloc);
  1435. blk_cleanup_queue(host->oob_q);
  1436. pci_free_consistent(pdev, CARM_SHM_SIZE, host->shm, host->shm_dma);
  1437. iounmap(host->mmio);
  1438. kfree(host);
  1439. pci_release_regions(pdev);
  1440. pci_disable_device(pdev);
  1441. }
  1442. module_pci_driver(carm_driver);