ibmvscsi.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robbin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/vio.h>
  73. #include <scsi/scsi.h>
  74. #include <scsi/scsi_cmnd.h>
  75. #include <scsi/scsi_host.h>
  76. #include <scsi/scsi_device.h>
  77. #include "ibmvscsi.h"
  78. /* The values below are somewhat arbitrary default values, but
  79. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  80. * Note that there are 3 bits of channel value, 6 bits of id, and
  81. * 5 bits of LUN.
  82. */
  83. static int max_id = 64;
  84. static int max_channel = 3;
  85. static int init_timeout = 5;
  86. static int max_requests = 50;
  87. #define IBMVSCSI_VERSION "1.5.5"
  88. MODULE_DESCRIPTION("IBM Virtual SCSI");
  89. MODULE_AUTHOR("Dave Boutcher");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(IBMVSCSI_VERSION);
  92. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  94. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(max_channel, "Largest channel value");
  96. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  98. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  100. /* ------------------------------------------------------------
  101. * Routines for the event pool and event structs
  102. */
  103. /**
  104. * initialize_event_pool: - Allocates and initializes the event pool for a host
  105. * @pool: event_pool to be initialized
  106. * @size: Number of events in pool
  107. * @hostdata: ibmvscsi_host_data who owns the event pool
  108. *
  109. * Returns zero on success.
  110. */
  111. static int initialize_event_pool(struct event_pool *pool,
  112. int size, struct ibmvscsi_host_data *hostdata)
  113. {
  114. int i;
  115. pool->size = size;
  116. pool->next = 0;
  117. pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
  118. if (!pool->events)
  119. return -ENOMEM;
  120. memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
  121. pool->iu_storage =
  122. dma_alloc_coherent(hostdata->dev,
  123. pool->size * sizeof(*pool->iu_storage),
  124. &pool->iu_token, 0);
  125. if (!pool->iu_storage) {
  126. kfree(pool->events);
  127. return -ENOMEM;
  128. }
  129. for (i = 0; i < pool->size; ++i) {
  130. struct srp_event_struct *evt = &pool->events[i];
  131. memset(&evt->crq, 0x00, sizeof(evt->crq));
  132. atomic_set(&evt->free, 1);
  133. evt->crq.valid = 0x80;
  134. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  135. evt->crq.IU_data_ptr = pool->iu_token +
  136. sizeof(*evt->xfer_iu) * i;
  137. evt->xfer_iu = pool->iu_storage + i;
  138. evt->hostdata = hostdata;
  139. }
  140. return 0;
  141. }
  142. /**
  143. * release_event_pool: - Frees memory of an event pool of a host
  144. * @pool: event_pool to be released
  145. * @hostdata: ibmvscsi_host_data who owns the even pool
  146. *
  147. * Returns zero on success.
  148. */
  149. static void release_event_pool(struct event_pool *pool,
  150. struct ibmvscsi_host_data *hostdata)
  151. {
  152. int i, in_use = 0;
  153. for (i = 0; i < pool->size; ++i)
  154. if (atomic_read(&pool->events[i].free) != 1)
  155. ++in_use;
  156. if (in_use)
  157. printk(KERN_WARNING
  158. "ibmvscsi: releasing event pool with %d "
  159. "events still in use?\n", in_use);
  160. kfree(pool->events);
  161. dma_free_coherent(hostdata->dev,
  162. pool->size * sizeof(*pool->iu_storage),
  163. pool->iu_storage, pool->iu_token);
  164. }
  165. /**
  166. * valid_event_struct: - Determines if event is valid.
  167. * @pool: event_pool that contains the event
  168. * @evt: srp_event_struct to be checked for validity
  169. *
  170. * Returns zero if event is invalid, one otherwise.
  171. */
  172. static int valid_event_struct(struct event_pool *pool,
  173. struct srp_event_struct *evt)
  174. {
  175. int index = evt - pool->events;
  176. if (index < 0 || index >= pool->size) /* outside of bounds */
  177. return 0;
  178. if (evt != pool->events + index) /* unaligned */
  179. return 0;
  180. return 1;
  181. }
  182. /**
  183. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  184. * @pool: event_pool that contains the event
  185. * @evt: srp_event_struct to be modified
  186. *
  187. */
  188. static void free_event_struct(struct event_pool *pool,
  189. struct srp_event_struct *evt)
  190. {
  191. if (!valid_event_struct(pool, evt)) {
  192. printk(KERN_ERR
  193. "ibmvscsi: Freeing invalid event_struct %p "
  194. "(not in pool %p)\n", evt, pool->events);
  195. return;
  196. }
  197. if (atomic_inc_return(&evt->free) != 1) {
  198. printk(KERN_ERR
  199. "ibmvscsi: Freeing event_struct %p "
  200. "which is not in use!\n", evt);
  201. return;
  202. }
  203. }
  204. /**
  205. * get_evt_struct: - Gets the next free event in pool
  206. * @pool: event_pool that contains the events to be searched
  207. *
  208. * Returns the next event in "free" state, and NULL if none are free.
  209. * Note that no synchronization is done here, we assume the host_lock
  210. * will syncrhonze things.
  211. */
  212. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  213. {
  214. int i;
  215. int poolsize = pool->size;
  216. int offset = pool->next;
  217. for (i = 0; i < poolsize; i++) {
  218. offset = (offset + 1) % poolsize;
  219. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  220. pool->next = offset;
  221. return &pool->events[offset];
  222. }
  223. }
  224. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  225. return NULL;
  226. }
  227. /**
  228. * init_event_struct: Initialize fields in an event struct that are always
  229. * required.
  230. * @evt: The event
  231. * @done: Routine to call when the event is responded to
  232. * @format: SRP or MAD format
  233. * @timeout: timeout value set in the CRQ
  234. */
  235. static void init_event_struct(struct srp_event_struct *evt_struct,
  236. void (*done) (struct srp_event_struct *),
  237. u8 format,
  238. int timeout)
  239. {
  240. evt_struct->cmnd = NULL;
  241. evt_struct->cmnd_done = NULL;
  242. evt_struct->sync_srp = NULL;
  243. evt_struct->crq.format = format;
  244. evt_struct->crq.timeout = timeout;
  245. evt_struct->done = done;
  246. }
  247. /* ------------------------------------------------------------
  248. * Routines for receiving SCSI responses from the hosting partition
  249. */
  250. /**
  251. * set_srp_direction: Set the fields in the srp related to data
  252. * direction and number of buffers based on the direction in
  253. * the scsi_cmnd and the number of buffers
  254. */
  255. static void set_srp_direction(struct scsi_cmnd *cmd,
  256. struct srp_cmd *srp_cmd,
  257. int numbuf)
  258. {
  259. if (numbuf == 0)
  260. return;
  261. if (numbuf == 1) {
  262. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  263. srp_cmd->data_out_format = SRP_DIRECT_BUFFER;
  264. else
  265. srp_cmd->data_in_format = SRP_DIRECT_BUFFER;
  266. } else {
  267. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  268. srp_cmd->data_out_format = SRP_INDIRECT_BUFFER;
  269. srp_cmd->data_out_count = numbuf;
  270. } else {
  271. srp_cmd->data_in_format = SRP_INDIRECT_BUFFER;
  272. srp_cmd->data_in_count = numbuf;
  273. }
  274. }
  275. }
  276. /**
  277. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  278. * @cmd: srp_cmd whose additional_data member will be unmapped
  279. * @dev: device for which the memory is mapped
  280. *
  281. */
  282. static void unmap_cmd_data(struct srp_cmd *cmd, struct device *dev)
  283. {
  284. int i;
  285. if ((cmd->data_out_format == SRP_NO_BUFFER) &&
  286. (cmd->data_in_format == SRP_NO_BUFFER))
  287. return;
  288. else if ((cmd->data_out_format == SRP_DIRECT_BUFFER) ||
  289. (cmd->data_in_format == SRP_DIRECT_BUFFER)) {
  290. struct memory_descriptor *data =
  291. (struct memory_descriptor *)cmd->additional_data;
  292. dma_unmap_single(dev, data->virtual_address, data->length,
  293. DMA_BIDIRECTIONAL);
  294. } else {
  295. struct indirect_descriptor *indirect =
  296. (struct indirect_descriptor *)cmd->additional_data;
  297. int num_mapped = indirect->head.length /
  298. sizeof(indirect->list[0]);
  299. for (i = 0; i < num_mapped; ++i) {
  300. struct memory_descriptor *data = &indirect->list[i];
  301. dma_unmap_single(dev,
  302. data->virtual_address,
  303. data->length, DMA_BIDIRECTIONAL);
  304. }
  305. }
  306. }
  307. /**
  308. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  309. * @cmd: Scsi_Cmnd with the scatterlist
  310. * @srp_cmd: srp_cmd that contains the memory descriptor
  311. * @dev: device for which to map dma memory
  312. *
  313. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  314. * Returns 1 on success.
  315. */
  316. static int map_sg_data(struct scsi_cmnd *cmd,
  317. struct srp_cmd *srp_cmd, struct device *dev)
  318. {
  319. int i, sg_mapped;
  320. u64 total_length = 0;
  321. struct scatterlist *sg = cmd->request_buffer;
  322. struct memory_descriptor *data =
  323. (struct memory_descriptor *)srp_cmd->additional_data;
  324. struct indirect_descriptor *indirect =
  325. (struct indirect_descriptor *)data;
  326. sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
  327. if (sg_mapped == 0)
  328. return 0;
  329. set_srp_direction(cmd, srp_cmd, sg_mapped);
  330. /* special case; we can use a single direct descriptor */
  331. if (sg_mapped == 1) {
  332. data->virtual_address = sg_dma_address(&sg[0]);
  333. data->length = sg_dma_len(&sg[0]);
  334. data->memory_handle = 0;
  335. return 1;
  336. }
  337. if (sg_mapped > MAX_INDIRECT_BUFS) {
  338. printk(KERN_ERR
  339. "ibmvscsi: More than %d mapped sg entries, got %d\n",
  340. MAX_INDIRECT_BUFS, sg_mapped);
  341. return 0;
  342. }
  343. indirect->head.virtual_address = 0;
  344. indirect->head.length = sg_mapped * sizeof(indirect->list[0]);
  345. indirect->head.memory_handle = 0;
  346. for (i = 0; i < sg_mapped; ++i) {
  347. struct memory_descriptor *descr = &indirect->list[i];
  348. struct scatterlist *sg_entry = &sg[i];
  349. descr->virtual_address = sg_dma_address(sg_entry);
  350. descr->length = sg_dma_len(sg_entry);
  351. descr->memory_handle = 0;
  352. total_length += sg_dma_len(sg_entry);
  353. }
  354. indirect->total_length = total_length;
  355. return 1;
  356. }
  357. /**
  358. * map_single_data: - Maps memory and initializes memory decriptor fields
  359. * @cmd: struct scsi_cmnd with the memory to be mapped
  360. * @srp_cmd: srp_cmd that contains the memory descriptor
  361. * @dev: device for which to map dma memory
  362. *
  363. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  364. * Returns 1 on success.
  365. */
  366. static int map_single_data(struct scsi_cmnd *cmd,
  367. struct srp_cmd *srp_cmd, struct device *dev)
  368. {
  369. struct memory_descriptor *data =
  370. (struct memory_descriptor *)srp_cmd->additional_data;
  371. data->virtual_address =
  372. dma_map_single(dev, cmd->request_buffer,
  373. cmd->request_bufflen,
  374. DMA_BIDIRECTIONAL);
  375. if (dma_mapping_error(data->virtual_address)) {
  376. printk(KERN_ERR
  377. "ibmvscsi: Unable to map request_buffer for command!\n");
  378. return 0;
  379. }
  380. data->length = cmd->request_bufflen;
  381. data->memory_handle = 0;
  382. set_srp_direction(cmd, srp_cmd, 1);
  383. return 1;
  384. }
  385. /**
  386. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  387. * @cmd: struct scsi_cmnd with the memory to be mapped
  388. * @srp_cmd: srp_cmd that contains the memory descriptor
  389. * @dev: dma device for which to map dma memory
  390. *
  391. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  392. * Returns 1 on success.
  393. */
  394. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  395. struct srp_cmd *srp_cmd, struct device *dev)
  396. {
  397. switch (cmd->sc_data_direction) {
  398. case DMA_FROM_DEVICE:
  399. case DMA_TO_DEVICE:
  400. break;
  401. case DMA_NONE:
  402. return 1;
  403. case DMA_BIDIRECTIONAL:
  404. printk(KERN_ERR
  405. "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
  406. return 0;
  407. default:
  408. printk(KERN_ERR
  409. "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
  410. cmd->sc_data_direction);
  411. return 0;
  412. }
  413. if (!cmd->request_buffer)
  414. return 1;
  415. if (cmd->use_sg)
  416. return map_sg_data(cmd, srp_cmd, dev);
  417. return map_single_data(cmd, srp_cmd, dev);
  418. }
  419. /* ------------------------------------------------------------
  420. * Routines for sending and receiving SRPs
  421. */
  422. /**
  423. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  424. * @evt_struct: evt_struct to be sent
  425. * @hostdata: ibmvscsi_host_data of host
  426. *
  427. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  428. * Note that this routine assumes that host_lock is held for synchronization
  429. */
  430. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  431. struct ibmvscsi_host_data *hostdata)
  432. {
  433. struct scsi_cmnd *cmnd;
  434. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  435. int rc;
  436. /* If we have exhausted our request limit, just fail this request.
  437. * Note that there are rare cases involving driver generated requests
  438. * (such as task management requests) that the mid layer may think we
  439. * can handle more requests (can_queue) when we actually can't
  440. */
  441. if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
  442. (atomic_dec_if_positive(&hostdata->request_limit) < 0)) {
  443. /* See if the adapter is disabled */
  444. if (atomic_read(&hostdata->request_limit) < 0)
  445. goto send_error;
  446. printk(KERN_WARNING
  447. "ibmvscsi: Warning, request_limit exceeded\n");
  448. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  449. hostdata->dev);
  450. free_event_struct(&hostdata->pool, evt_struct);
  451. return SCSI_MLQUEUE_HOST_BUSY;
  452. }
  453. /* Copy the IU into the transfer area */
  454. *evt_struct->xfer_iu = evt_struct->iu;
  455. evt_struct->xfer_iu->srp.generic.tag = (u64)evt_struct;
  456. /* Add this to the sent list. We need to do this
  457. * before we actually send
  458. * in case it comes back REALLY fast
  459. */
  460. list_add_tail(&evt_struct->list, &hostdata->sent);
  461. if ((rc =
  462. ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  463. list_del(&evt_struct->list);
  464. printk(KERN_ERR "ibmvscsi: failed to send event struct rc %d\n",
  465. rc);
  466. goto send_error;
  467. }
  468. return 0;
  469. send_error:
  470. unmap_cmd_data(&evt_struct->iu.srp.cmd, hostdata->dev);
  471. if ((cmnd = evt_struct->cmnd) != NULL) {
  472. cmnd->result = DID_ERROR << 16;
  473. evt_struct->cmnd_done(cmnd);
  474. } else if (evt_struct->done)
  475. evt_struct->done(evt_struct);
  476. free_event_struct(&hostdata->pool, evt_struct);
  477. return 0;
  478. }
  479. /**
  480. * handle_cmd_rsp: - Handle responses from commands
  481. * @evt_struct: srp_event_struct to be handled
  482. *
  483. * Used as a callback by when sending scsi cmds.
  484. * Gets called by ibmvscsi_handle_crq()
  485. */
  486. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  487. {
  488. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  489. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  490. if (unlikely(rsp->type != SRP_RSP_TYPE)) {
  491. if (printk_ratelimit())
  492. printk(KERN_WARNING
  493. "ibmvscsi: bad SRP RSP type %d\n",
  494. rsp->type);
  495. }
  496. if (cmnd) {
  497. cmnd->result = rsp->status;
  498. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  499. memcpy(cmnd->sense_buffer,
  500. rsp->sense_and_response_data,
  501. rsp->sense_data_list_length);
  502. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  503. evt_struct->hostdata->dev);
  504. if (rsp->doover)
  505. cmnd->resid = rsp->data_out_residual_count;
  506. else if (rsp->diover)
  507. cmnd->resid = rsp->data_in_residual_count;
  508. }
  509. if (evt_struct->cmnd_done)
  510. evt_struct->cmnd_done(cmnd);
  511. }
  512. /**
  513. * lun_from_dev: - Returns the lun of the scsi device
  514. * @dev: struct scsi_device
  515. *
  516. */
  517. static inline u16 lun_from_dev(struct scsi_device *dev)
  518. {
  519. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  520. }
  521. /**
  522. * ibmvscsi_queue: - The queuecommand function of the scsi template
  523. * @cmd: struct scsi_cmnd to be executed
  524. * @done: Callback function to be called when cmd is completed
  525. */
  526. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  527. void (*done) (struct scsi_cmnd *))
  528. {
  529. struct srp_cmd *srp_cmd;
  530. struct srp_event_struct *evt_struct;
  531. struct ibmvscsi_host_data *hostdata =
  532. (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
  533. u16 lun = lun_from_dev(cmnd->device);
  534. evt_struct = get_event_struct(&hostdata->pool);
  535. if (!evt_struct)
  536. return SCSI_MLQUEUE_HOST_BUSY;
  537. init_event_struct(evt_struct,
  538. handle_cmd_rsp,
  539. VIOSRP_SRP_FORMAT,
  540. cmnd->timeout);
  541. evt_struct->cmnd = cmnd;
  542. evt_struct->cmnd_done = done;
  543. /* Set up the actual SRP IU */
  544. srp_cmd = &evt_struct->iu.srp.cmd;
  545. memset(srp_cmd, 0x00, sizeof(*srp_cmd));
  546. srp_cmd->type = SRP_CMD_TYPE;
  547. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
  548. srp_cmd->lun = ((u64) lun) << 48;
  549. if (!map_data_for_srp_cmd(cmnd, srp_cmd, hostdata->dev)) {
  550. printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
  551. free_event_struct(&hostdata->pool, evt_struct);
  552. return SCSI_MLQUEUE_HOST_BUSY;
  553. }
  554. /* Fix up dma address of the buffer itself */
  555. if ((srp_cmd->data_out_format == SRP_INDIRECT_BUFFER) ||
  556. (srp_cmd->data_in_format == SRP_INDIRECT_BUFFER)) {
  557. struct indirect_descriptor *indirect =
  558. (struct indirect_descriptor *)srp_cmd->additional_data;
  559. indirect->head.virtual_address = evt_struct->crq.IU_data_ptr +
  560. offsetof(struct srp_cmd, additional_data) +
  561. offsetof(struct indirect_descriptor, list);
  562. }
  563. return ibmvscsi_send_srp_event(evt_struct, hostdata);
  564. }
  565. /* ------------------------------------------------------------
  566. * Routines for driver initialization
  567. */
  568. /**
  569. * adapter_info_rsp: - Handle response to MAD adapter info request
  570. * @evt_struct: srp_event_struct with the response
  571. *
  572. * Used as a "done" callback by when sending adapter_info. Gets called
  573. * by ibmvscsi_handle_crq()
  574. */
  575. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  576. {
  577. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  578. dma_unmap_single(hostdata->dev,
  579. evt_struct->iu.mad.adapter_info.buffer,
  580. evt_struct->iu.mad.adapter_info.common.length,
  581. DMA_BIDIRECTIONAL);
  582. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  583. printk("ibmvscsi: error %d getting adapter info\n",
  584. evt_struct->xfer_iu->mad.adapter_info.common.status);
  585. } else {
  586. printk("ibmvscsi: host srp version: %s, "
  587. "host partition %s (%d), OS %d, max io %u\n",
  588. hostdata->madapter_info.srp_version,
  589. hostdata->madapter_info.partition_name,
  590. hostdata->madapter_info.partition_number,
  591. hostdata->madapter_info.os_type,
  592. hostdata->madapter_info.port_max_txu[0]);
  593. if (hostdata->madapter_info.port_max_txu[0])
  594. hostdata->host->max_sectors =
  595. hostdata->madapter_info.port_max_txu[0] >> 9;
  596. }
  597. }
  598. /**
  599. * send_mad_adapter_info: - Sends the mad adapter info request
  600. * and stores the result so it can be retrieved with
  601. * sysfs. We COULD consider causing a failure if the
  602. * returned SRP version doesn't match ours.
  603. * @hostdata: ibmvscsi_host_data of host
  604. *
  605. * Returns zero if successful.
  606. */
  607. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  608. {
  609. struct viosrp_adapter_info *req;
  610. struct srp_event_struct *evt_struct;
  611. memset(&hostdata->madapter_info, 0x00, sizeof(hostdata->madapter_info));
  612. evt_struct = get_event_struct(&hostdata->pool);
  613. if (!evt_struct) {
  614. printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
  615. "for ADAPTER_INFO_REQ!\n");
  616. return;
  617. }
  618. init_event_struct(evt_struct,
  619. adapter_info_rsp,
  620. VIOSRP_MAD_FORMAT,
  621. init_timeout * HZ);
  622. req = &evt_struct->iu.mad.adapter_info;
  623. memset(req, 0x00, sizeof(*req));
  624. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  625. req->common.length = sizeof(hostdata->madapter_info);
  626. req->buffer = dma_map_single(hostdata->dev,
  627. &hostdata->madapter_info,
  628. sizeof(hostdata->madapter_info),
  629. DMA_BIDIRECTIONAL);
  630. if (dma_mapping_error(req->buffer)) {
  631. printk(KERN_ERR
  632. "ibmvscsi: Unable to map request_buffer "
  633. "for adapter_info!\n");
  634. free_event_struct(&hostdata->pool, evt_struct);
  635. return;
  636. }
  637. if (ibmvscsi_send_srp_event(evt_struct, hostdata))
  638. printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
  639. };
  640. /**
  641. * login_rsp: - Handle response to SRP login request
  642. * @evt_struct: srp_event_struct with the response
  643. *
  644. * Used as a "done" callback by when sending srp_login. Gets called
  645. * by ibmvscsi_handle_crq()
  646. */
  647. static void login_rsp(struct srp_event_struct *evt_struct)
  648. {
  649. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  650. switch (evt_struct->xfer_iu->srp.generic.type) {
  651. case SRP_LOGIN_RSP_TYPE: /* it worked! */
  652. break;
  653. case SRP_LOGIN_REJ_TYPE: /* refused! */
  654. printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REQ rejected\n");
  655. /* Login failed. */
  656. atomic_set(&hostdata->request_limit, -1);
  657. return;
  658. default:
  659. printk(KERN_ERR
  660. "ibmvscsi: Invalid login response typecode 0x%02x!\n",
  661. evt_struct->xfer_iu->srp.generic.type);
  662. /* Login failed. */
  663. atomic_set(&hostdata->request_limit, -1);
  664. return;
  665. }
  666. printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
  667. if (evt_struct->xfer_iu->srp.login_rsp.request_limit_delta >
  668. (max_requests - 2))
  669. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta =
  670. max_requests - 2;
  671. /* Now we know what the real request-limit is */
  672. atomic_set(&hostdata->request_limit,
  673. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta);
  674. hostdata->host->can_queue =
  675. evt_struct->xfer_iu->srp.login_rsp.request_limit_delta - 2;
  676. if (hostdata->host->can_queue < 1) {
  677. printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
  678. return;
  679. }
  680. send_mad_adapter_info(hostdata);
  681. return;
  682. }
  683. /**
  684. * send_srp_login: - Sends the srp login
  685. * @hostdata: ibmvscsi_host_data of host
  686. *
  687. * Returns zero if successful.
  688. */
  689. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  690. {
  691. int rc;
  692. unsigned long flags;
  693. struct srp_login_req *login;
  694. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  695. if (!evt_struct) {
  696. printk(KERN_ERR
  697. "ibmvscsi: couldn't allocate an event for login req!\n");
  698. return FAILED;
  699. }
  700. init_event_struct(evt_struct,
  701. login_rsp,
  702. VIOSRP_SRP_FORMAT,
  703. init_timeout * HZ);
  704. login = &evt_struct->iu.srp.login_req;
  705. login->type = SRP_LOGIN_REQ_TYPE;
  706. login->max_requested_initiator_to_target_iulen = sizeof(union srp_iu);
  707. login->required_buffer_formats = 0x0006;
  708. /* Start out with a request limit of 1, since this is negotiated in
  709. * the login request we are just sending
  710. */
  711. atomic_set(&hostdata->request_limit, 1);
  712. spin_lock_irqsave(hostdata->host->host_lock, flags);
  713. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  714. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  715. return rc;
  716. };
  717. /**
  718. * sync_completion: Signal that a synchronous command has completed
  719. * Note that after returning from this call, the evt_struct is freed.
  720. * the caller waiting on this completion shouldn't touch the evt_struct
  721. * again.
  722. */
  723. static void sync_completion(struct srp_event_struct *evt_struct)
  724. {
  725. /* copy the response back */
  726. if (evt_struct->sync_srp)
  727. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  728. complete(&evt_struct->comp);
  729. }
  730. /**
  731. * ibmvscsi_abort: Abort a command...from scsi host template
  732. * send this over to the server and wait synchronously for the response
  733. */
  734. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  735. {
  736. struct ibmvscsi_host_data *hostdata =
  737. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  738. struct srp_tsk_mgmt *tsk_mgmt;
  739. struct srp_event_struct *evt;
  740. struct srp_event_struct *tmp_evt, *found_evt;
  741. union viosrp_iu srp_rsp;
  742. int rsp_rc;
  743. u16 lun = lun_from_dev(cmd->device);
  744. /* First, find this command in our sent list so we can figure
  745. * out the correct tag
  746. */
  747. found_evt = NULL;
  748. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  749. if (tmp_evt->cmnd == cmd) {
  750. found_evt = tmp_evt;
  751. break;
  752. }
  753. }
  754. if (!found_evt)
  755. return FAILED;
  756. evt = get_event_struct(&hostdata->pool);
  757. if (evt == NULL) {
  758. printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
  759. return FAILED;
  760. }
  761. init_event_struct(evt,
  762. sync_completion,
  763. VIOSRP_SRP_FORMAT,
  764. init_timeout * HZ);
  765. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  766. /* Set up an abort SRP command */
  767. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  768. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  769. tsk_mgmt->lun = ((u64) lun) << 48;
  770. tsk_mgmt->task_mgmt_flags = 0x01; /* ABORT TASK */
  771. tsk_mgmt->managed_task_tag = (u64) found_evt;
  772. printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
  773. tsk_mgmt->lun, tsk_mgmt->managed_task_tag);
  774. evt->sync_srp = &srp_rsp;
  775. init_completion(&evt->comp);
  776. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  777. printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
  778. return FAILED;
  779. }
  780. spin_unlock_irq(hostdata->host->host_lock);
  781. wait_for_completion(&evt->comp);
  782. spin_lock_irq(hostdata->host->host_lock);
  783. /* make sure we got a good response */
  784. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  785. if (printk_ratelimit())
  786. printk(KERN_WARNING
  787. "ibmvscsi: abort bad SRP RSP type %d\n",
  788. srp_rsp.srp.generic.type);
  789. return FAILED;
  790. }
  791. if (srp_rsp.srp.rsp.rspvalid)
  792. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  793. else
  794. rsp_rc = srp_rsp.srp.rsp.status;
  795. if (rsp_rc) {
  796. if (printk_ratelimit())
  797. printk(KERN_WARNING
  798. "ibmvscsi: abort code %d for task tag 0x%lx\n",
  799. rsp_rc,
  800. tsk_mgmt->managed_task_tag);
  801. return FAILED;
  802. }
  803. /* Because we dropped the spinlock above, it's possible
  804. * The event is no longer in our list. Make sure it didn't
  805. * complete while we were aborting
  806. */
  807. found_evt = NULL;
  808. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  809. if (tmp_evt->cmnd == cmd) {
  810. found_evt = tmp_evt;
  811. break;
  812. }
  813. }
  814. if (found_evt == NULL) {
  815. printk(KERN_INFO
  816. "ibmvscsi: aborted task tag 0x%lx completed\n",
  817. tsk_mgmt->managed_task_tag);
  818. return SUCCESS;
  819. }
  820. printk(KERN_INFO
  821. "ibmvscsi: successfully aborted task tag 0x%lx\n",
  822. tsk_mgmt->managed_task_tag);
  823. cmd->result = (DID_ABORT << 16);
  824. list_del(&found_evt->list);
  825. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt->hostdata->dev);
  826. free_event_struct(&found_evt->hostdata->pool, found_evt);
  827. atomic_inc(&hostdata->request_limit);
  828. return SUCCESS;
  829. }
  830. /**
  831. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  832. * template send this over to the server and wait synchronously for the
  833. * response
  834. */
  835. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  836. {
  837. struct ibmvscsi_host_data *hostdata =
  838. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  839. struct srp_tsk_mgmt *tsk_mgmt;
  840. struct srp_event_struct *evt;
  841. struct srp_event_struct *tmp_evt, *pos;
  842. union viosrp_iu srp_rsp;
  843. int rsp_rc;
  844. u16 lun = lun_from_dev(cmd->device);
  845. evt = get_event_struct(&hostdata->pool);
  846. if (evt == NULL) {
  847. printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
  848. return FAILED;
  849. }
  850. init_event_struct(evt,
  851. sync_completion,
  852. VIOSRP_SRP_FORMAT,
  853. init_timeout * HZ);
  854. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  855. /* Set up a lun reset SRP command */
  856. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  857. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  858. tsk_mgmt->lun = ((u64) lun) << 48;
  859. tsk_mgmt->task_mgmt_flags = 0x08; /* LUN RESET */
  860. printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
  861. tsk_mgmt->lun);
  862. evt->sync_srp = &srp_rsp;
  863. init_completion(&evt->comp);
  864. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  865. printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
  866. return FAILED;
  867. }
  868. spin_unlock_irq(hostdata->host->host_lock);
  869. wait_for_completion(&evt->comp);
  870. spin_lock_irq(hostdata->host->host_lock);
  871. /* make sure we got a good response */
  872. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  873. if (printk_ratelimit())
  874. printk(KERN_WARNING
  875. "ibmvscsi: reset bad SRP RSP type %d\n",
  876. srp_rsp.srp.generic.type);
  877. return FAILED;
  878. }
  879. if (srp_rsp.srp.rsp.rspvalid)
  880. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  881. else
  882. rsp_rc = srp_rsp.srp.rsp.status;
  883. if (rsp_rc) {
  884. if (printk_ratelimit())
  885. printk(KERN_WARNING
  886. "ibmvscsi: reset code %d for task tag 0x%lx\n",
  887. rsp_rc,
  888. tsk_mgmt->managed_task_tag);
  889. return FAILED;
  890. }
  891. /* We need to find all commands for this LUN that have not yet been
  892. * responded to, and fail them with DID_RESET
  893. */
  894. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  895. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  896. if (tmp_evt->cmnd)
  897. tmp_evt->cmnd->result = (DID_RESET << 16);
  898. list_del(&tmp_evt->list);
  899. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt->hostdata->dev);
  900. free_event_struct(&tmp_evt->hostdata->pool,
  901. tmp_evt);
  902. atomic_inc(&hostdata->request_limit);
  903. if (tmp_evt->cmnd_done)
  904. tmp_evt->cmnd_done(tmp_evt->cmnd);
  905. else if (tmp_evt->done)
  906. tmp_evt->done(tmp_evt);
  907. }
  908. }
  909. return SUCCESS;
  910. }
  911. /**
  912. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  913. * @hostdata: the adapter
  914. */
  915. static void purge_requests(struct ibmvscsi_host_data *hostdata)
  916. {
  917. struct srp_event_struct *tmp_evt, *pos;
  918. unsigned long flags;
  919. spin_lock_irqsave(hostdata->host->host_lock, flags);
  920. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  921. list_del(&tmp_evt->list);
  922. if (tmp_evt->cmnd) {
  923. tmp_evt->cmnd->result = (DID_ERROR << 16);
  924. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  925. tmp_evt->hostdata->dev);
  926. if (tmp_evt->cmnd_done)
  927. tmp_evt->cmnd_done(tmp_evt->cmnd);
  928. } else {
  929. if (tmp_evt->done) {
  930. tmp_evt->done(tmp_evt);
  931. }
  932. }
  933. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  934. }
  935. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  936. }
  937. /**
  938. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  939. * @crq: Command/Response queue
  940. * @hostdata: ibmvscsi_host_data of host
  941. *
  942. */
  943. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  944. struct ibmvscsi_host_data *hostdata)
  945. {
  946. unsigned long flags;
  947. struct srp_event_struct *evt_struct =
  948. (struct srp_event_struct *)crq->IU_data_ptr;
  949. switch (crq->valid) {
  950. case 0xC0: /* initialization */
  951. switch (crq->format) {
  952. case 0x01: /* Initialization message */
  953. printk(KERN_INFO "ibmvscsi: partner initialized\n");
  954. /* Send back a response */
  955. if (ibmvscsi_send_crq(hostdata,
  956. 0xC002000000000000LL, 0) == 0) {
  957. /* Now login */
  958. send_srp_login(hostdata);
  959. } else {
  960. printk(KERN_ERR
  961. "ibmvscsi: Unable to send init rsp\n");
  962. }
  963. break;
  964. case 0x02: /* Initialization response */
  965. printk(KERN_INFO
  966. "ibmvscsi: partner initialization complete\n");
  967. /* Now login */
  968. send_srp_login(hostdata);
  969. break;
  970. default:
  971. printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
  972. }
  973. return;
  974. case 0xFF: /* Hypervisor telling us the connection is closed */
  975. printk(KERN_INFO "ibmvscsi: Virtual adapter failed!\n");
  976. atomic_set(&hostdata->request_limit, -1);
  977. purge_requests(hostdata);
  978. ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  979. return;
  980. case 0x80: /* real payload */
  981. break;
  982. default:
  983. printk(KERN_ERR
  984. "ibmvscsi: got an invalid message type 0x%02x\n",
  985. crq->valid);
  986. return;
  987. }
  988. /* The only kind of payload CRQs we should get are responses to
  989. * things we send. Make sure this response is to something we
  990. * actually sent
  991. */
  992. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  993. printk(KERN_ERR
  994. "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
  995. (void *)crq->IU_data_ptr);
  996. return;
  997. }
  998. if (atomic_read(&evt_struct->free)) {
  999. printk(KERN_ERR
  1000. "ibmvscsi: received duplicate correlation_token 0x%p!\n",
  1001. (void *)crq->IU_data_ptr);
  1002. return;
  1003. }
  1004. if (crq->format == VIOSRP_SRP_FORMAT)
  1005. atomic_add(evt_struct->xfer_iu->srp.rsp.request_limit_delta,
  1006. &hostdata->request_limit);
  1007. if (evt_struct->done)
  1008. evt_struct->done(evt_struct);
  1009. else
  1010. printk(KERN_ERR
  1011. "ibmvscsi: returned done() is NULL; not running it!\n");
  1012. /*
  1013. * Lock the host_lock before messing with these structures, since we
  1014. * are running in a task context
  1015. */
  1016. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1017. list_del(&evt_struct->list);
  1018. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1019. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1020. }
  1021. /**
  1022. * ibmvscsi_get_host_config: Send the command to the server to get host
  1023. * configuration data. The data is opaque to us.
  1024. */
  1025. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1026. unsigned char *buffer, int length)
  1027. {
  1028. struct viosrp_host_config *host_config;
  1029. struct srp_event_struct *evt_struct;
  1030. int rc;
  1031. evt_struct = get_event_struct(&hostdata->pool);
  1032. if (!evt_struct) {
  1033. printk(KERN_ERR
  1034. "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
  1035. return -1;
  1036. }
  1037. init_event_struct(evt_struct,
  1038. sync_completion,
  1039. VIOSRP_MAD_FORMAT,
  1040. init_timeout * HZ);
  1041. host_config = &evt_struct->iu.mad.host_config;
  1042. /* Set up a lun reset SRP command */
  1043. memset(host_config, 0x00, sizeof(*host_config));
  1044. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1045. host_config->common.length = length;
  1046. host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
  1047. DMA_BIDIRECTIONAL);
  1048. if (dma_mapping_error(host_config->buffer)) {
  1049. printk(KERN_ERR
  1050. "ibmvscsi: dma_mapping error " "getting host config\n");
  1051. free_event_struct(&hostdata->pool, evt_struct);
  1052. return -1;
  1053. }
  1054. init_completion(&evt_struct->comp);
  1055. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  1056. if (rc == 0) {
  1057. wait_for_completion(&evt_struct->comp);
  1058. dma_unmap_single(hostdata->dev, host_config->buffer,
  1059. length, DMA_BIDIRECTIONAL);
  1060. }
  1061. return rc;
  1062. }
  1063. /* ------------------------------------------------------------
  1064. * sysfs attributes
  1065. */
  1066. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1067. {
  1068. struct Scsi_Host *shost = class_to_shost(class_dev);
  1069. struct ibmvscsi_host_data *hostdata =
  1070. (struct ibmvscsi_host_data *)shost->hostdata;
  1071. int len;
  1072. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1073. hostdata->madapter_info.srp_version);
  1074. return len;
  1075. }
  1076. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1077. .attr = {
  1078. .name = "srp_version",
  1079. .mode = S_IRUGO,
  1080. },
  1081. .show = show_host_srp_version,
  1082. };
  1083. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1084. char *buf)
  1085. {
  1086. struct Scsi_Host *shost = class_to_shost(class_dev);
  1087. struct ibmvscsi_host_data *hostdata =
  1088. (struct ibmvscsi_host_data *)shost->hostdata;
  1089. int len;
  1090. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1091. hostdata->madapter_info.partition_name);
  1092. return len;
  1093. }
  1094. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1095. .attr = {
  1096. .name = "partition_name",
  1097. .mode = S_IRUGO,
  1098. },
  1099. .show = show_host_partition_name,
  1100. };
  1101. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1102. char *buf)
  1103. {
  1104. struct Scsi_Host *shost = class_to_shost(class_dev);
  1105. struct ibmvscsi_host_data *hostdata =
  1106. (struct ibmvscsi_host_data *)shost->hostdata;
  1107. int len;
  1108. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1109. hostdata->madapter_info.partition_number);
  1110. return len;
  1111. }
  1112. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1113. .attr = {
  1114. .name = "partition_number",
  1115. .mode = S_IRUGO,
  1116. },
  1117. .show = show_host_partition_number,
  1118. };
  1119. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1120. {
  1121. struct Scsi_Host *shost = class_to_shost(class_dev);
  1122. struct ibmvscsi_host_data *hostdata =
  1123. (struct ibmvscsi_host_data *)shost->hostdata;
  1124. int len;
  1125. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1126. hostdata->madapter_info.mad_version);
  1127. return len;
  1128. }
  1129. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1130. .attr = {
  1131. .name = "mad_version",
  1132. .mode = S_IRUGO,
  1133. },
  1134. .show = show_host_mad_version,
  1135. };
  1136. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1137. {
  1138. struct Scsi_Host *shost = class_to_shost(class_dev);
  1139. struct ibmvscsi_host_data *hostdata =
  1140. (struct ibmvscsi_host_data *)shost->hostdata;
  1141. int len;
  1142. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1143. return len;
  1144. }
  1145. static struct class_device_attribute ibmvscsi_host_os_type = {
  1146. .attr = {
  1147. .name = "os_type",
  1148. .mode = S_IRUGO,
  1149. },
  1150. .show = show_host_os_type,
  1151. };
  1152. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1153. {
  1154. struct Scsi_Host *shost = class_to_shost(class_dev);
  1155. struct ibmvscsi_host_data *hostdata =
  1156. (struct ibmvscsi_host_data *)shost->hostdata;
  1157. /* returns null-terminated host config data */
  1158. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1159. return strlen(buf);
  1160. else
  1161. return 0;
  1162. }
  1163. static struct class_device_attribute ibmvscsi_host_config = {
  1164. .attr = {
  1165. .name = "config",
  1166. .mode = S_IRUGO,
  1167. },
  1168. .show = show_host_config,
  1169. };
  1170. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1171. &ibmvscsi_host_srp_version,
  1172. &ibmvscsi_host_partition_name,
  1173. &ibmvscsi_host_partition_number,
  1174. &ibmvscsi_host_mad_version,
  1175. &ibmvscsi_host_os_type,
  1176. &ibmvscsi_host_config,
  1177. NULL
  1178. };
  1179. /* ------------------------------------------------------------
  1180. * SCSI driver registration
  1181. */
  1182. static struct scsi_host_template driver_template = {
  1183. .module = THIS_MODULE,
  1184. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1185. .proc_name = "ibmvscsi",
  1186. .queuecommand = ibmvscsi_queuecommand,
  1187. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1188. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1189. .cmd_per_lun = 16,
  1190. .can_queue = 1, /* Updated after SRP_LOGIN */
  1191. .this_id = -1,
  1192. .sg_tablesize = MAX_INDIRECT_BUFS,
  1193. .use_clustering = ENABLE_CLUSTERING,
  1194. .shost_attrs = ibmvscsi_attrs,
  1195. };
  1196. /**
  1197. * Called by bus code for each adapter
  1198. */
  1199. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1200. {
  1201. struct ibmvscsi_host_data *hostdata;
  1202. struct Scsi_Host *host;
  1203. struct device *dev = &vdev->dev;
  1204. unsigned long wait_switch = 0;
  1205. vdev->dev.driver_data = NULL;
  1206. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1207. if (!host) {
  1208. printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
  1209. goto scsi_host_alloc_failed;
  1210. }
  1211. hostdata = (struct ibmvscsi_host_data *)host->hostdata;
  1212. memset(hostdata, 0x00, sizeof(*hostdata));
  1213. INIT_LIST_HEAD(&hostdata->sent);
  1214. hostdata->host = host;
  1215. hostdata->dev = dev;
  1216. atomic_set(&hostdata->request_limit, -1);
  1217. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1218. if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
  1219. max_requests) != 0) {
  1220. printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
  1221. goto init_crq_failed;
  1222. }
  1223. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1224. printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
  1225. goto init_pool_failed;
  1226. }
  1227. host->max_lun = 8;
  1228. host->max_id = max_id;
  1229. host->max_channel = max_channel;
  1230. if (scsi_add_host(hostdata->host, hostdata->dev))
  1231. goto add_host_failed;
  1232. /* Try to send an initialization message. Note that this is allowed
  1233. * to fail if the other end is not acive. In that case we don't
  1234. * want to scan
  1235. */
  1236. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
  1237. /*
  1238. * Wait around max init_timeout secs for the adapter to finish
  1239. * initializing. When we are done initializing, we will have a
  1240. * valid request_limit. We don't want Linux scanning before
  1241. * we are ready.
  1242. */
  1243. for (wait_switch = jiffies + (init_timeout * HZ);
  1244. time_before(jiffies, wait_switch) &&
  1245. atomic_read(&hostdata->request_limit) < 2;) {
  1246. msleep(10);
  1247. }
  1248. /* if we now have a valid request_limit, initiate a scan */
  1249. if (atomic_read(&hostdata->request_limit) > 0)
  1250. scsi_scan_host(host);
  1251. }
  1252. vdev->dev.driver_data = hostdata;
  1253. return 0;
  1254. add_host_failed:
  1255. release_event_pool(&hostdata->pool, hostdata);
  1256. init_pool_failed:
  1257. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1258. init_crq_failed:
  1259. scsi_host_put(host);
  1260. scsi_host_alloc_failed:
  1261. return -1;
  1262. }
  1263. static int ibmvscsi_remove(struct vio_dev *vdev)
  1264. {
  1265. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1266. release_event_pool(&hostdata->pool, hostdata);
  1267. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  1268. max_requests);
  1269. scsi_remove_host(hostdata->host);
  1270. scsi_host_put(hostdata->host);
  1271. return 0;
  1272. }
  1273. /**
  1274. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1275. * support.
  1276. */
  1277. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1278. {"vscsi", "IBM,v-scsi"},
  1279. {0,}
  1280. };
  1281. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1282. static struct vio_driver ibmvscsi_driver = {
  1283. .name = "ibmvscsi",
  1284. .id_table = ibmvscsi_device_table,
  1285. .probe = ibmvscsi_probe,
  1286. .remove = ibmvscsi_remove
  1287. };
  1288. int __init ibmvscsi_module_init(void)
  1289. {
  1290. return vio_register_driver(&ibmvscsi_driver);
  1291. }
  1292. void __exit ibmvscsi_module_exit(void)
  1293. {
  1294. vio_unregister_driver(&ibmvscsi_driver);
  1295. }
  1296. module_init(ibmvscsi_module_init);
  1297. module_exit(ibmvscsi_module_exit);