sx8.c 40 KB

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