ibmvscsi.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. wait_for_completion(&evt->comp);
  781. /* make sure we got a good response */
  782. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  783. if (printk_ratelimit())
  784. printk(KERN_WARNING
  785. "ibmvscsi: abort bad SRP RSP type %d\n",
  786. srp_rsp.srp.generic.type);
  787. return FAILED;
  788. }
  789. if (srp_rsp.srp.rsp.rspvalid)
  790. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  791. else
  792. rsp_rc = srp_rsp.srp.rsp.status;
  793. if (rsp_rc) {
  794. if (printk_ratelimit())
  795. printk(KERN_WARNING
  796. "ibmvscsi: abort code %d for task tag 0x%lx\n",
  797. rsp_rc,
  798. tsk_mgmt->managed_task_tag);
  799. return FAILED;
  800. }
  801. /* Because we dropped the spinlock above, it's possible
  802. * The event is no longer in our list. Make sure it didn't
  803. * complete while we were aborting
  804. */
  805. found_evt = NULL;
  806. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  807. if (tmp_evt->cmnd == cmd) {
  808. found_evt = tmp_evt;
  809. break;
  810. }
  811. }
  812. if (found_evt == NULL) {
  813. printk(KERN_INFO
  814. "ibmvscsi: aborted task tag 0x%lx completed\n",
  815. tsk_mgmt->managed_task_tag);
  816. return SUCCESS;
  817. }
  818. printk(KERN_INFO
  819. "ibmvscsi: successfully aborted task tag 0x%lx\n",
  820. tsk_mgmt->managed_task_tag);
  821. cmd->result = (DID_ABORT << 16);
  822. list_del(&found_evt->list);
  823. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt->hostdata->dev);
  824. free_event_struct(&found_evt->hostdata->pool, found_evt);
  825. atomic_inc(&hostdata->request_limit);
  826. return SUCCESS;
  827. }
  828. /**
  829. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  830. * template send this over to the server and wait synchronously for the
  831. * response
  832. */
  833. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  834. {
  835. struct ibmvscsi_host_data *hostdata =
  836. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  837. struct srp_tsk_mgmt *tsk_mgmt;
  838. struct srp_event_struct *evt;
  839. struct srp_event_struct *tmp_evt, *pos;
  840. union viosrp_iu srp_rsp;
  841. int rsp_rc;
  842. u16 lun = lun_from_dev(cmd->device);
  843. evt = get_event_struct(&hostdata->pool);
  844. if (evt == NULL) {
  845. printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
  846. return FAILED;
  847. }
  848. init_event_struct(evt,
  849. sync_completion,
  850. VIOSRP_SRP_FORMAT,
  851. init_timeout * HZ);
  852. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  853. /* Set up a lun reset SRP command */
  854. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  855. tsk_mgmt->type = SRP_TSK_MGMT_TYPE;
  856. tsk_mgmt->lun = ((u64) lun) << 48;
  857. tsk_mgmt->task_mgmt_flags = 0x08; /* LUN RESET */
  858. printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
  859. tsk_mgmt->lun);
  860. evt->sync_srp = &srp_rsp;
  861. init_completion(&evt->comp);
  862. if (ibmvscsi_send_srp_event(evt, hostdata) != 0) {
  863. printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
  864. return FAILED;
  865. }
  866. wait_for_completion(&evt->comp);
  867. /* make sure we got a good response */
  868. if (unlikely(srp_rsp.srp.generic.type != SRP_RSP_TYPE)) {
  869. if (printk_ratelimit())
  870. printk(KERN_WARNING
  871. "ibmvscsi: reset bad SRP RSP type %d\n",
  872. srp_rsp.srp.generic.type);
  873. return FAILED;
  874. }
  875. if (srp_rsp.srp.rsp.rspvalid)
  876. rsp_rc = *((int *)srp_rsp.srp.rsp.sense_and_response_data);
  877. else
  878. rsp_rc = srp_rsp.srp.rsp.status;
  879. if (rsp_rc) {
  880. if (printk_ratelimit())
  881. printk(KERN_WARNING
  882. "ibmvscsi: reset code %d for task tag 0x%lx\n",
  883. rsp_rc,
  884. tsk_mgmt->managed_task_tag);
  885. return FAILED;
  886. }
  887. /* We need to find all commands for this LUN that have not yet been
  888. * responded to, and fail them with DID_RESET
  889. */
  890. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  891. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  892. if (tmp_evt->cmnd)
  893. tmp_evt->cmnd->result = (DID_RESET << 16);
  894. list_del(&tmp_evt->list);
  895. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt->hostdata->dev);
  896. free_event_struct(&tmp_evt->hostdata->pool,
  897. tmp_evt);
  898. atomic_inc(&hostdata->request_limit);
  899. if (tmp_evt->cmnd_done)
  900. tmp_evt->cmnd_done(tmp_evt->cmnd);
  901. else if (tmp_evt->done)
  902. tmp_evt->done(tmp_evt);
  903. }
  904. }
  905. return SUCCESS;
  906. }
  907. /**
  908. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  909. * @hostdata: the adapter
  910. */
  911. static void purge_requests(struct ibmvscsi_host_data *hostdata)
  912. {
  913. struct srp_event_struct *tmp_evt, *pos;
  914. unsigned long flags;
  915. spin_lock_irqsave(hostdata->host->host_lock, flags);
  916. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  917. list_del(&tmp_evt->list);
  918. if (tmp_evt->cmnd) {
  919. tmp_evt->cmnd->result = (DID_ERROR << 16);
  920. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  921. tmp_evt->hostdata->dev);
  922. if (tmp_evt->cmnd_done)
  923. tmp_evt->cmnd_done(tmp_evt->cmnd);
  924. } else {
  925. if (tmp_evt->done) {
  926. tmp_evt->done(tmp_evt);
  927. }
  928. }
  929. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  930. }
  931. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  932. }
  933. /**
  934. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  935. * @crq: Command/Response queue
  936. * @hostdata: ibmvscsi_host_data of host
  937. *
  938. */
  939. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  940. struct ibmvscsi_host_data *hostdata)
  941. {
  942. unsigned long flags;
  943. struct srp_event_struct *evt_struct =
  944. (struct srp_event_struct *)crq->IU_data_ptr;
  945. switch (crq->valid) {
  946. case 0xC0: /* initialization */
  947. switch (crq->format) {
  948. case 0x01: /* Initialization message */
  949. printk(KERN_INFO "ibmvscsi: partner initialized\n");
  950. /* Send back a response */
  951. if (ibmvscsi_send_crq(hostdata,
  952. 0xC002000000000000LL, 0) == 0) {
  953. /* Now login */
  954. send_srp_login(hostdata);
  955. } else {
  956. printk(KERN_ERR
  957. "ibmvscsi: Unable to send init rsp\n");
  958. }
  959. break;
  960. case 0x02: /* Initialization response */
  961. printk(KERN_INFO
  962. "ibmvscsi: partner initialization complete\n");
  963. /* Now login */
  964. send_srp_login(hostdata);
  965. break;
  966. default:
  967. printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
  968. }
  969. return;
  970. case 0xFF: /* Hypervisor telling us the connection is closed */
  971. printk(KERN_INFO "ibmvscsi: Virtual adapter failed!\n");
  972. atomic_set(&hostdata->request_limit, -1);
  973. purge_requests(hostdata);
  974. ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  975. return;
  976. case 0x80: /* real payload */
  977. break;
  978. default:
  979. printk(KERN_ERR
  980. "ibmvscsi: got an invalid message type 0x%02x\n",
  981. crq->valid);
  982. return;
  983. }
  984. /* The only kind of payload CRQs we should get are responses to
  985. * things we send. Make sure this response is to something we
  986. * actually sent
  987. */
  988. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  989. printk(KERN_ERR
  990. "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
  991. (void *)crq->IU_data_ptr);
  992. return;
  993. }
  994. if (atomic_read(&evt_struct->free)) {
  995. printk(KERN_ERR
  996. "ibmvscsi: received duplicate correlation_token 0x%p!\n",
  997. (void *)crq->IU_data_ptr);
  998. return;
  999. }
  1000. if (crq->format == VIOSRP_SRP_FORMAT)
  1001. atomic_add(evt_struct->xfer_iu->srp.rsp.request_limit_delta,
  1002. &hostdata->request_limit);
  1003. if (evt_struct->done)
  1004. evt_struct->done(evt_struct);
  1005. else
  1006. printk(KERN_ERR
  1007. "ibmvscsi: returned done() is NULL; not running it!\n");
  1008. /*
  1009. * Lock the host_lock before messing with these structures, since we
  1010. * are running in a task context
  1011. */
  1012. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1013. list_del(&evt_struct->list);
  1014. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1015. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1016. }
  1017. /**
  1018. * ibmvscsi_get_host_config: Send the command to the server to get host
  1019. * configuration data. The data is opaque to us.
  1020. */
  1021. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1022. unsigned char *buffer, int length)
  1023. {
  1024. struct viosrp_host_config *host_config;
  1025. struct srp_event_struct *evt_struct;
  1026. int rc;
  1027. evt_struct = get_event_struct(&hostdata->pool);
  1028. if (!evt_struct) {
  1029. printk(KERN_ERR
  1030. "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
  1031. return -1;
  1032. }
  1033. init_event_struct(evt_struct,
  1034. sync_completion,
  1035. VIOSRP_MAD_FORMAT,
  1036. init_timeout * HZ);
  1037. host_config = &evt_struct->iu.mad.host_config;
  1038. /* Set up a lun reset SRP command */
  1039. memset(host_config, 0x00, sizeof(*host_config));
  1040. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1041. host_config->common.length = length;
  1042. host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
  1043. DMA_BIDIRECTIONAL);
  1044. if (dma_mapping_error(host_config->buffer)) {
  1045. printk(KERN_ERR
  1046. "ibmvscsi: dma_mapping error " "getting host config\n");
  1047. free_event_struct(&hostdata->pool, evt_struct);
  1048. return -1;
  1049. }
  1050. init_completion(&evt_struct->comp);
  1051. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  1052. if (rc == 0) {
  1053. wait_for_completion(&evt_struct->comp);
  1054. dma_unmap_single(hostdata->dev, host_config->buffer,
  1055. length, DMA_BIDIRECTIONAL);
  1056. }
  1057. return rc;
  1058. }
  1059. /* ------------------------------------------------------------
  1060. * sysfs attributes
  1061. */
  1062. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1063. {
  1064. struct Scsi_Host *shost = class_to_shost(class_dev);
  1065. struct ibmvscsi_host_data *hostdata =
  1066. (struct ibmvscsi_host_data *)shost->hostdata;
  1067. int len;
  1068. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1069. hostdata->madapter_info.srp_version);
  1070. return len;
  1071. }
  1072. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1073. .attr = {
  1074. .name = "srp_version",
  1075. .mode = S_IRUGO,
  1076. },
  1077. .show = show_host_srp_version,
  1078. };
  1079. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1080. char *buf)
  1081. {
  1082. struct Scsi_Host *shost = class_to_shost(class_dev);
  1083. struct ibmvscsi_host_data *hostdata =
  1084. (struct ibmvscsi_host_data *)shost->hostdata;
  1085. int len;
  1086. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1087. hostdata->madapter_info.partition_name);
  1088. return len;
  1089. }
  1090. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1091. .attr = {
  1092. .name = "partition_name",
  1093. .mode = S_IRUGO,
  1094. },
  1095. .show = show_host_partition_name,
  1096. };
  1097. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1098. char *buf)
  1099. {
  1100. struct Scsi_Host *shost = class_to_shost(class_dev);
  1101. struct ibmvscsi_host_data *hostdata =
  1102. (struct ibmvscsi_host_data *)shost->hostdata;
  1103. int len;
  1104. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1105. hostdata->madapter_info.partition_number);
  1106. return len;
  1107. }
  1108. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1109. .attr = {
  1110. .name = "partition_number",
  1111. .mode = S_IRUGO,
  1112. },
  1113. .show = show_host_partition_number,
  1114. };
  1115. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1116. {
  1117. struct Scsi_Host *shost = class_to_shost(class_dev);
  1118. struct ibmvscsi_host_data *hostdata =
  1119. (struct ibmvscsi_host_data *)shost->hostdata;
  1120. int len;
  1121. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1122. hostdata->madapter_info.mad_version);
  1123. return len;
  1124. }
  1125. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1126. .attr = {
  1127. .name = "mad_version",
  1128. .mode = S_IRUGO,
  1129. },
  1130. .show = show_host_mad_version,
  1131. };
  1132. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1133. {
  1134. struct Scsi_Host *shost = class_to_shost(class_dev);
  1135. struct ibmvscsi_host_data *hostdata =
  1136. (struct ibmvscsi_host_data *)shost->hostdata;
  1137. int len;
  1138. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1139. return len;
  1140. }
  1141. static struct class_device_attribute ibmvscsi_host_os_type = {
  1142. .attr = {
  1143. .name = "os_type",
  1144. .mode = S_IRUGO,
  1145. },
  1146. .show = show_host_os_type,
  1147. };
  1148. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1149. {
  1150. struct Scsi_Host *shost = class_to_shost(class_dev);
  1151. struct ibmvscsi_host_data *hostdata =
  1152. (struct ibmvscsi_host_data *)shost->hostdata;
  1153. /* returns null-terminated host config data */
  1154. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1155. return strlen(buf);
  1156. else
  1157. return 0;
  1158. }
  1159. static struct class_device_attribute ibmvscsi_host_config = {
  1160. .attr = {
  1161. .name = "config",
  1162. .mode = S_IRUGO,
  1163. },
  1164. .show = show_host_config,
  1165. };
  1166. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1167. &ibmvscsi_host_srp_version,
  1168. &ibmvscsi_host_partition_name,
  1169. &ibmvscsi_host_partition_number,
  1170. &ibmvscsi_host_mad_version,
  1171. &ibmvscsi_host_os_type,
  1172. &ibmvscsi_host_config,
  1173. NULL
  1174. };
  1175. /* ------------------------------------------------------------
  1176. * SCSI driver registration
  1177. */
  1178. static struct scsi_host_template driver_template = {
  1179. .module = THIS_MODULE,
  1180. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1181. .proc_name = "ibmvscsi",
  1182. .queuecommand = ibmvscsi_queuecommand,
  1183. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1184. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1185. .cmd_per_lun = 16,
  1186. .can_queue = 1, /* Updated after SRP_LOGIN */
  1187. .this_id = -1,
  1188. .sg_tablesize = MAX_INDIRECT_BUFS,
  1189. .use_clustering = ENABLE_CLUSTERING,
  1190. .shost_attrs = ibmvscsi_attrs,
  1191. };
  1192. /**
  1193. * Called by bus code for each adapter
  1194. */
  1195. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1196. {
  1197. struct ibmvscsi_host_data *hostdata;
  1198. struct Scsi_Host *host;
  1199. struct device *dev = &vdev->dev;
  1200. unsigned long wait_switch = 0;
  1201. vdev->dev.driver_data = NULL;
  1202. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1203. if (!host) {
  1204. printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
  1205. goto scsi_host_alloc_failed;
  1206. }
  1207. hostdata = (struct ibmvscsi_host_data *)host->hostdata;
  1208. memset(hostdata, 0x00, sizeof(*hostdata));
  1209. INIT_LIST_HEAD(&hostdata->sent);
  1210. hostdata->host = host;
  1211. hostdata->dev = dev;
  1212. atomic_set(&hostdata->request_limit, -1);
  1213. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1214. if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
  1215. max_requests) != 0) {
  1216. printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
  1217. goto init_crq_failed;
  1218. }
  1219. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1220. printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
  1221. goto init_pool_failed;
  1222. }
  1223. host->max_lun = 8;
  1224. host->max_id = max_id;
  1225. host->max_channel = max_channel;
  1226. if (scsi_add_host(hostdata->host, hostdata->dev))
  1227. goto add_host_failed;
  1228. /* Try to send an initialization message. Note that this is allowed
  1229. * to fail if the other end is not acive. In that case we don't
  1230. * want to scan
  1231. */
  1232. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
  1233. /*
  1234. * Wait around max init_timeout secs for the adapter to finish
  1235. * initializing. When we are done initializing, we will have a
  1236. * valid request_limit. We don't want Linux scanning before
  1237. * we are ready.
  1238. */
  1239. for (wait_switch = jiffies + (init_timeout * HZ);
  1240. time_before(jiffies, wait_switch) &&
  1241. atomic_read(&hostdata->request_limit) < 2;) {
  1242. msleep(10);
  1243. }
  1244. /* if we now have a valid request_limit, initiate a scan */
  1245. if (atomic_read(&hostdata->request_limit) > 0)
  1246. scsi_scan_host(host);
  1247. }
  1248. vdev->dev.driver_data = hostdata;
  1249. return 0;
  1250. add_host_failed:
  1251. release_event_pool(&hostdata->pool, hostdata);
  1252. init_pool_failed:
  1253. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1254. init_crq_failed:
  1255. scsi_host_put(host);
  1256. scsi_host_alloc_failed:
  1257. return -1;
  1258. }
  1259. static int ibmvscsi_remove(struct vio_dev *vdev)
  1260. {
  1261. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1262. release_event_pool(&hostdata->pool, hostdata);
  1263. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  1264. max_requests);
  1265. scsi_remove_host(hostdata->host);
  1266. scsi_host_put(hostdata->host);
  1267. return 0;
  1268. }
  1269. /**
  1270. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1271. * support.
  1272. */
  1273. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1274. {"vscsi", "IBM,v-scsi"},
  1275. {0,}
  1276. };
  1277. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1278. static struct vio_driver ibmvscsi_driver = {
  1279. .name = "ibmvscsi",
  1280. .id_table = ibmvscsi_device_table,
  1281. .probe = ibmvscsi_probe,
  1282. .remove = ibmvscsi_remove
  1283. };
  1284. int __init ibmvscsi_module_init(void)
  1285. {
  1286. return vio_register_driver(&ibmvscsi_driver);
  1287. }
  1288. void __exit ibmvscsi_module_exit(void)
  1289. {
  1290. vio_unregister_driver(&ibmvscsi_driver);
  1291. }
  1292. module_init(ibmvscsi_module_init);
  1293. module_exit(ibmvscsi_module_exit);