i2o_block.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. return ireq;
  244. };
  245. /**
  246. * i2o_block_request_free - Frees a I2O block request
  247. * @ireq: I2O block request which should be freed
  248. *
  249. * Frees the allocated memory (give it back to the request mempool).
  250. */
  251. static inline void i2o_block_request_free(struct i2o_block_request *ireq)
  252. {
  253. mempool_free(ireq, i2o_blk_req_pool.pool);
  254. };
  255. /**
  256. * i2o_block_sglist_alloc - Allocate the SG list and map it
  257. * @c: I2O controller to which the request belongs
  258. * @ireq: I2O block request
  259. * @mptr: message body pointer
  260. *
  261. * Builds the SG list and map it to be accessable by the controller.
  262. *
  263. * Returns 0 on failure or 1 on success.
  264. */
  265. static inline int i2o_block_sglist_alloc(struct i2o_controller *c,
  266. struct i2o_block_request *ireq,
  267. u32 ** mptr)
  268. {
  269. int nents;
  270. enum dma_data_direction direction;
  271. ireq->dev = &c->pdev->dev;
  272. nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
  273. if (rq_data_dir(ireq->req) == READ)
  274. direction = PCI_DMA_FROMDEVICE;
  275. else
  276. direction = PCI_DMA_TODEVICE;
  277. ireq->sg_nents = nents;
  278. return i2o_dma_map_sg(c, ireq->sg_table, nents, direction, mptr);
  279. };
  280. /**
  281. * i2o_block_sglist_free - Frees the SG list
  282. * @ireq: I2O block request from which the SG should be freed
  283. *
  284. * Frees the SG list from the I2O block request.
  285. */
  286. static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
  287. {
  288. enum dma_data_direction direction;
  289. if (rq_data_dir(ireq->req) == READ)
  290. direction = PCI_DMA_FROMDEVICE;
  291. else
  292. direction = PCI_DMA_TODEVICE;
  293. dma_unmap_sg(ireq->dev, ireq->sg_table, ireq->sg_nents, direction);
  294. };
  295. /**
  296. * i2o_block_prep_req_fn - Allocates I2O block device specific struct
  297. * @q: request queue for the request
  298. * @req: the request to prepare
  299. *
  300. * Allocate the necessary i2o_block_request struct and connect it to
  301. * the request. This is needed that we not loose the SG list later on.
  302. *
  303. * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
  304. */
  305. static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
  306. {
  307. struct i2o_block_device *i2o_blk_dev = q->queuedata;
  308. struct i2o_block_request *ireq;
  309. if (unlikely(!i2o_blk_dev)) {
  310. osm_err("block device already removed\n");
  311. return BLKPREP_KILL;
  312. }
  313. /* connect the i2o_block_request to the request */
  314. if (!req->special) {
  315. ireq = i2o_block_request_alloc();
  316. if (unlikely(IS_ERR(ireq))) {
  317. osm_debug("unable to allocate i2o_block_request!\n");
  318. return BLKPREP_DEFER;
  319. }
  320. ireq->i2o_blk_dev = i2o_blk_dev;
  321. req->special = ireq;
  322. ireq->req = req;
  323. }
  324. /* do not come back here */
  325. req->cmd_flags |= REQ_DONTPREP;
  326. return BLKPREP_OK;
  327. };
  328. /**
  329. * i2o_block_delayed_request_fn - delayed request queue function
  330. * @work: the delayed request with the queue to start
  331. *
  332. * If the request queue is stopped for a disk, and there is no open
  333. * request, a new event is created, which calls this function to start
  334. * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
  335. * be started again.
  336. */
  337. static void i2o_block_delayed_request_fn(struct work_struct *work)
  338. {
  339. struct i2o_block_delayed_request *dreq =
  340. container_of(work, struct i2o_block_delayed_request,
  341. work.work);
  342. struct request_queue *q = dreq->queue;
  343. unsigned long flags;
  344. spin_lock_irqsave(q->queue_lock, flags);
  345. blk_start_queue(q);
  346. spin_unlock_irqrestore(q->queue_lock, flags);
  347. kfree(dreq);
  348. };
  349. /**
  350. * i2o_block_end_request - Post-processing of completed commands
  351. * @req: request which should be completed
  352. * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
  353. * @nr_bytes: number of bytes to complete
  354. *
  355. * Mark the request as complete. The lock must not be held when entering.
  356. *
  357. */
  358. static void i2o_block_end_request(struct request *req, int uptodate,
  359. int nr_bytes)
  360. {
  361. struct i2o_block_request *ireq = req->special;
  362. struct i2o_block_device *dev = ireq->i2o_blk_dev;
  363. struct request_queue *q = req->q;
  364. unsigned long flags;
  365. if (end_that_request_chunk(req, uptodate, nr_bytes)) {
  366. int leftover = (req->hard_nr_sectors << KERNEL_SECTOR_SHIFT);
  367. if (blk_pc_request(req))
  368. leftover = req->data_len;
  369. if (end_io_error(uptodate))
  370. end_that_request_chunk(req, 0, leftover);
  371. }
  372. add_disk_randomness(req->rq_disk);
  373. spin_lock_irqsave(q->queue_lock, flags);
  374. end_that_request_last(req, uptodate);
  375. if (likely(dev)) {
  376. dev->open_queue_depth--;
  377. list_del(&ireq->queue);
  378. }
  379. blk_start_queue(q);
  380. spin_unlock_irqrestore(q->queue_lock, flags);
  381. i2o_block_sglist_free(ireq);
  382. i2o_block_request_free(ireq);
  383. };
  384. /**
  385. * i2o_block_reply - Block OSM reply handler.
  386. * @c: I2O controller from which the message arrives
  387. * @m: message id of reply
  388. * @msg: the actual I2O message reply
  389. *
  390. * This function gets all the message replies.
  391. *
  392. */
  393. static int i2o_block_reply(struct i2o_controller *c, u32 m,
  394. struct i2o_message *msg)
  395. {
  396. struct request *req;
  397. int uptodate = 1;
  398. req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  399. if (unlikely(!req)) {
  400. osm_err("NULL reply received!\n");
  401. return -1;
  402. }
  403. /*
  404. * Lets see what is cooking. We stuffed the
  405. * request in the context.
  406. */
  407. if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
  408. u32 status = le32_to_cpu(msg->body[0]);
  409. /*
  410. * Device not ready means two things. One is that the
  411. * the thing went offline (but not a removal media)
  412. *
  413. * The second is that you have a SuperTrak 100 and the
  414. * firmware got constipated. Unlike standard i2o card
  415. * setups the supertrak returns an error rather than
  416. * blocking for the timeout in these cases.
  417. *
  418. * Don't stick a supertrak100 into cache aggressive modes
  419. */
  420. osm_err("TID %03x error status: 0x%02x, detailed status: "
  421. "0x%04x\n", (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
  422. status >> 24, status & 0xffff);
  423. req->errors++;
  424. uptodate = 0;
  425. }
  426. i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
  427. return 1;
  428. };
  429. static void i2o_block_event(struct work_struct *work)
  430. {
  431. struct i2o_event *evt = container_of(work, struct i2o_event, work);
  432. osm_debug("event received\n");
  433. kfree(evt);
  434. };
  435. /*
  436. * SCSI-CAM for ioctl geometry mapping
  437. * Duplicated with SCSI - this should be moved into somewhere common
  438. * perhaps genhd ?
  439. *
  440. * LBA -> CHS mapping table taken from:
  441. *
  442. * "Incorporating the I2O Architecture into BIOS for Intel Architecture
  443. * Platforms"
  444. *
  445. * This is an I2O document that is only available to I2O members,
  446. * not developers.
  447. *
  448. * From my understanding, this is how all the I2O cards do this
  449. *
  450. * Disk Size | Sectors | Heads | Cylinders
  451. * ---------------+---------+-------+-------------------
  452. * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
  453. * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
  454. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  455. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  456. *
  457. */
  458. #define BLOCK_SIZE_528M 1081344
  459. #define BLOCK_SIZE_1G 2097152
  460. #define BLOCK_SIZE_21G 4403200
  461. #define BLOCK_SIZE_42G 8806400
  462. #define BLOCK_SIZE_84G 17612800
  463. static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
  464. unsigned char *hds, unsigned char *secs)
  465. {
  466. unsigned long heads, sectors, cylinders;
  467. sectors = 63L; /* Maximize sectors per track */
  468. if (capacity <= BLOCK_SIZE_528M)
  469. heads = 16;
  470. else if (capacity <= BLOCK_SIZE_1G)
  471. heads = 32;
  472. else if (capacity <= BLOCK_SIZE_21G)
  473. heads = 64;
  474. else if (capacity <= BLOCK_SIZE_42G)
  475. heads = 128;
  476. else
  477. heads = 255;
  478. cylinders = (unsigned long)capacity / (heads * sectors);
  479. *cyls = (unsigned short)cylinders; /* Stuff return values */
  480. *secs = (unsigned char)sectors;
  481. *hds = (unsigned char)heads;
  482. }
  483. /**
  484. * i2o_block_open - Open the block device
  485. * @inode: inode for block device being opened
  486. * @file: file to open
  487. *
  488. * Power up the device, mount and lock the media. This function is called,
  489. * if the block device is opened for access.
  490. *
  491. * Returns 0 on success or negative error code on failure.
  492. */
  493. static int i2o_block_open(struct inode *inode, struct file *file)
  494. {
  495. struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
  496. if (!dev->i2o_dev)
  497. return -ENODEV;
  498. if (dev->power > 0x1f)
  499. i2o_block_device_power(dev, 0x02);
  500. i2o_block_device_mount(dev->i2o_dev, -1);
  501. i2o_block_device_lock(dev->i2o_dev, -1);
  502. osm_debug("Ready.\n");
  503. return 0;
  504. };
  505. /**
  506. * i2o_block_release - Release the I2O block device
  507. * @inode: inode for block device being released
  508. * @file: file to close
  509. *
  510. * Unlock and unmount the media, and power down the device. Gets called if
  511. * the block device is closed.
  512. *
  513. * Returns 0 on success or negative error code on failure.
  514. */
  515. static int i2o_block_release(struct inode *inode, struct file *file)
  516. {
  517. struct gendisk *disk = inode->i_bdev->bd_disk;
  518. struct i2o_block_device *dev = disk->private_data;
  519. u8 operation;
  520. /*
  521. * This is to deail with the case of an application
  522. * opening a device and then the device dissapears while
  523. * it's in use, and then the application tries to release
  524. * it. ex: Unmounting a deleted RAID volume at reboot.
  525. * If we send messages, it will just cause FAILs since
  526. * the TID no longer exists.
  527. */
  528. if (!dev->i2o_dev)
  529. return 0;
  530. i2o_block_device_flush(dev->i2o_dev);
  531. i2o_block_device_unlock(dev->i2o_dev, -1);
  532. if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
  533. operation = 0x21;
  534. else
  535. operation = 0x24;
  536. i2o_block_device_power(dev, operation);
  537. return 0;
  538. }
  539. static int i2o_block_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  540. {
  541. i2o_block_biosparam(get_capacity(bdev->bd_disk),
  542. &geo->cylinders, &geo->heads, &geo->sectors);
  543. return 0;
  544. }
  545. /**
  546. * i2o_block_ioctl - Issue device specific ioctl calls.
  547. * @inode: inode for block device ioctl
  548. * @file: file for ioctl
  549. * @cmd: ioctl command
  550. * @arg: arg
  551. *
  552. * Handles ioctl request for the block device.
  553. *
  554. * Return 0 on success or negative error on failure.
  555. */
  556. static int i2o_block_ioctl(struct inode *inode, struct file *file,
  557. unsigned int cmd, unsigned long arg)
  558. {
  559. struct gendisk *disk = inode->i_bdev->bd_disk;
  560. struct i2o_block_device *dev = disk->private_data;
  561. /* Anyone capable of this syscall can do *real bad* things */
  562. if (!capable(CAP_SYS_ADMIN))
  563. return -EPERM;
  564. switch (cmd) {
  565. case BLKI2OGRSTRAT:
  566. return put_user(dev->rcache, (int __user *)arg);
  567. case BLKI2OGWSTRAT:
  568. return put_user(dev->wcache, (int __user *)arg);
  569. case BLKI2OSRSTRAT:
  570. if (arg < 0 || arg > CACHE_SMARTFETCH)
  571. return -EINVAL;
  572. dev->rcache = arg;
  573. break;
  574. case BLKI2OSWSTRAT:
  575. if (arg != 0
  576. && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
  577. return -EINVAL;
  578. dev->wcache = arg;
  579. break;
  580. }
  581. return -ENOTTY;
  582. };
  583. /**
  584. * i2o_block_media_changed - Have we seen a media change?
  585. * @disk: gendisk which should be verified
  586. *
  587. * Verifies if the media has changed.
  588. *
  589. * Returns 1 if the media was changed or 0 otherwise.
  590. */
  591. static int i2o_block_media_changed(struct gendisk *disk)
  592. {
  593. struct i2o_block_device *p = disk->private_data;
  594. if (p->media_change_flag) {
  595. p->media_change_flag = 0;
  596. return 1;
  597. }
  598. return 0;
  599. }
  600. /**
  601. * i2o_block_transfer - Transfer a request to/from the I2O controller
  602. * @req: the request which should be transfered
  603. *
  604. * This function converts the request into a I2O message. The necessary
  605. * DMA buffers are allocated and after everything is setup post the message
  606. * to the I2O controller. No cleanup is done by this function. It is done
  607. * on the interrupt side when the reply arrives.
  608. *
  609. * Return 0 on success or negative error code on failure.
  610. */
  611. static int i2o_block_transfer(struct request *req)
  612. {
  613. struct i2o_block_device *dev = req->rq_disk->private_data;
  614. struct i2o_controller *c;
  615. u32 tid = dev->i2o_dev->lct_data.tid;
  616. struct i2o_message *msg;
  617. u32 *mptr;
  618. struct i2o_block_request *ireq = req->special;
  619. u32 tcntxt;
  620. u32 sgl_offset = SGL_OFFSET_8;
  621. u32 ctl_flags = 0x00000000;
  622. int rc;
  623. u32 cmd;
  624. if (unlikely(!dev->i2o_dev)) {
  625. osm_err("transfer to removed drive\n");
  626. rc = -ENODEV;
  627. goto exit;
  628. }
  629. c = dev->i2o_dev->iop;
  630. msg = i2o_msg_get(c);
  631. if (IS_ERR(msg)) {
  632. rc = PTR_ERR(msg);
  633. goto exit;
  634. }
  635. tcntxt = i2o_cntxt_list_add(c, req);
  636. if (!tcntxt) {
  637. rc = -ENOMEM;
  638. goto nop_msg;
  639. }
  640. msg->u.s.icntxt = cpu_to_le32(i2o_block_driver.context);
  641. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  642. mptr = &msg->body[0];
  643. if (rq_data_dir(req) == READ) {
  644. cmd = I2O_CMD_BLOCK_READ << 24;
  645. switch (dev->rcache) {
  646. case CACHE_PREFETCH:
  647. ctl_flags = 0x201F0008;
  648. break;
  649. case CACHE_SMARTFETCH:
  650. if (req->nr_sectors > 16)
  651. ctl_flags = 0x201F0008;
  652. else
  653. ctl_flags = 0x001F0000;
  654. break;
  655. default:
  656. break;
  657. }
  658. } else {
  659. cmd = I2O_CMD_BLOCK_WRITE << 24;
  660. switch (dev->wcache) {
  661. case CACHE_WRITETHROUGH:
  662. ctl_flags = 0x001F0008;
  663. break;
  664. case CACHE_WRITEBACK:
  665. ctl_flags = 0x001F0010;
  666. break;
  667. case CACHE_SMARTBACK:
  668. if (req->nr_sectors > 16)
  669. ctl_flags = 0x001F0004;
  670. else
  671. ctl_flags = 0x001F0010;
  672. break;
  673. case CACHE_SMARTTHROUGH:
  674. if (req->nr_sectors > 16)
  675. ctl_flags = 0x001F0004;
  676. else
  677. ctl_flags = 0x001F0010;
  678. default:
  679. break;
  680. }
  681. }
  682. #ifdef CONFIG_I2O_EXT_ADAPTEC
  683. if (c->adaptec) {
  684. u8 cmd[10];
  685. u32 scsi_flags;
  686. u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;
  687. memset(cmd, 0, 10);
  688. sgl_offset = SGL_OFFSET_12;
  689. msg->u.head[1] =
  690. cpu_to_le32(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid);
  691. *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
  692. *mptr++ = cpu_to_le32(tid);
  693. /*
  694. * ENABLE_DISCONNECT
  695. * SIMPLE_TAG
  696. * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
  697. */
  698. if (rq_data_dir(req) == READ) {
  699. cmd[0] = READ_10;
  700. scsi_flags = 0x60a0000a;
  701. } else {
  702. cmd[0] = WRITE_10;
  703. scsi_flags = 0xa0a0000a;
  704. }
  705. *mptr++ = cpu_to_le32(scsi_flags);
  706. *((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
  707. *((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);
  708. memcpy(mptr, cmd, 10);
  709. mptr += 4;
  710. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  711. } else
  712. #endif
  713. {
  714. msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
  715. *mptr++ = cpu_to_le32(ctl_flags);
  716. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  717. *mptr++ =
  718. cpu_to_le32((u32) (req->sector << KERNEL_SECTOR_SHIFT));
  719. *mptr++ =
  720. cpu_to_le32(req->sector >> (32 - KERNEL_SECTOR_SHIFT));
  721. }
  722. if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
  723. rc = -ENOMEM;
  724. goto context_remove;
  725. }
  726. msg->u.head[0] =
  727. cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
  728. list_add_tail(&ireq->queue, &dev->open_queue);
  729. dev->open_queue_depth++;
  730. i2o_msg_post(c, msg);
  731. return 0;
  732. context_remove:
  733. i2o_cntxt_list_remove(c, req);
  734. nop_msg:
  735. i2o_msg_nop(c, msg);
  736. exit:
  737. return rc;
  738. };
  739. /**
  740. * i2o_block_request_fn - request queue handling function
  741. * @q: request queue from which the request could be fetched
  742. *
  743. * Takes the next request from the queue, transfers it and if no error
  744. * occurs dequeue it from the queue. On arrival of the reply the message
  745. * will be processed further. If an error occurs requeue the request.
  746. */
  747. static void i2o_block_request_fn(struct request_queue *q)
  748. {
  749. struct request *req;
  750. while (!blk_queue_plugged(q)) {
  751. req = elv_next_request(q);
  752. if (!req)
  753. break;
  754. if (blk_fs_request(req)) {
  755. struct i2o_block_delayed_request *dreq;
  756. struct i2o_block_request *ireq = req->special;
  757. unsigned int queue_depth;
  758. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  759. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
  760. if (!i2o_block_transfer(req)) {
  761. blkdev_dequeue_request(req);
  762. continue;
  763. } else
  764. osm_info("transfer error\n");
  765. }
  766. if (queue_depth)
  767. break;
  768. /* stop the queue and retry later */
  769. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  770. if (!dreq)
  771. continue;
  772. dreq->queue = q;
  773. INIT_DELAYED_WORK(&dreq->work,
  774. i2o_block_delayed_request_fn);
  775. if (!queue_delayed_work(i2o_block_driver.event_queue,
  776. &dreq->work,
  777. I2O_BLOCK_RETRY_TIME))
  778. kfree(dreq);
  779. else {
  780. blk_stop_queue(q);
  781. break;
  782. }
  783. } else
  784. end_request(req, 0);
  785. }
  786. };
  787. /* I2O Block device operations definition */
  788. static struct block_device_operations i2o_block_fops = {
  789. .owner = THIS_MODULE,
  790. .open = i2o_block_open,
  791. .release = i2o_block_release,
  792. .ioctl = i2o_block_ioctl,
  793. .getgeo = i2o_block_getgeo,
  794. .media_changed = i2o_block_media_changed
  795. };
  796. /**
  797. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  798. *
  799. * Allocate memory for the i2o_block_device struct, gendisk and request
  800. * queue and initialize them as far as no additional information is needed.
  801. *
  802. * Returns a pointer to the allocated I2O Block device on succes or a
  803. * negative error code on failure.
  804. */
  805. static struct i2o_block_device *i2o_block_device_alloc(void)
  806. {
  807. struct i2o_block_device *dev;
  808. struct gendisk *gd;
  809. struct request_queue *queue;
  810. int rc;
  811. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  812. if (!dev) {
  813. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  814. rc = -ENOMEM;
  815. goto exit;
  816. }
  817. INIT_LIST_HEAD(&dev->open_queue);
  818. spin_lock_init(&dev->lock);
  819. dev->rcache = CACHE_PREFETCH;
  820. dev->wcache = CACHE_WRITEBACK;
  821. /* allocate a gendisk with 16 partitions */
  822. gd = alloc_disk(16);
  823. if (!gd) {
  824. osm_err("Insufficient memory to allocate gendisk.\n");
  825. rc = -ENOMEM;
  826. goto cleanup_dev;
  827. }
  828. /* initialize the request queue */
  829. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  830. if (!queue) {
  831. osm_err("Insufficient memory to allocate request queue.\n");
  832. rc = -ENOMEM;
  833. goto cleanup_queue;
  834. }
  835. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  836. gd->major = I2O_MAJOR;
  837. gd->queue = queue;
  838. gd->fops = &i2o_block_fops;
  839. gd->private_data = dev;
  840. dev->gd = gd;
  841. return dev;
  842. cleanup_queue:
  843. put_disk(gd);
  844. cleanup_dev:
  845. kfree(dev);
  846. exit:
  847. return ERR_PTR(rc);
  848. };
  849. /**
  850. * i2o_block_probe - verify if dev is a I2O Block device and install it
  851. * @dev: device to verify if it is a I2O Block device
  852. *
  853. * We only verify if the user_tid of the device is 0xfff and then install
  854. * the device. Otherwise it is used by some other device (e. g. RAID).
  855. *
  856. * Returns 0 on success or negative error code on failure.
  857. */
  858. static int i2o_block_probe(struct device *dev)
  859. {
  860. struct i2o_device *i2o_dev = to_i2o_device(dev);
  861. struct i2o_controller *c = i2o_dev->iop;
  862. struct i2o_block_device *i2o_blk_dev;
  863. struct gendisk *gd;
  864. struct request_queue *queue;
  865. static int unit = 0;
  866. int rc;
  867. u64 size;
  868. u32 blocksize;
  869. u16 body_size = 4;
  870. u16 power;
  871. unsigned short max_sectors;
  872. #ifdef CONFIG_I2O_EXT_ADAPTEC
  873. if (c->adaptec)
  874. body_size = 8;
  875. #endif
  876. if (c->limit_sectors)
  877. max_sectors = I2O_MAX_SECTORS_LIMITED;
  878. else
  879. max_sectors = I2O_MAX_SECTORS;
  880. /* skip devices which are used by IOP */
  881. if (i2o_dev->lct_data.user_tid != 0xfff) {
  882. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  883. return -ENODEV;
  884. }
  885. if (i2o_device_claim(i2o_dev)) {
  886. osm_warn("Unable to claim device. Installation aborted\n");
  887. rc = -EFAULT;
  888. goto exit;
  889. }
  890. i2o_blk_dev = i2o_block_device_alloc();
  891. if (IS_ERR(i2o_blk_dev)) {
  892. osm_err("could not alloc a new I2O block device");
  893. rc = PTR_ERR(i2o_blk_dev);
  894. goto claim_release;
  895. }
  896. i2o_blk_dev->i2o_dev = i2o_dev;
  897. dev_set_drvdata(dev, i2o_blk_dev);
  898. /* setup gendisk */
  899. gd = i2o_blk_dev->gd;
  900. gd->first_minor = unit << 4;
  901. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  902. gd->driverfs_dev = &i2o_dev->device;
  903. /* setup request queue */
  904. queue = gd->queue;
  905. queue->queuedata = i2o_blk_dev;
  906. blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
  907. blk_queue_max_sectors(queue, max_sectors);
  908. blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
  909. osm_debug("max sectors = %d\n", queue->max_phys_segments);
  910. osm_debug("phys segments = %d\n", queue->max_sectors);
  911. osm_debug("max hw segments = %d\n", queue->max_hw_segments);
  912. /*
  913. * Ask for the current media data. If that isn't supported
  914. * then we ask for the device capacity data
  915. */
  916. if (!i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
  917. !i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
  918. blk_queue_hardsect_size(queue, le32_to_cpu(blocksize));
  919. } else
  920. osm_warn("unable to get blocksize of %s\n", gd->disk_name);
  921. if (!i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
  922. !i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
  923. set_capacity(gd, le64_to_cpu(size) >> KERNEL_SECTOR_SHIFT);
  924. } else
  925. osm_warn("could not get size of %s\n", gd->disk_name);
  926. if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &power, 2))
  927. i2o_blk_dev->power = power;
  928. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  929. add_disk(gd);
  930. unit++;
  931. osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  932. i2o_blk_dev->gd->disk_name);
  933. return 0;
  934. claim_release:
  935. i2o_device_claim_release(i2o_dev);
  936. exit:
  937. return rc;
  938. };
  939. /* Block OSM driver struct */
  940. static struct i2o_driver i2o_block_driver = {
  941. .name = OSM_NAME,
  942. .event = i2o_block_event,
  943. .reply = i2o_block_reply,
  944. .classes = i2o_block_class_id,
  945. .driver = {
  946. .probe = i2o_block_probe,
  947. .remove = i2o_block_remove,
  948. },
  949. };
  950. /**
  951. * i2o_block_init - Block OSM initialization function
  952. *
  953. * Allocate the slab and mempool for request structs, registers i2o_block
  954. * block device and finally register the Block OSM in the I2O core.
  955. *
  956. * Returns 0 on success or negative error code on failure.
  957. */
  958. static int __init i2o_block_init(void)
  959. {
  960. int rc;
  961. int size;
  962. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  963. /* Allocate request mempool and slab */
  964. size = sizeof(struct i2o_block_request);
  965. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  966. SLAB_HWCACHE_ALIGN, NULL);
  967. if (!i2o_blk_req_pool.slab) {
  968. osm_err("can't init request slab\n");
  969. rc = -ENOMEM;
  970. goto exit;
  971. }
  972. i2o_blk_req_pool.pool =
  973. mempool_create_slab_pool(I2O_BLOCK_REQ_MEMPOOL_SIZE,
  974. i2o_blk_req_pool.slab);
  975. if (!i2o_blk_req_pool.pool) {
  976. osm_err("can't init request mempool\n");
  977. rc = -ENOMEM;
  978. goto free_slab;
  979. }
  980. /* Register the block device interfaces */
  981. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  982. if (rc) {
  983. osm_err("unable to register block device\n");
  984. goto free_mempool;
  985. }
  986. #ifdef MODULE
  987. osm_info("registered device at major %d\n", I2O_MAJOR);
  988. #endif
  989. /* Register Block OSM into I2O core */
  990. rc = i2o_driver_register(&i2o_block_driver);
  991. if (rc) {
  992. osm_err("Could not register Block driver\n");
  993. goto unregister_blkdev;
  994. }
  995. return 0;
  996. unregister_blkdev:
  997. unregister_blkdev(I2O_MAJOR, "i2o_block");
  998. free_mempool:
  999. mempool_destroy(i2o_blk_req_pool.pool);
  1000. free_slab:
  1001. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1002. exit:
  1003. return rc;
  1004. };
  1005. /**
  1006. * i2o_block_exit - Block OSM exit function
  1007. *
  1008. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  1009. * and frees the mempool and slab.
  1010. */
  1011. static void __exit i2o_block_exit(void)
  1012. {
  1013. /* Unregister I2O Block OSM from I2O core */
  1014. i2o_driver_unregister(&i2o_block_driver);
  1015. /* Unregister block device */
  1016. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1017. /* Free request mempool and slab */
  1018. mempool_destroy(i2o_blk_req_pool.pool);
  1019. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1020. };
  1021. MODULE_AUTHOR("Red Hat");
  1022. MODULE_LICENSE("GPL");
  1023. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1024. MODULE_VERSION(OSM_VERSION);
  1025. module_init(i2o_block_init);
  1026. module_exit(i2o_block_exit);