i2o_block.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * Block OSM
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * For the purpose of avoiding doubt the preferred form of the work
  19. * for making modifications shall be a standards compliant form such
  20. * gzipped tar and not one requiring a proprietary or patent encumbered
  21. * tool to unpack.
  22. *
  23. * Fixes/additions:
  24. * Steve Ralston:
  25. * Multiple device handling error fixes,
  26. * Added a queue depth.
  27. * Alan Cox:
  28. * FC920 has an rmw bug. Dont or in the end marker.
  29. * Removed queue walk, fixed for 64bitness.
  30. * Rewrote much of the code over time
  31. * Added indirect block lists
  32. * Handle 64K limits on many controllers
  33. * Don't use indirects on the Promise (breaks)
  34. * Heavily chop down the queue depths
  35. * Deepak Saxena:
  36. * Independent queues per IOP
  37. * Support for dynamic device creation/deletion
  38. * Code cleanup
  39. * Support for larger I/Os through merge* functions
  40. * (taken from DAC960 driver)
  41. * Boji T Kannanthanam:
  42. * Set the I2O Block devices to be detected in increasing
  43. * order of TIDs during boot.
  44. * Search and set the I2O block device that we boot off
  45. * from as the first device to be claimed (as /dev/i2o/hda)
  46. * Properly attach/detach I2O gendisk structure from the
  47. * system gendisk list. The I2O block devices now appear in
  48. * /proc/partitions.
  49. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  50. * Minor bugfixes for 2.6.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/i2o.h>
  54. #include <linux/mempool.h>
  55. #include <linux/genhd.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/hdreg.h>
  58. #include <scsi/scsi.h>
  59. #include "i2o_block.h"
  60. #define OSM_NAME "block-osm"
  61. #define OSM_VERSION "1.325"
  62. #define OSM_DESCRIPTION "I2O Block Device OSM"
  63. static struct i2o_driver i2o_block_driver;
  64. /* global Block OSM request mempool */
  65. static struct i2o_block_mempool i2o_blk_req_pool;
  66. /* Block OSM class handling definition */
  67. static struct i2o_class_id i2o_block_class_id[] = {
  68. {I2O_CLASS_RANDOM_BLOCK_STORAGE},
  69. {I2O_CLASS_END}
  70. };
  71. /**
  72. * i2o_block_device_free - free the memory of the I2O Block device
  73. * @dev: I2O Block device, which should be cleaned up
  74. *
  75. * Frees the request queue, gendisk and the i2o_block_device structure.
  76. */
  77. static void i2o_block_device_free(struct i2o_block_device *dev)
  78. {
  79. blk_cleanup_queue(dev->gd->queue);
  80. put_disk(dev->gd);
  81. kfree(dev);
  82. };
  83. /**
  84. * i2o_block_remove - remove the I2O Block device from the system again
  85. * @dev: I2O Block device which should be removed
  86. *
  87. * Remove gendisk from system and free all allocated memory.
  88. *
  89. * Always returns 0.
  90. */
  91. static int i2o_block_remove(struct device *dev)
  92. {
  93. struct i2o_device *i2o_dev = to_i2o_device(dev);
  94. struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);
  95. osm_info("device removed (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  96. i2o_blk_dev->gd->disk_name);
  97. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);
  98. del_gendisk(i2o_blk_dev->gd);
  99. dev_set_drvdata(dev, NULL);
  100. i2o_device_claim_release(i2o_dev);
  101. i2o_block_device_free(i2o_blk_dev);
  102. return 0;
  103. };
  104. /**
  105. * i2o_block_device flush - Flush all dirty data of I2O device dev
  106. * @dev: I2O device which should be flushed
  107. *
  108. * Flushes all dirty data on device dev.
  109. *
  110. * Returns 0 on success or negative error code on failure.
  111. */
  112. static int i2o_block_device_flush(struct i2o_device *dev)
  113. {
  114. struct i2o_message *msg;
  115. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  116. if (IS_ERR(msg))
  117. return PTR_ERR(msg);
  118. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  119. msg->u.head[1] =
  120. cpu_to_le32(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->
  121. lct_data.tid);
  122. msg->body[0] = cpu_to_le32(60 << 16);
  123. osm_debug("Flushing...\n");
  124. return i2o_msg_post_wait(dev->iop, msg, 60);
  125. };
  126. /**
  127. * i2o_block_device_mount - Mount (load) the media of device dev
  128. * @dev: I2O device which should receive the mount request
  129. * @media_id: Media Identifier
  130. *
  131. * Load a media into drive. Identifier should be set to -1, because the
  132. * spec does not support any other value.
  133. *
  134. * Returns 0 on success or negative error code on failure.
  135. */
  136. static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
  137. {
  138. struct i2o_message *msg;
  139. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  140. if (IS_ERR(msg))
  141. return PTR_ERR(msg);
  142. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  143. msg->u.head[1] =
  144. cpu_to_le32(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->
  145. lct_data.tid);
  146. msg->body[0] = cpu_to_le32(-1);
  147. msg->body[1] = cpu_to_le32(0x00000000);
  148. osm_debug("Mounting...\n");
  149. return i2o_msg_post_wait(dev->iop, msg, 2);
  150. };
  151. /**
  152. * i2o_block_device_lock - Locks the media of device dev
  153. * @dev: I2O device which should receive the lock request
  154. * @media_id: Media Identifier
  155. *
  156. * Lock media of device dev to prevent removal. The media identifier
  157. * should be set to -1, because the spec does not support any other value.
  158. *
  159. * Returns 0 on success or negative error code on failure.
  160. */
  161. static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
  162. {
  163. struct i2o_message *msg;
  164. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  165. if (IS_ERR(msg))
  166. return PTR_ERR(msg);
  167. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  168. msg->u.head[1] =
  169. cpu_to_le32(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->
  170. lct_data.tid);
  171. msg->body[0] = cpu_to_le32(-1);
  172. osm_debug("Locking...\n");
  173. return i2o_msg_post_wait(dev->iop, msg, 2);
  174. };
  175. /**
  176. * i2o_block_device_unlock - Unlocks the media of device dev
  177. * @dev: I2O device which should receive the unlocked request
  178. * @media_id: Media Identifier
  179. *
  180. * Unlocks the media in device dev. The media identifier should be set to
  181. * -1, because the spec does not support any other value.
  182. *
  183. * Returns 0 on success or negative error code on failure.
  184. */
  185. static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
  186. {
  187. struct i2o_message *msg;
  188. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  189. if (IS_ERR(msg))
  190. return PTR_ERR(msg);
  191. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  192. msg->u.head[1] =
  193. cpu_to_le32(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->
  194. lct_data.tid);
  195. msg->body[0] = cpu_to_le32(media_id);
  196. osm_debug("Unlocking...\n");
  197. return i2o_msg_post_wait(dev->iop, msg, 2);
  198. };
  199. /**
  200. * i2o_block_device_power - Power management for device dev
  201. * @dev: I2O device which should receive the power management request
  202. * @op: Operation to send
  203. *
  204. * Send a power management request to the device dev.
  205. *
  206. * Returns 0 on success or negative error code on failure.
  207. */
  208. static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
  209. {
  210. struct i2o_device *i2o_dev = dev->i2o_dev;
  211. struct i2o_controller *c = i2o_dev->iop;
  212. struct i2o_message *msg;
  213. int rc;
  214. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  215. if (IS_ERR(msg))
  216. return PTR_ERR(msg);
  217. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  218. msg->u.head[1] =
  219. cpu_to_le32(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->
  220. lct_data.tid);
  221. msg->body[0] = cpu_to_le32(op << 24);
  222. osm_debug("Power...\n");
  223. rc = i2o_msg_post_wait(c, msg, 60);
  224. if (!rc)
  225. dev->power = op;
  226. return rc;
  227. };
  228. /**
  229. * i2o_block_request_alloc - Allocate an I2O block request struct
  230. *
  231. * Allocates an I2O block request struct and initialize the list.
  232. *
  233. * Returns a i2o_block_request pointer on success or negative error code
  234. * on failure.
  235. */
  236. static inline struct i2o_block_request *i2o_block_request_alloc(void)
  237. {
  238. struct i2o_block_request *ireq;
  239. ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
  240. if (!ireq)
  241. return ERR_PTR(-ENOMEM);
  242. INIT_LIST_HEAD(&ireq->queue);
  243. sg_init_table(ireq->sg_table, I2O_MAX_PHYS_SEGMENTS);
  244. return ireq;
  245. };
  246. /**
  247. * i2o_block_request_free - Frees a I2O block request
  248. * @ireq: I2O block request which should be freed
  249. *
  250. * Frees the allocated memory (give it back to the request mempool).
  251. */
  252. static inline void i2o_block_request_free(struct i2o_block_request *ireq)
  253. {
  254. mempool_free(ireq, i2o_blk_req_pool.pool);
  255. };
  256. /**
  257. * i2o_block_sglist_alloc - Allocate the SG list and map it
  258. * @c: I2O controller to which the request belongs
  259. * @ireq: I2O block request
  260. * @mptr: message body pointer
  261. *
  262. * Builds the SG list and map it to be accessable by the controller.
  263. *
  264. * Returns 0 on failure or 1 on success.
  265. */
  266. static inline int i2o_block_sglist_alloc(struct i2o_controller *c,
  267. struct i2o_block_request *ireq,
  268. u32 ** mptr)
  269. {
  270. int nents;
  271. enum dma_data_direction direction;
  272. ireq->dev = &c->pdev->dev;
  273. nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
  274. if (rq_data_dir(ireq->req) == READ)
  275. direction = PCI_DMA_FROMDEVICE;
  276. else
  277. direction = PCI_DMA_TODEVICE;
  278. ireq->sg_nents = nents;
  279. return i2o_dma_map_sg(c, ireq->sg_table, nents, direction, mptr);
  280. };
  281. /**
  282. * i2o_block_sglist_free - Frees the SG list
  283. * @ireq: I2O block request from which the SG should be freed
  284. *
  285. * Frees the SG list from the I2O block request.
  286. */
  287. static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
  288. {
  289. enum dma_data_direction direction;
  290. if (rq_data_dir(ireq->req) == READ)
  291. direction = PCI_DMA_FROMDEVICE;
  292. else
  293. direction = PCI_DMA_TODEVICE;
  294. dma_unmap_sg(ireq->dev, ireq->sg_table, ireq->sg_nents, direction);
  295. };
  296. /**
  297. * i2o_block_prep_req_fn - Allocates I2O block device specific struct
  298. * @q: request queue for the request
  299. * @req: the request to prepare
  300. *
  301. * Allocate the necessary i2o_block_request struct and connect it to
  302. * the request. This is needed that we not lose the SG list later on.
  303. *
  304. * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
  305. */
  306. static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
  307. {
  308. struct i2o_block_device *i2o_blk_dev = q->queuedata;
  309. struct i2o_block_request *ireq;
  310. if (unlikely(!i2o_blk_dev)) {
  311. osm_err("block device already removed\n");
  312. return BLKPREP_KILL;
  313. }
  314. /* connect the i2o_block_request to the request */
  315. if (!req->special) {
  316. ireq = i2o_block_request_alloc();
  317. if (IS_ERR(ireq)) {
  318. osm_debug("unable to allocate i2o_block_request!\n");
  319. return BLKPREP_DEFER;
  320. }
  321. ireq->i2o_blk_dev = i2o_blk_dev;
  322. req->special = ireq;
  323. ireq->req = req;
  324. }
  325. /* do not come back here */
  326. req->cmd_flags |= REQ_DONTPREP;
  327. return BLKPREP_OK;
  328. };
  329. /**
  330. * i2o_block_delayed_request_fn - delayed request queue function
  331. * @work: the delayed request with the queue to start
  332. *
  333. * If the request queue is stopped for a disk, and there is no open
  334. * request, a new event is created, which calls this function to start
  335. * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
  336. * be started again.
  337. */
  338. static void i2o_block_delayed_request_fn(struct work_struct *work)
  339. {
  340. struct i2o_block_delayed_request *dreq =
  341. container_of(work, struct i2o_block_delayed_request,
  342. work.work);
  343. struct request_queue *q = dreq->queue;
  344. unsigned long flags;
  345. spin_lock_irqsave(q->queue_lock, flags);
  346. blk_start_queue(q);
  347. spin_unlock_irqrestore(q->queue_lock, flags);
  348. kfree(dreq);
  349. };
  350. /**
  351. * i2o_block_end_request - Post-processing of completed commands
  352. * @req: request which should be completed
  353. * @error: 0 for success, < 0 for error
  354. * @nr_bytes: number of bytes to complete
  355. *
  356. * Mark the request as complete. The lock must not be held when entering.
  357. *
  358. */
  359. static void i2o_block_end_request(struct request *req, int error,
  360. int nr_bytes)
  361. {
  362. struct i2o_block_request *ireq = req->special;
  363. struct i2o_block_device *dev = ireq->i2o_blk_dev;
  364. struct request_queue *q = req->q;
  365. unsigned long flags;
  366. if (blk_end_request(req, error, nr_bytes)) {
  367. int leftover = (req->hard_nr_sectors << KERNEL_SECTOR_SHIFT);
  368. if (blk_pc_request(req))
  369. leftover = req->data_len;
  370. if (error)
  371. blk_end_request(req, -EIO, leftover);
  372. }
  373. spin_lock_irqsave(q->queue_lock, flags);
  374. if (likely(dev)) {
  375. dev->open_queue_depth--;
  376. list_del(&ireq->queue);
  377. }
  378. blk_start_queue(q);
  379. spin_unlock_irqrestore(q->queue_lock, flags);
  380. i2o_block_sglist_free(ireq);
  381. i2o_block_request_free(ireq);
  382. };
  383. /**
  384. * i2o_block_reply - Block OSM reply handler.
  385. * @c: I2O controller from which the message arrives
  386. * @m: message id of reply
  387. * @msg: the actual I2O message reply
  388. *
  389. * This function gets all the message replies.
  390. *
  391. */
  392. static int i2o_block_reply(struct i2o_controller *c, u32 m,
  393. struct i2o_message *msg)
  394. {
  395. struct request *req;
  396. int error = 0;
  397. req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  398. if (unlikely(!req)) {
  399. osm_err("NULL reply received!\n");
  400. return -1;
  401. }
  402. /*
  403. * Lets see what is cooking. We stuffed the
  404. * request in the context.
  405. */
  406. if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
  407. u32 status = le32_to_cpu(msg->body[0]);
  408. /*
  409. * Device not ready means two things. One is that the
  410. * the thing went offline (but not a removal media)
  411. *
  412. * The second is that you have a SuperTrak 100 and the
  413. * firmware got constipated. Unlike standard i2o card
  414. * setups the supertrak returns an error rather than
  415. * blocking for the timeout in these cases.
  416. *
  417. * Don't stick a supertrak100 into cache aggressive modes
  418. */
  419. osm_err("TID %03x error status: 0x%02x, detailed status: "
  420. "0x%04x\n", (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
  421. status >> 24, status & 0xffff);
  422. req->errors++;
  423. error = -EIO;
  424. }
  425. i2o_block_end_request(req, error, le32_to_cpu(msg->body[1]));
  426. return 1;
  427. };
  428. static void i2o_block_event(struct work_struct *work)
  429. {
  430. struct i2o_event *evt = container_of(work, struct i2o_event, work);
  431. osm_debug("event received\n");
  432. kfree(evt);
  433. };
  434. /*
  435. * SCSI-CAM for ioctl geometry mapping
  436. * Duplicated with SCSI - this should be moved into somewhere common
  437. * perhaps genhd ?
  438. *
  439. * LBA -> CHS mapping table taken from:
  440. *
  441. * "Incorporating the I2O Architecture into BIOS for Intel Architecture
  442. * Platforms"
  443. *
  444. * This is an I2O document that is only available to I2O members,
  445. * not developers.
  446. *
  447. * From my understanding, this is how all the I2O cards do this
  448. *
  449. * Disk Size | Sectors | Heads | Cylinders
  450. * ---------------+---------+-------+-------------------
  451. * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
  452. * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
  453. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  454. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  455. *
  456. */
  457. #define BLOCK_SIZE_528M 1081344
  458. #define BLOCK_SIZE_1G 2097152
  459. #define BLOCK_SIZE_21G 4403200
  460. #define BLOCK_SIZE_42G 8806400
  461. #define BLOCK_SIZE_84G 17612800
  462. static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
  463. unsigned char *hds, unsigned char *secs)
  464. {
  465. unsigned long heads, sectors, cylinders;
  466. sectors = 63L; /* Maximize sectors per track */
  467. if (capacity <= BLOCK_SIZE_528M)
  468. heads = 16;
  469. else if (capacity <= BLOCK_SIZE_1G)
  470. heads = 32;
  471. else if (capacity <= BLOCK_SIZE_21G)
  472. heads = 64;
  473. else if (capacity <= BLOCK_SIZE_42G)
  474. heads = 128;
  475. else
  476. heads = 255;
  477. cylinders = (unsigned long)capacity / (heads * sectors);
  478. *cyls = (unsigned short)cylinders; /* Stuff return values */
  479. *secs = (unsigned char)sectors;
  480. *hds = (unsigned char)heads;
  481. }
  482. /**
  483. * i2o_block_open - Open the block device
  484. * @bdev: block device being opened
  485. * @mode: file open mode
  486. *
  487. * Power up the device, mount and lock the media. This function is called,
  488. * if the block device is opened for access.
  489. *
  490. * Returns 0 on success or negative error code on failure.
  491. */
  492. static int i2o_block_open(struct block_device *bdev, fmode_t mode)
  493. {
  494. struct i2o_block_device *dev = bdev->bd_disk->private_data;
  495. if (!dev->i2o_dev)
  496. return -ENODEV;
  497. if (dev->power > 0x1f)
  498. i2o_block_device_power(dev, 0x02);
  499. i2o_block_device_mount(dev->i2o_dev, -1);
  500. i2o_block_device_lock(dev->i2o_dev, -1);
  501. osm_debug("Ready.\n");
  502. return 0;
  503. };
  504. /**
  505. * i2o_block_release - Release the I2O block device
  506. * @disk: gendisk device being released
  507. * @mode: file open mode
  508. *
  509. * Unlock and unmount the media, and power down the device. Gets called if
  510. * the block device is closed.
  511. *
  512. * Returns 0 on success or negative error code on failure.
  513. */
  514. static int i2o_block_release(struct gendisk *disk, fmode_t mode)
  515. {
  516. struct i2o_block_device *dev = disk->private_data;
  517. u8 operation;
  518. /*
  519. * This is to deail with the case of an application
  520. * opening a device and then the device dissapears while
  521. * it's in use, and then the application tries to release
  522. * it. ex: Unmounting a deleted RAID volume at reboot.
  523. * If we send messages, it will just cause FAILs since
  524. * the TID no longer exists.
  525. */
  526. if (!dev->i2o_dev)
  527. return 0;
  528. i2o_block_device_flush(dev->i2o_dev);
  529. i2o_block_device_unlock(dev->i2o_dev, -1);
  530. if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
  531. operation = 0x21;
  532. else
  533. operation = 0x24;
  534. i2o_block_device_power(dev, operation);
  535. return 0;
  536. }
  537. static int i2o_block_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  538. {
  539. i2o_block_biosparam(get_capacity(bdev->bd_disk),
  540. &geo->cylinders, &geo->heads, &geo->sectors);
  541. return 0;
  542. }
  543. /**
  544. * i2o_block_ioctl - Issue device specific ioctl calls.
  545. * @bdev: block device being opened
  546. * @mode: file open mode
  547. * @cmd: ioctl command
  548. * @arg: arg
  549. *
  550. * Handles ioctl request for the block device.
  551. *
  552. * Return 0 on success or negative error on failure.
  553. */
  554. static int i2o_block_ioctl(struct block_device *bdev, fmode_t mode,
  555. unsigned int cmd, unsigned long arg)
  556. {
  557. struct gendisk *disk = bdev->bd_disk;
  558. struct i2o_block_device *dev = disk->private_data;
  559. /* Anyone capable of this syscall can do *real bad* things */
  560. if (!capable(CAP_SYS_ADMIN))
  561. return -EPERM;
  562. switch (cmd) {
  563. case BLKI2OGRSTRAT:
  564. return put_user(dev->rcache, (int __user *)arg);
  565. case BLKI2OGWSTRAT:
  566. return put_user(dev->wcache, (int __user *)arg);
  567. case BLKI2OSRSTRAT:
  568. if (arg < 0 || arg > CACHE_SMARTFETCH)
  569. return -EINVAL;
  570. dev->rcache = arg;
  571. break;
  572. case BLKI2OSWSTRAT:
  573. if (arg != 0
  574. && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
  575. return -EINVAL;
  576. dev->wcache = arg;
  577. break;
  578. }
  579. return -ENOTTY;
  580. };
  581. /**
  582. * i2o_block_media_changed - Have we seen a media change?
  583. * @disk: gendisk which should be verified
  584. *
  585. * Verifies if the media has changed.
  586. *
  587. * Returns 1 if the media was changed or 0 otherwise.
  588. */
  589. static int i2o_block_media_changed(struct gendisk *disk)
  590. {
  591. struct i2o_block_device *p = disk->private_data;
  592. if (p->media_change_flag) {
  593. p->media_change_flag = 0;
  594. return 1;
  595. }
  596. return 0;
  597. }
  598. /**
  599. * i2o_block_transfer - Transfer a request to/from the I2O controller
  600. * @req: the request which should be transfered
  601. *
  602. * This function converts the request into a I2O message. The necessary
  603. * DMA buffers are allocated and after everything is setup post the message
  604. * to the I2O controller. No cleanup is done by this function. It is done
  605. * on the interrupt side when the reply arrives.
  606. *
  607. * Return 0 on success or negative error code on failure.
  608. */
  609. static int i2o_block_transfer(struct request *req)
  610. {
  611. struct i2o_block_device *dev = req->rq_disk->private_data;
  612. struct i2o_controller *c;
  613. u32 tid = dev->i2o_dev->lct_data.tid;
  614. struct i2o_message *msg;
  615. u32 *mptr;
  616. struct i2o_block_request *ireq = req->special;
  617. u32 tcntxt;
  618. u32 sgl_offset = SGL_OFFSET_8;
  619. u32 ctl_flags = 0x00000000;
  620. int rc;
  621. u32 cmd;
  622. if (unlikely(!dev->i2o_dev)) {
  623. osm_err("transfer to removed drive\n");
  624. rc = -ENODEV;
  625. goto exit;
  626. }
  627. c = dev->i2o_dev->iop;
  628. msg = i2o_msg_get(c);
  629. if (IS_ERR(msg)) {
  630. rc = PTR_ERR(msg);
  631. goto exit;
  632. }
  633. tcntxt = i2o_cntxt_list_add(c, req);
  634. if (!tcntxt) {
  635. rc = -ENOMEM;
  636. goto nop_msg;
  637. }
  638. msg->u.s.icntxt = cpu_to_le32(i2o_block_driver.context);
  639. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  640. mptr = &msg->body[0];
  641. if (rq_data_dir(req) == READ) {
  642. cmd = I2O_CMD_BLOCK_READ << 24;
  643. switch (dev->rcache) {
  644. case CACHE_PREFETCH:
  645. ctl_flags = 0x201F0008;
  646. break;
  647. case CACHE_SMARTFETCH:
  648. if (req->nr_sectors > 16)
  649. ctl_flags = 0x201F0008;
  650. else
  651. ctl_flags = 0x001F0000;
  652. break;
  653. default:
  654. break;
  655. }
  656. } else {
  657. cmd = I2O_CMD_BLOCK_WRITE << 24;
  658. switch (dev->wcache) {
  659. case CACHE_WRITETHROUGH:
  660. ctl_flags = 0x001F0008;
  661. break;
  662. case CACHE_WRITEBACK:
  663. ctl_flags = 0x001F0010;
  664. break;
  665. case CACHE_SMARTBACK:
  666. if (req->nr_sectors > 16)
  667. ctl_flags = 0x001F0004;
  668. else
  669. ctl_flags = 0x001F0010;
  670. break;
  671. case CACHE_SMARTTHROUGH:
  672. if (req->nr_sectors > 16)
  673. ctl_flags = 0x001F0004;
  674. else
  675. ctl_flags = 0x001F0010;
  676. default:
  677. break;
  678. }
  679. }
  680. #ifdef CONFIG_I2O_EXT_ADAPTEC
  681. if (c->adaptec) {
  682. u8 cmd[10];
  683. u32 scsi_flags;
  684. u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;
  685. memset(cmd, 0, 10);
  686. sgl_offset = SGL_OFFSET_12;
  687. msg->u.head[1] =
  688. cpu_to_le32(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid);
  689. *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
  690. *mptr++ = cpu_to_le32(tid);
  691. /*
  692. * ENABLE_DISCONNECT
  693. * SIMPLE_TAG
  694. * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
  695. */
  696. if (rq_data_dir(req) == READ) {
  697. cmd[0] = READ_10;
  698. scsi_flags = 0x60a0000a;
  699. } else {
  700. cmd[0] = WRITE_10;
  701. scsi_flags = 0xa0a0000a;
  702. }
  703. *mptr++ = cpu_to_le32(scsi_flags);
  704. *((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
  705. *((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);
  706. memcpy(mptr, cmd, 10);
  707. mptr += 4;
  708. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  709. } else
  710. #endif
  711. {
  712. msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
  713. *mptr++ = cpu_to_le32(ctl_flags);
  714. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  715. *mptr++ =
  716. cpu_to_le32((u32) (req->sector << KERNEL_SECTOR_SHIFT));
  717. *mptr++ =
  718. cpu_to_le32(req->sector >> (32 - KERNEL_SECTOR_SHIFT));
  719. }
  720. if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
  721. rc = -ENOMEM;
  722. goto context_remove;
  723. }
  724. msg->u.head[0] =
  725. cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
  726. list_add_tail(&ireq->queue, &dev->open_queue);
  727. dev->open_queue_depth++;
  728. i2o_msg_post(c, msg);
  729. return 0;
  730. context_remove:
  731. i2o_cntxt_list_remove(c, req);
  732. nop_msg:
  733. i2o_msg_nop(c, msg);
  734. exit:
  735. return rc;
  736. };
  737. /**
  738. * i2o_block_request_fn - request queue handling function
  739. * @q: request queue from which the request could be fetched
  740. *
  741. * Takes the next request from the queue, transfers it and if no error
  742. * occurs dequeue it from the queue. On arrival of the reply the message
  743. * will be processed further. If an error occurs requeue the request.
  744. */
  745. static void i2o_block_request_fn(struct request_queue *q)
  746. {
  747. struct request *req;
  748. while (!blk_queue_plugged(q)) {
  749. req = elv_next_request(q);
  750. if (!req)
  751. break;
  752. if (blk_fs_request(req)) {
  753. struct i2o_block_delayed_request *dreq;
  754. struct i2o_block_request *ireq = req->special;
  755. unsigned int queue_depth;
  756. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  757. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
  758. if (!i2o_block_transfer(req)) {
  759. blkdev_dequeue_request(req);
  760. continue;
  761. } else
  762. osm_info("transfer error\n");
  763. }
  764. if (queue_depth)
  765. break;
  766. /* stop the queue and retry later */
  767. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  768. if (!dreq)
  769. continue;
  770. dreq->queue = q;
  771. INIT_DELAYED_WORK(&dreq->work,
  772. i2o_block_delayed_request_fn);
  773. if (!queue_delayed_work(i2o_block_driver.event_queue,
  774. &dreq->work,
  775. I2O_BLOCK_RETRY_TIME))
  776. kfree(dreq);
  777. else {
  778. blk_stop_queue(q);
  779. break;
  780. }
  781. } else
  782. end_request(req, 0);
  783. }
  784. };
  785. /* I2O Block device operations definition */
  786. static struct block_device_operations i2o_block_fops = {
  787. .owner = THIS_MODULE,
  788. .open = i2o_block_open,
  789. .release = i2o_block_release,
  790. .locked_ioctl = i2o_block_ioctl,
  791. .getgeo = i2o_block_getgeo,
  792. .media_changed = i2o_block_media_changed
  793. };
  794. /**
  795. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  796. *
  797. * Allocate memory for the i2o_block_device struct, gendisk and request
  798. * queue and initialize them as far as no additional information is needed.
  799. *
  800. * Returns a pointer to the allocated I2O Block device on succes or a
  801. * negative error code on failure.
  802. */
  803. static struct i2o_block_device *i2o_block_device_alloc(void)
  804. {
  805. struct i2o_block_device *dev;
  806. struct gendisk *gd;
  807. struct request_queue *queue;
  808. int rc;
  809. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  810. if (!dev) {
  811. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  812. rc = -ENOMEM;
  813. goto exit;
  814. }
  815. INIT_LIST_HEAD(&dev->open_queue);
  816. spin_lock_init(&dev->lock);
  817. dev->rcache = CACHE_PREFETCH;
  818. dev->wcache = CACHE_WRITEBACK;
  819. /* allocate a gendisk with 16 partitions */
  820. gd = alloc_disk(16);
  821. if (!gd) {
  822. osm_err("Insufficient memory to allocate gendisk.\n");
  823. rc = -ENOMEM;
  824. goto cleanup_dev;
  825. }
  826. /* initialize the request queue */
  827. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  828. if (!queue) {
  829. osm_err("Insufficient memory to allocate request queue.\n");
  830. rc = -ENOMEM;
  831. goto cleanup_queue;
  832. }
  833. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  834. gd->major = I2O_MAJOR;
  835. gd->queue = queue;
  836. gd->fops = &i2o_block_fops;
  837. gd->private_data = dev;
  838. dev->gd = gd;
  839. return dev;
  840. cleanup_queue:
  841. put_disk(gd);
  842. cleanup_dev:
  843. kfree(dev);
  844. exit:
  845. return ERR_PTR(rc);
  846. };
  847. /**
  848. * i2o_block_probe - verify if dev is a I2O Block device and install it
  849. * @dev: device to verify if it is a I2O Block device
  850. *
  851. * We only verify if the user_tid of the device is 0xfff and then install
  852. * the device. Otherwise it is used by some other device (e. g. RAID).
  853. *
  854. * Returns 0 on success or negative error code on failure.
  855. */
  856. static int i2o_block_probe(struct device *dev)
  857. {
  858. struct i2o_device *i2o_dev = to_i2o_device(dev);
  859. struct i2o_controller *c = i2o_dev->iop;
  860. struct i2o_block_device *i2o_blk_dev;
  861. struct gendisk *gd;
  862. struct request_queue *queue;
  863. static int unit = 0;
  864. int rc;
  865. u64 size;
  866. u32 blocksize;
  867. u16 body_size = 4;
  868. u16 power;
  869. unsigned short max_sectors;
  870. #ifdef CONFIG_I2O_EXT_ADAPTEC
  871. if (c->adaptec)
  872. body_size = 8;
  873. #endif
  874. if (c->limit_sectors)
  875. max_sectors = I2O_MAX_SECTORS_LIMITED;
  876. else
  877. max_sectors = I2O_MAX_SECTORS;
  878. /* skip devices which are used by IOP */
  879. if (i2o_dev->lct_data.user_tid != 0xfff) {
  880. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  881. return -ENODEV;
  882. }
  883. if (i2o_device_claim(i2o_dev)) {
  884. osm_warn("Unable to claim device. Installation aborted\n");
  885. rc = -EFAULT;
  886. goto exit;
  887. }
  888. i2o_blk_dev = i2o_block_device_alloc();
  889. if (IS_ERR(i2o_blk_dev)) {
  890. osm_err("could not alloc a new I2O block device");
  891. rc = PTR_ERR(i2o_blk_dev);
  892. goto claim_release;
  893. }
  894. i2o_blk_dev->i2o_dev = i2o_dev;
  895. dev_set_drvdata(dev, i2o_blk_dev);
  896. /* setup gendisk */
  897. gd = i2o_blk_dev->gd;
  898. gd->first_minor = unit << 4;
  899. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  900. gd->driverfs_dev = &i2o_dev->device;
  901. /* setup request queue */
  902. queue = gd->queue;
  903. queue->queuedata = i2o_blk_dev;
  904. blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
  905. blk_queue_max_sectors(queue, max_sectors);
  906. blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
  907. osm_debug("max sectors = %d\n", queue->max_sectors);
  908. osm_debug("phys segments = %d\n", queue->max_phys_segments);
  909. osm_debug("max hw segments = %d\n", queue->max_hw_segments);
  910. /*
  911. * Ask for the current media data. If that isn't supported
  912. * then we ask for the device capacity data
  913. */
  914. if (!i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
  915. !i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
  916. blk_queue_hardsect_size(queue, le32_to_cpu(blocksize));
  917. } else
  918. osm_warn("unable to get blocksize of %s\n", gd->disk_name);
  919. if (!i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
  920. !i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
  921. set_capacity(gd, le64_to_cpu(size) >> KERNEL_SECTOR_SHIFT);
  922. } else
  923. osm_warn("could not get size of %s\n", gd->disk_name);
  924. if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &power, 2))
  925. i2o_blk_dev->power = power;
  926. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  927. add_disk(gd);
  928. unit++;
  929. osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  930. i2o_blk_dev->gd->disk_name);
  931. return 0;
  932. claim_release:
  933. i2o_device_claim_release(i2o_dev);
  934. exit:
  935. return rc;
  936. };
  937. /* Block OSM driver struct */
  938. static struct i2o_driver i2o_block_driver = {
  939. .name = OSM_NAME,
  940. .event = i2o_block_event,
  941. .reply = i2o_block_reply,
  942. .classes = i2o_block_class_id,
  943. .driver = {
  944. .probe = i2o_block_probe,
  945. .remove = i2o_block_remove,
  946. },
  947. };
  948. /**
  949. * i2o_block_init - Block OSM initialization function
  950. *
  951. * Allocate the slab and mempool for request structs, registers i2o_block
  952. * block device and finally register the Block OSM in the I2O core.
  953. *
  954. * Returns 0 on success or negative error code on failure.
  955. */
  956. static int __init i2o_block_init(void)
  957. {
  958. int rc;
  959. int size;
  960. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  961. /* Allocate request mempool and slab */
  962. size = sizeof(struct i2o_block_request);
  963. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  964. SLAB_HWCACHE_ALIGN, NULL);
  965. if (!i2o_blk_req_pool.slab) {
  966. osm_err("can't init request slab\n");
  967. rc = -ENOMEM;
  968. goto exit;
  969. }
  970. i2o_blk_req_pool.pool =
  971. mempool_create_slab_pool(I2O_BLOCK_REQ_MEMPOOL_SIZE,
  972. i2o_blk_req_pool.slab);
  973. if (!i2o_blk_req_pool.pool) {
  974. osm_err("can't init request mempool\n");
  975. rc = -ENOMEM;
  976. goto free_slab;
  977. }
  978. /* Register the block device interfaces */
  979. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  980. if (rc) {
  981. osm_err("unable to register block device\n");
  982. goto free_mempool;
  983. }
  984. #ifdef MODULE
  985. osm_info("registered device at major %d\n", I2O_MAJOR);
  986. #endif
  987. /* Register Block OSM into I2O core */
  988. rc = i2o_driver_register(&i2o_block_driver);
  989. if (rc) {
  990. osm_err("Could not register Block driver\n");
  991. goto unregister_blkdev;
  992. }
  993. return 0;
  994. unregister_blkdev:
  995. unregister_blkdev(I2O_MAJOR, "i2o_block");
  996. free_mempool:
  997. mempool_destroy(i2o_blk_req_pool.pool);
  998. free_slab:
  999. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1000. exit:
  1001. return rc;
  1002. };
  1003. /**
  1004. * i2o_block_exit - Block OSM exit function
  1005. *
  1006. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  1007. * and frees the mempool and slab.
  1008. */
  1009. static void __exit i2o_block_exit(void)
  1010. {
  1011. /* Unregister I2O Block OSM from I2O core */
  1012. i2o_driver_unregister(&i2o_block_driver);
  1013. /* Unregister block device */
  1014. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1015. /* Free request mempool and slab */
  1016. mempool_destroy(i2o_blk_req_pool.pool);
  1017. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1018. };
  1019. MODULE_AUTHOR("Red Hat");
  1020. MODULE_LICENSE("GPL");
  1021. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1022. MODULE_VERSION(OSM_VERSION);
  1023. module_init(i2o_block_init);
  1024. module_exit(i2o_block_exit);