ibmvscsi.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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-robin 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. * TODO: This is currently pretty tied to the IBM pSeries hypervisor
  59. * interfaces. It would be really nice to abstract this above an RDMA
  60. * layer.
  61. */
  62. #include <linux/module.h>
  63. #include <linux/moduleparam.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/delay.h>
  66. #include <linux/slab.h>
  67. #include <linux/of.h>
  68. #include <linux/pm.h>
  69. #include <linux/kthread.h>
  70. #include <asm/firmware.h>
  71. #include <asm/vio.h>
  72. #include <scsi/scsi.h>
  73. #include <scsi/scsi_cmnd.h>
  74. #include <scsi/scsi_host.h>
  75. #include <scsi/scsi_device.h>
  76. #include <scsi/scsi_transport_srp.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 = 300;
  86. static int login_timeout = 60;
  87. static int info_timeout = 30;
  88. static int abort_timeout = 60;
  89. static int reset_timeout = 60;
  90. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  91. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  92. static int fast_fail = 1;
  93. static int client_reserve = 1;
  94. static char partition_name[96] = "UNKNOWN";
  95. static unsigned int partition_number = -1;
  96. static LIST_HEAD(ibmvscsi_head);
  97. static DEFINE_SPINLOCK(ibmvscsi_driver_lock);
  98. static struct scsi_transport_template *ibmvscsi_transport_template;
  99. #define IBMVSCSI_VERSION "1.5.9"
  100. MODULE_DESCRIPTION("IBM Virtual SCSI");
  101. MODULE_AUTHOR("Dave Boutcher");
  102. MODULE_LICENSE("GPL");
  103. MODULE_VERSION(IBMVSCSI_VERSION);
  104. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(max_id, "Largest ID value for each channel [Default=64]");
  106. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(max_channel, "Largest channel value [Default=3]");
  108. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  109. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  110. module_param_named(max_requests, max_requests, int, S_IRUGO);
  111. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  112. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  113. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  114. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  115. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  116. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  117. struct ibmvscsi_host_data *hostdata);
  118. /* ------------------------------------------------------------
  119. * Routines for managing the command/response queue
  120. */
  121. /**
  122. * ibmvscsi_handle_event: - Interrupt handler for crq events
  123. * @irq: number of irq to handle, not used
  124. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  125. *
  126. * Disables interrupts and schedules srp_task
  127. * Always returns IRQ_HANDLED
  128. */
  129. static irqreturn_t ibmvscsi_handle_event(int irq, void *dev_instance)
  130. {
  131. struct ibmvscsi_host_data *hostdata =
  132. (struct ibmvscsi_host_data *)dev_instance;
  133. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  134. tasklet_schedule(&hostdata->srp_task);
  135. return IRQ_HANDLED;
  136. }
  137. /**
  138. * release_crq_queue: - Deallocates data and unregisters CRQ
  139. * @queue: crq_queue to initialize and register
  140. * @host_data: ibmvscsi_host_data of host
  141. *
  142. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  143. * the crq with the hypervisor.
  144. */
  145. static void ibmvscsi_release_crq_queue(struct crq_queue *queue,
  146. struct ibmvscsi_host_data *hostdata,
  147. int max_requests)
  148. {
  149. long rc = 0;
  150. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  151. free_irq(vdev->irq, (void *)hostdata);
  152. tasklet_kill(&hostdata->srp_task);
  153. do {
  154. if (rc)
  155. msleep(100);
  156. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  157. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  158. dma_unmap_single(hostdata->dev,
  159. queue->msg_token,
  160. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  161. free_page((unsigned long)queue->msgs);
  162. }
  163. /**
  164. * crq_queue_next_crq: - Returns the next entry in message queue
  165. * @queue: crq_queue to use
  166. *
  167. * Returns pointer to next entry in queue, or NULL if there are no new
  168. * entried in the CRQ.
  169. */
  170. static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
  171. {
  172. struct viosrp_crq *crq;
  173. unsigned long flags;
  174. spin_lock_irqsave(&queue->lock, flags);
  175. crq = &queue->msgs[queue->cur];
  176. if (crq->valid != VIOSRP_CRQ_FREE) {
  177. if (++queue->cur == queue->size)
  178. queue->cur = 0;
  179. /* Ensure the read of the valid bit occurs before reading any
  180. * other bits of the CRQ entry
  181. */
  182. rmb();
  183. } else
  184. crq = NULL;
  185. spin_unlock_irqrestore(&queue->lock, flags);
  186. return crq;
  187. }
  188. /**
  189. * ibmvscsi_send_crq: - Send a CRQ
  190. * @hostdata: the adapter
  191. * @word1: the first 64 bits of the data
  192. * @word2: the second 64 bits of the data
  193. */
  194. static int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata,
  195. u64 word1, u64 word2)
  196. {
  197. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  198. /*
  199. * Ensure the command buffer is flushed to memory before handing it
  200. * over to the VIOS to prevent it from fetching any stale data.
  201. */
  202. mb();
  203. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  204. }
  205. /**
  206. * ibmvscsi_task: - Process srps asynchronously
  207. * @data: ibmvscsi_host_data of host
  208. */
  209. static void ibmvscsi_task(void *data)
  210. {
  211. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  212. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  213. struct viosrp_crq *crq;
  214. int done = 0;
  215. while (!done) {
  216. /* Pull all the valid messages off the CRQ */
  217. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  218. ibmvscsi_handle_crq(crq, hostdata);
  219. crq->valid = VIOSRP_CRQ_FREE;
  220. wmb();
  221. }
  222. vio_enable_interrupts(vdev);
  223. crq = crq_queue_next_crq(&hostdata->queue);
  224. if (crq != NULL) {
  225. vio_disable_interrupts(vdev);
  226. ibmvscsi_handle_crq(crq, hostdata);
  227. crq->valid = VIOSRP_CRQ_FREE;
  228. wmb();
  229. } else {
  230. done = 1;
  231. }
  232. }
  233. }
  234. static void gather_partition_info(void)
  235. {
  236. const char *ppartition_name;
  237. const __be32 *p_number_ptr;
  238. /* Retrieve information about this partition */
  239. if (!of_root)
  240. return;
  241. of_node_get(of_root);
  242. ppartition_name = of_get_property(of_root, "ibm,partition-name", NULL);
  243. if (ppartition_name)
  244. strlcpy(partition_name, ppartition_name,
  245. sizeof(partition_name));
  246. p_number_ptr = of_get_property(of_root, "ibm,partition-no", NULL);
  247. if (p_number_ptr)
  248. partition_number = of_read_number(p_number_ptr, 1);
  249. of_node_put(of_root);
  250. }
  251. static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
  252. {
  253. memset(&hostdata->madapter_info, 0x00,
  254. sizeof(hostdata->madapter_info));
  255. dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION);
  256. strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
  257. strncpy(hostdata->madapter_info.partition_name, partition_name,
  258. sizeof(hostdata->madapter_info.partition_name));
  259. hostdata->madapter_info.partition_number =
  260. cpu_to_be32(partition_number);
  261. hostdata->madapter_info.mad_version = cpu_to_be32(SRP_MAD_VERSION_1);
  262. hostdata->madapter_info.os_type = cpu_to_be32(SRP_MAD_OS_LINUX);
  263. }
  264. /**
  265. * reset_crq_queue: - resets a crq after a failure
  266. * @queue: crq_queue to initialize and register
  267. * @hostdata: ibmvscsi_host_data of host
  268. *
  269. */
  270. static int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
  271. struct ibmvscsi_host_data *hostdata)
  272. {
  273. int rc = 0;
  274. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  275. /* Close the CRQ */
  276. do {
  277. if (rc)
  278. msleep(100);
  279. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  280. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  281. /* Clean out the queue */
  282. memset(queue->msgs, 0x00, PAGE_SIZE);
  283. queue->cur = 0;
  284. set_adapter_info(hostdata);
  285. /* And re-open it again */
  286. rc = plpar_hcall_norets(H_REG_CRQ,
  287. vdev->unit_address,
  288. queue->msg_token, PAGE_SIZE);
  289. if (rc == H_CLOSED) {
  290. /* Adapter is good, but other end is not ready */
  291. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  292. } else if (rc != 0) {
  293. dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
  294. }
  295. return rc;
  296. }
  297. /**
  298. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  299. * @queue: crq_queue to initialize and register
  300. * @hostdata: ibmvscsi_host_data of host
  301. *
  302. * Allocates a page for messages, maps it for dma, and registers
  303. * the crq with the hypervisor.
  304. * Returns zero on success.
  305. */
  306. static int ibmvscsi_init_crq_queue(struct crq_queue *queue,
  307. struct ibmvscsi_host_data *hostdata,
  308. int max_requests)
  309. {
  310. int rc;
  311. int retrc;
  312. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  313. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  314. if (!queue->msgs)
  315. goto malloc_failed;
  316. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  317. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  318. queue->size * sizeof(*queue->msgs),
  319. DMA_BIDIRECTIONAL);
  320. if (dma_mapping_error(hostdata->dev, queue->msg_token))
  321. goto map_failed;
  322. gather_partition_info();
  323. set_adapter_info(hostdata);
  324. retrc = rc = plpar_hcall_norets(H_REG_CRQ,
  325. vdev->unit_address,
  326. queue->msg_token, PAGE_SIZE);
  327. if (rc == H_RESOURCE)
  328. /* maybe kexecing and resource is busy. try a reset */
  329. rc = ibmvscsi_reset_crq_queue(queue,
  330. hostdata);
  331. if (rc == H_CLOSED) {
  332. /* Adapter is good, but other end is not ready */
  333. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  334. retrc = 0;
  335. } else if (rc != 0) {
  336. dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
  337. goto reg_crq_failed;
  338. }
  339. queue->cur = 0;
  340. spin_lock_init(&queue->lock);
  341. tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
  342. (unsigned long)hostdata);
  343. if (request_irq(vdev->irq,
  344. ibmvscsi_handle_event,
  345. 0, "ibmvscsi", (void *)hostdata) != 0) {
  346. dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
  347. vdev->irq);
  348. goto req_irq_failed;
  349. }
  350. rc = vio_enable_interrupts(vdev);
  351. if (rc != 0) {
  352. dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
  353. goto req_irq_failed;
  354. }
  355. return retrc;
  356. req_irq_failed:
  357. tasklet_kill(&hostdata->srp_task);
  358. rc = 0;
  359. do {
  360. if (rc)
  361. msleep(100);
  362. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  363. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  364. reg_crq_failed:
  365. dma_unmap_single(hostdata->dev,
  366. queue->msg_token,
  367. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  368. map_failed:
  369. free_page((unsigned long)queue->msgs);
  370. malloc_failed:
  371. return -1;
  372. }
  373. /**
  374. * reenable_crq_queue: - reenables a crq after
  375. * @queue: crq_queue to initialize and register
  376. * @hostdata: ibmvscsi_host_data of host
  377. *
  378. */
  379. static int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
  380. struct ibmvscsi_host_data *hostdata)
  381. {
  382. int rc = 0;
  383. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  384. /* Re-enable the CRQ */
  385. do {
  386. if (rc)
  387. msleep(100);
  388. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  389. } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  390. if (rc)
  391. dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
  392. return rc;
  393. }
  394. /* ------------------------------------------------------------
  395. * Routines for the event pool and event structs
  396. */
  397. /**
  398. * initialize_event_pool: - Allocates and initializes the event pool for a host
  399. * @pool: event_pool to be initialized
  400. * @size: Number of events in pool
  401. * @hostdata: ibmvscsi_host_data who owns the event pool
  402. *
  403. * Returns zero on success.
  404. */
  405. static int initialize_event_pool(struct event_pool *pool,
  406. int size, struct ibmvscsi_host_data *hostdata)
  407. {
  408. int i;
  409. pool->size = size;
  410. pool->next = 0;
  411. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  412. if (!pool->events)
  413. return -ENOMEM;
  414. pool->iu_storage =
  415. dma_alloc_coherent(hostdata->dev,
  416. pool->size * sizeof(*pool->iu_storage),
  417. &pool->iu_token, 0);
  418. if (!pool->iu_storage) {
  419. kfree(pool->events);
  420. return -ENOMEM;
  421. }
  422. for (i = 0; i < pool->size; ++i) {
  423. struct srp_event_struct *evt = &pool->events[i];
  424. memset(&evt->crq, 0x00, sizeof(evt->crq));
  425. atomic_set(&evt->free, 1);
  426. evt->crq.valid = VIOSRP_CRQ_CMD_RSP;
  427. evt->crq.IU_length = cpu_to_be16(sizeof(*evt->xfer_iu));
  428. evt->crq.IU_data_ptr = cpu_to_be64(pool->iu_token +
  429. sizeof(*evt->xfer_iu) * i);
  430. evt->xfer_iu = pool->iu_storage + i;
  431. evt->hostdata = hostdata;
  432. evt->ext_list = NULL;
  433. evt->ext_list_token = 0;
  434. }
  435. return 0;
  436. }
  437. /**
  438. * release_event_pool: - Frees memory of an event pool of a host
  439. * @pool: event_pool to be released
  440. * @hostdata: ibmvscsi_host_data who owns the even pool
  441. *
  442. * Returns zero on success.
  443. */
  444. static void release_event_pool(struct event_pool *pool,
  445. struct ibmvscsi_host_data *hostdata)
  446. {
  447. int i, in_use = 0;
  448. for (i = 0; i < pool->size; ++i) {
  449. if (atomic_read(&pool->events[i].free) != 1)
  450. ++in_use;
  451. if (pool->events[i].ext_list) {
  452. dma_free_coherent(hostdata->dev,
  453. SG_ALL * sizeof(struct srp_direct_buf),
  454. pool->events[i].ext_list,
  455. pool->events[i].ext_list_token);
  456. }
  457. }
  458. if (in_use)
  459. dev_warn(hostdata->dev, "releasing event pool with %d "
  460. "events still in use?\n", in_use);
  461. kfree(pool->events);
  462. dma_free_coherent(hostdata->dev,
  463. pool->size * sizeof(*pool->iu_storage),
  464. pool->iu_storage, pool->iu_token);
  465. }
  466. /**
  467. * valid_event_struct: - Determines if event is valid.
  468. * @pool: event_pool that contains the event
  469. * @evt: srp_event_struct to be checked for validity
  470. *
  471. * Returns zero if event is invalid, one otherwise.
  472. */
  473. static int valid_event_struct(struct event_pool *pool,
  474. struct srp_event_struct *evt)
  475. {
  476. int index = evt - pool->events;
  477. if (index < 0 || index >= pool->size) /* outside of bounds */
  478. return 0;
  479. if (evt != pool->events + index) /* unaligned */
  480. return 0;
  481. return 1;
  482. }
  483. /**
  484. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  485. * @pool: event_pool that contains the event
  486. * @evt: srp_event_struct to be modified
  487. *
  488. */
  489. static void free_event_struct(struct event_pool *pool,
  490. struct srp_event_struct *evt)
  491. {
  492. if (!valid_event_struct(pool, evt)) {
  493. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  494. "(not in pool %p)\n", evt, pool->events);
  495. return;
  496. }
  497. if (atomic_inc_return(&evt->free) != 1) {
  498. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  499. "which is not in use!\n", evt);
  500. return;
  501. }
  502. }
  503. /**
  504. * get_evt_struct: - Gets the next free event in pool
  505. * @pool: event_pool that contains the events to be searched
  506. *
  507. * Returns the next event in "free" state, and NULL if none are free.
  508. * Note that no synchronization is done here, we assume the host_lock
  509. * will syncrhonze things.
  510. */
  511. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  512. {
  513. int i;
  514. int poolsize = pool->size;
  515. int offset = pool->next;
  516. for (i = 0; i < poolsize; i++) {
  517. offset = (offset + 1) % poolsize;
  518. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  519. pool->next = offset;
  520. return &pool->events[offset];
  521. }
  522. }
  523. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  524. return NULL;
  525. }
  526. /**
  527. * init_event_struct: Initialize fields in an event struct that are always
  528. * required.
  529. * @evt: The event
  530. * @done: Routine to call when the event is responded to
  531. * @format: SRP or MAD format
  532. * @timeout: timeout value set in the CRQ
  533. */
  534. static void init_event_struct(struct srp_event_struct *evt_struct,
  535. void (*done) (struct srp_event_struct *),
  536. u8 format,
  537. int timeout)
  538. {
  539. evt_struct->cmnd = NULL;
  540. evt_struct->cmnd_done = NULL;
  541. evt_struct->sync_srp = NULL;
  542. evt_struct->crq.format = format;
  543. evt_struct->crq.timeout = cpu_to_be16(timeout);
  544. evt_struct->done = done;
  545. }
  546. /* ------------------------------------------------------------
  547. * Routines for receiving SCSI responses from the hosting partition
  548. */
  549. /**
  550. * set_srp_direction: Set the fields in the srp related to data
  551. * direction and number of buffers based on the direction in
  552. * the scsi_cmnd and the number of buffers
  553. */
  554. static void set_srp_direction(struct scsi_cmnd *cmd,
  555. struct srp_cmd *srp_cmd,
  556. int numbuf)
  557. {
  558. u8 fmt;
  559. if (numbuf == 0)
  560. return;
  561. if (numbuf == 1)
  562. fmt = SRP_DATA_DESC_DIRECT;
  563. else {
  564. fmt = SRP_DATA_DESC_INDIRECT;
  565. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  566. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  567. srp_cmd->data_out_desc_cnt = numbuf;
  568. else
  569. srp_cmd->data_in_desc_cnt = numbuf;
  570. }
  571. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  572. srp_cmd->buf_fmt = fmt << 4;
  573. else
  574. srp_cmd->buf_fmt = fmt;
  575. }
  576. /**
  577. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  578. * @cmd: srp_cmd whose additional_data member will be unmapped
  579. * @dev: device for which the memory is mapped
  580. *
  581. */
  582. static void unmap_cmd_data(struct srp_cmd *cmd,
  583. struct srp_event_struct *evt_struct,
  584. struct device *dev)
  585. {
  586. u8 out_fmt, in_fmt;
  587. out_fmt = cmd->buf_fmt >> 4;
  588. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  589. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  590. return;
  591. if (evt_struct->cmnd)
  592. scsi_dma_unmap(evt_struct->cmnd);
  593. }
  594. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  595. struct srp_direct_buf *md)
  596. {
  597. int i;
  598. struct scatterlist *sg;
  599. u64 total_length = 0;
  600. scsi_for_each_sg(cmd, sg, nseg, i) {
  601. struct srp_direct_buf *descr = md + i;
  602. descr->va = cpu_to_be64(sg_dma_address(sg));
  603. descr->len = cpu_to_be32(sg_dma_len(sg));
  604. descr->key = 0;
  605. total_length += sg_dma_len(sg);
  606. }
  607. return total_length;
  608. }
  609. /**
  610. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  611. * @cmd: struct scsi_cmnd with the scatterlist
  612. * @srp_cmd: srp_cmd that contains the memory descriptor
  613. * @dev: device for which to map dma memory
  614. *
  615. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  616. * Returns 1 on success.
  617. */
  618. static int map_sg_data(struct scsi_cmnd *cmd,
  619. struct srp_event_struct *evt_struct,
  620. struct srp_cmd *srp_cmd, struct device *dev)
  621. {
  622. int sg_mapped;
  623. u64 total_length = 0;
  624. struct srp_direct_buf *data =
  625. (struct srp_direct_buf *) srp_cmd->add_data;
  626. struct srp_indirect_buf *indirect =
  627. (struct srp_indirect_buf *) data;
  628. sg_mapped = scsi_dma_map(cmd);
  629. if (!sg_mapped)
  630. return 1;
  631. else if (sg_mapped < 0)
  632. return 0;
  633. set_srp_direction(cmd, srp_cmd, sg_mapped);
  634. /* special case; we can use a single direct descriptor */
  635. if (sg_mapped == 1) {
  636. map_sg_list(cmd, sg_mapped, data);
  637. return 1;
  638. }
  639. indirect->table_desc.va = 0;
  640. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  641. sizeof(struct srp_direct_buf));
  642. indirect->table_desc.key = 0;
  643. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  644. total_length = map_sg_list(cmd, sg_mapped,
  645. &indirect->desc_list[0]);
  646. indirect->len = cpu_to_be32(total_length);
  647. return 1;
  648. }
  649. /* get indirect table */
  650. if (!evt_struct->ext_list) {
  651. evt_struct->ext_list = (struct srp_direct_buf *)
  652. dma_alloc_coherent(dev,
  653. SG_ALL * sizeof(struct srp_direct_buf),
  654. &evt_struct->ext_list_token, 0);
  655. if (!evt_struct->ext_list) {
  656. if (!firmware_has_feature(FW_FEATURE_CMO))
  657. sdev_printk(KERN_ERR, cmd->device,
  658. "Can't allocate memory "
  659. "for indirect table\n");
  660. scsi_dma_unmap(cmd);
  661. return 0;
  662. }
  663. }
  664. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  665. indirect->len = cpu_to_be32(total_length);
  666. indirect->table_desc.va = cpu_to_be64(evt_struct->ext_list_token);
  667. indirect->table_desc.len = cpu_to_be32(sg_mapped *
  668. sizeof(indirect->desc_list[0]));
  669. memcpy(indirect->desc_list, evt_struct->ext_list,
  670. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  671. return 1;
  672. }
  673. /**
  674. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  675. * @cmd: struct scsi_cmnd with the memory to be mapped
  676. * @srp_cmd: srp_cmd that contains the memory descriptor
  677. * @dev: dma device for which to map dma memory
  678. *
  679. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  680. * Returns 1 on success.
  681. */
  682. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  683. struct srp_event_struct *evt_struct,
  684. struct srp_cmd *srp_cmd, struct device *dev)
  685. {
  686. switch (cmd->sc_data_direction) {
  687. case DMA_FROM_DEVICE:
  688. case DMA_TO_DEVICE:
  689. break;
  690. case DMA_NONE:
  691. return 1;
  692. case DMA_BIDIRECTIONAL:
  693. sdev_printk(KERN_ERR, cmd->device,
  694. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  695. return 0;
  696. default:
  697. sdev_printk(KERN_ERR, cmd->device,
  698. "Unknown data direction 0x%02x; can't map!\n",
  699. cmd->sc_data_direction);
  700. return 0;
  701. }
  702. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  703. }
  704. /**
  705. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  706. * @hostdata: the adapter
  707. */
  708. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  709. {
  710. struct srp_event_struct *evt;
  711. unsigned long flags;
  712. spin_lock_irqsave(hostdata->host->host_lock, flags);
  713. while (!list_empty(&hostdata->sent)) {
  714. evt = list_first_entry(&hostdata->sent, struct srp_event_struct, list);
  715. list_del(&evt->list);
  716. del_timer(&evt->timer);
  717. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  718. if (evt->cmnd) {
  719. evt->cmnd->result = (error_code << 16);
  720. unmap_cmd_data(&evt->iu.srp.cmd, evt,
  721. evt->hostdata->dev);
  722. if (evt->cmnd_done)
  723. evt->cmnd_done(evt->cmnd);
  724. } else if (evt->done && evt->crq.format != VIOSRP_MAD_FORMAT &&
  725. evt->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  726. evt->done(evt);
  727. free_event_struct(&evt->hostdata->pool, evt);
  728. spin_lock_irqsave(hostdata->host->host_lock, flags);
  729. }
  730. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  731. }
  732. /**
  733. * ibmvscsi_reset_host - Reset the connection to the server
  734. * @hostdata: struct ibmvscsi_host_data to reset
  735. */
  736. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  737. {
  738. scsi_block_requests(hostdata->host);
  739. atomic_set(&hostdata->request_limit, 0);
  740. purge_requests(hostdata, DID_ERROR);
  741. hostdata->reset_crq = 1;
  742. wake_up(&hostdata->work_wait_q);
  743. }
  744. /**
  745. * ibmvscsi_timeout - Internal command timeout handler
  746. * @evt_struct: struct srp_event_struct that timed out
  747. *
  748. * Called when an internally generated command times out
  749. */
  750. static void ibmvscsi_timeout(struct timer_list *t)
  751. {
  752. struct srp_event_struct *evt_struct = from_timer(evt_struct, t, timer);
  753. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  754. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  755. evt_struct->iu.srp.cmd.opcode);
  756. ibmvscsi_reset_host(hostdata);
  757. }
  758. /* ------------------------------------------------------------
  759. * Routines for sending and receiving SRPs
  760. */
  761. /**
  762. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  763. * @evt_struct: evt_struct to be sent
  764. * @hostdata: ibmvscsi_host_data of host
  765. * @timeout: timeout in seconds - 0 means do not time command
  766. *
  767. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  768. * Note that this routine assumes that host_lock is held for synchronization
  769. */
  770. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  771. struct ibmvscsi_host_data *hostdata,
  772. unsigned long timeout)
  773. {
  774. __be64 *crq_as_u64 = (__be64 *)&evt_struct->crq;
  775. int request_status = 0;
  776. int rc;
  777. int srp_req = 0;
  778. /* If we have exhausted our request limit, just fail this request,
  779. * unless it is for a reset or abort.
  780. * Note that there are rare cases involving driver generated requests
  781. * (such as task management requests) that the mid layer may think we
  782. * can handle more requests (can_queue) when we actually can't
  783. */
  784. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  785. srp_req = 1;
  786. request_status =
  787. atomic_dec_if_positive(&hostdata->request_limit);
  788. /* If request limit was -1 when we started, it is now even
  789. * less than that
  790. */
  791. if (request_status < -1)
  792. goto send_error;
  793. /* Otherwise, we may have run out of requests. */
  794. /* If request limit was 0 when we started the adapter is in the
  795. * process of performing a login with the server adapter, or
  796. * we may have run out of requests.
  797. */
  798. else if (request_status == -1 &&
  799. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  800. goto send_busy;
  801. /* Abort and reset calls should make it through.
  802. * Nothing except abort and reset should use the last two
  803. * slots unless we had two or less to begin with.
  804. */
  805. else if (request_status < 2 &&
  806. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  807. /* In the case that we have less than two requests
  808. * available, check the server limit as a combination
  809. * of the request limit and the number of requests
  810. * in-flight (the size of the send list). If the
  811. * server limit is greater than 2, return busy so
  812. * that the last two are reserved for reset and abort.
  813. */
  814. int server_limit = request_status;
  815. struct srp_event_struct *tmp_evt;
  816. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  817. server_limit++;
  818. }
  819. if (server_limit > 2)
  820. goto send_busy;
  821. }
  822. }
  823. /* Copy the IU into the transfer area */
  824. *evt_struct->xfer_iu = evt_struct->iu;
  825. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  826. /* Add this to the sent list. We need to do this
  827. * before we actually send
  828. * in case it comes back REALLY fast
  829. */
  830. list_add_tail(&evt_struct->list, &hostdata->sent);
  831. timer_setup(&evt_struct->timer, ibmvscsi_timeout, 0);
  832. if (timeout) {
  833. evt_struct->timer.expires = jiffies + (timeout * HZ);
  834. add_timer(&evt_struct->timer);
  835. }
  836. rc = ibmvscsi_send_crq(hostdata, be64_to_cpu(crq_as_u64[0]),
  837. be64_to_cpu(crq_as_u64[1]));
  838. if (rc != 0) {
  839. list_del(&evt_struct->list);
  840. del_timer(&evt_struct->timer);
  841. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  842. * Firmware will send a CRQ with a transport event (0xFF) to
  843. * tell this client what has happened to the transport. This
  844. * will be handled in ibmvscsi_handle_crq()
  845. */
  846. if (rc == H_CLOSED) {
  847. dev_warn(hostdata->dev, "send warning. "
  848. "Receive queue closed, will retry.\n");
  849. goto send_busy;
  850. }
  851. dev_err(hostdata->dev, "send error %d\n", rc);
  852. if (srp_req)
  853. atomic_inc(&hostdata->request_limit);
  854. goto send_error;
  855. }
  856. return 0;
  857. send_busy:
  858. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  859. free_event_struct(&hostdata->pool, evt_struct);
  860. if (srp_req && request_status != -1)
  861. atomic_inc(&hostdata->request_limit);
  862. return SCSI_MLQUEUE_HOST_BUSY;
  863. send_error:
  864. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  865. if (evt_struct->cmnd != NULL) {
  866. evt_struct->cmnd->result = DID_ERROR << 16;
  867. evt_struct->cmnd_done(evt_struct->cmnd);
  868. } else if (evt_struct->done)
  869. evt_struct->done(evt_struct);
  870. free_event_struct(&hostdata->pool, evt_struct);
  871. return 0;
  872. }
  873. /**
  874. * handle_cmd_rsp: - Handle responses from commands
  875. * @evt_struct: srp_event_struct to be handled
  876. *
  877. * Used as a callback by when sending scsi cmds.
  878. * Gets called by ibmvscsi_handle_crq()
  879. */
  880. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  881. {
  882. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  883. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  884. if (unlikely(rsp->opcode != SRP_RSP)) {
  885. if (printk_ratelimit())
  886. dev_warn(evt_struct->hostdata->dev,
  887. "bad SRP RSP type %#02x\n", rsp->opcode);
  888. }
  889. if (cmnd) {
  890. cmnd->result |= rsp->status;
  891. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  892. memcpy(cmnd->sense_buffer,
  893. rsp->data,
  894. be32_to_cpu(rsp->sense_data_len));
  895. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  896. evt_struct,
  897. evt_struct->hostdata->dev);
  898. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  899. scsi_set_resid(cmnd,
  900. be32_to_cpu(rsp->data_out_res_cnt));
  901. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  902. scsi_set_resid(cmnd, be32_to_cpu(rsp->data_in_res_cnt));
  903. }
  904. if (evt_struct->cmnd_done)
  905. evt_struct->cmnd_done(cmnd);
  906. }
  907. /**
  908. * lun_from_dev: - Returns the lun of the scsi device
  909. * @dev: struct scsi_device
  910. *
  911. */
  912. static inline u16 lun_from_dev(struct scsi_device *dev)
  913. {
  914. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  915. }
  916. /**
  917. * ibmvscsi_queue: - The queuecommand function of the scsi template
  918. * @cmd: struct scsi_cmnd to be executed
  919. * @done: Callback function to be called when cmd is completed
  920. */
  921. static int ibmvscsi_queuecommand_lck(struct scsi_cmnd *cmnd,
  922. void (*done) (struct scsi_cmnd *))
  923. {
  924. struct srp_cmd *srp_cmd;
  925. struct srp_event_struct *evt_struct;
  926. struct srp_indirect_buf *indirect;
  927. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  928. u16 lun = lun_from_dev(cmnd->device);
  929. u8 out_fmt, in_fmt;
  930. cmnd->result = (DID_OK << 16);
  931. evt_struct = get_event_struct(&hostdata->pool);
  932. if (!evt_struct)
  933. return SCSI_MLQUEUE_HOST_BUSY;
  934. /* Set up the actual SRP IU */
  935. srp_cmd = &evt_struct->iu.srp.cmd;
  936. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  937. srp_cmd->opcode = SRP_CMD;
  938. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  939. int_to_scsilun(lun, &srp_cmd->lun);
  940. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  941. if (!firmware_has_feature(FW_FEATURE_CMO))
  942. sdev_printk(KERN_ERR, cmnd->device,
  943. "couldn't convert cmd to srp_cmd\n");
  944. free_event_struct(&hostdata->pool, evt_struct);
  945. return SCSI_MLQUEUE_HOST_BUSY;
  946. }
  947. init_event_struct(evt_struct,
  948. handle_cmd_rsp,
  949. VIOSRP_SRP_FORMAT,
  950. cmnd->request->timeout/HZ);
  951. evt_struct->cmnd = cmnd;
  952. evt_struct->cmnd_done = done;
  953. /* Fix up dma address of the buffer itself */
  954. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  955. out_fmt = srp_cmd->buf_fmt >> 4;
  956. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  957. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  958. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  959. indirect->table_desc.va == 0) {
  960. indirect->table_desc.va =
  961. cpu_to_be64(be64_to_cpu(evt_struct->crq.IU_data_ptr) +
  962. offsetof(struct srp_cmd, add_data) +
  963. offsetof(struct srp_indirect_buf, desc_list));
  964. }
  965. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  966. }
  967. static DEF_SCSI_QCMD(ibmvscsi_queuecommand)
  968. /* ------------------------------------------------------------
  969. * Routines for driver initialization
  970. */
  971. /**
  972. * map_persist_bufs: - Pre-map persistent data for adapter logins
  973. * @hostdata: ibmvscsi_host_data of host
  974. *
  975. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  976. * Return 1 on error, 0 on success.
  977. */
  978. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  979. {
  980. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  981. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  982. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  983. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  984. return 1;
  985. }
  986. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  987. &hostdata->madapter_info,
  988. sizeof(hostdata->madapter_info),
  989. DMA_BIDIRECTIONAL);
  990. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  991. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  992. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  993. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  994. return 1;
  995. }
  996. return 0;
  997. }
  998. /**
  999. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  1000. * @hostdata: ibmvscsi_host_data of host
  1001. *
  1002. * Unmap the capabilities and adapter info DMA buffers
  1003. */
  1004. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  1005. {
  1006. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  1007. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  1008. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  1009. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  1010. }
  1011. /**
  1012. * login_rsp: - Handle response to SRP login request
  1013. * @evt_struct: srp_event_struct with the response
  1014. *
  1015. * Used as a "done" callback by when sending srp_login. Gets called
  1016. * by ibmvscsi_handle_crq()
  1017. */
  1018. static void login_rsp(struct srp_event_struct *evt_struct)
  1019. {
  1020. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1021. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  1022. case SRP_LOGIN_RSP: /* it worked! */
  1023. break;
  1024. case SRP_LOGIN_REJ: /* refused! */
  1025. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  1026. evt_struct->xfer_iu->srp.login_rej.reason);
  1027. /* Login failed. */
  1028. atomic_set(&hostdata->request_limit, -1);
  1029. return;
  1030. default:
  1031. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  1032. evt_struct->xfer_iu->srp.login_rsp.opcode);
  1033. /* Login failed. */
  1034. atomic_set(&hostdata->request_limit, -1);
  1035. return;
  1036. }
  1037. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  1038. hostdata->client_migrated = 0;
  1039. /* Now we know what the real request-limit is.
  1040. * This value is set rather than added to request_limit because
  1041. * request_limit could have been set to -1 by this client.
  1042. */
  1043. atomic_set(&hostdata->request_limit,
  1044. be32_to_cpu(evt_struct->xfer_iu->srp.login_rsp.req_lim_delta));
  1045. /* If we had any pending I/Os, kick them */
  1046. scsi_unblock_requests(hostdata->host);
  1047. }
  1048. /**
  1049. * send_srp_login: - Sends the srp login
  1050. * @hostdata: ibmvscsi_host_data of host
  1051. *
  1052. * Returns zero if successful.
  1053. */
  1054. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  1055. {
  1056. int rc;
  1057. unsigned long flags;
  1058. struct srp_login_req *login;
  1059. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  1060. BUG_ON(!evt_struct);
  1061. init_event_struct(evt_struct, login_rsp,
  1062. VIOSRP_SRP_FORMAT, login_timeout);
  1063. login = &evt_struct->iu.srp.login_req;
  1064. memset(login, 0, sizeof(*login));
  1065. login->opcode = SRP_LOGIN_REQ;
  1066. login->req_it_iu_len = cpu_to_be32(sizeof(union srp_iu));
  1067. login->req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  1068. SRP_BUF_FORMAT_INDIRECT);
  1069. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1070. /* Start out with a request limit of 0, since this is negotiated in
  1071. * the login request we are just sending and login requests always
  1072. * get sent by the driver regardless of request_limit.
  1073. */
  1074. atomic_set(&hostdata->request_limit, 0);
  1075. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  1076. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1077. dev_info(hostdata->dev, "sent SRP login\n");
  1078. return rc;
  1079. };
  1080. /**
  1081. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  1082. * @evt_struct: srp_event_struct with the response
  1083. *
  1084. * Used as a "done" callback by when sending adapter_info.
  1085. */
  1086. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  1087. {
  1088. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1089. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  1090. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  1091. evt_struct->xfer_iu->mad.capabilities.common.status);
  1092. } else {
  1093. if (hostdata->caps.migration.common.server_support !=
  1094. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1095. dev_info(hostdata->dev, "Partition migration not supported\n");
  1096. if (client_reserve) {
  1097. if (hostdata->caps.reserve.common.server_support ==
  1098. cpu_to_be16(SERVER_SUPPORTS_CAP))
  1099. dev_info(hostdata->dev, "Client reserve enabled\n");
  1100. else
  1101. dev_info(hostdata->dev, "Client reserve not supported\n");
  1102. }
  1103. }
  1104. send_srp_login(hostdata);
  1105. }
  1106. /**
  1107. * send_mad_capabilities: - Sends the mad capabilities request
  1108. * and stores the result so it can be retrieved with
  1109. * @hostdata: ibmvscsi_host_data of host
  1110. */
  1111. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  1112. {
  1113. struct viosrp_capabilities *req;
  1114. struct srp_event_struct *evt_struct;
  1115. unsigned long flags;
  1116. struct device_node *of_node = hostdata->dev->of_node;
  1117. const char *location;
  1118. evt_struct = get_event_struct(&hostdata->pool);
  1119. BUG_ON(!evt_struct);
  1120. init_event_struct(evt_struct, capabilities_rsp,
  1121. VIOSRP_MAD_FORMAT, info_timeout);
  1122. req = &evt_struct->iu.mad.capabilities;
  1123. memset(req, 0, sizeof(*req));
  1124. hostdata->caps.flags = cpu_to_be32(CAP_LIST_SUPPORTED);
  1125. if (hostdata->client_migrated)
  1126. hostdata->caps.flags |= cpu_to_be32(CLIENT_MIGRATED);
  1127. strlcpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  1128. sizeof(hostdata->caps.name));
  1129. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1130. location = location ? location : dev_name(hostdata->dev);
  1131. strlcpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  1132. req->common.type = cpu_to_be32(VIOSRP_CAPABILITIES_TYPE);
  1133. req->buffer = cpu_to_be64(hostdata->caps_addr);
  1134. hostdata->caps.migration.common.cap_type =
  1135. cpu_to_be32(MIGRATION_CAPABILITIES);
  1136. hostdata->caps.migration.common.length =
  1137. cpu_to_be16(sizeof(hostdata->caps.migration));
  1138. hostdata->caps.migration.common.server_support =
  1139. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1140. hostdata->caps.migration.ecl = cpu_to_be32(1);
  1141. if (client_reserve) {
  1142. hostdata->caps.reserve.common.cap_type =
  1143. cpu_to_be32(RESERVATION_CAPABILITIES);
  1144. hostdata->caps.reserve.common.length =
  1145. cpu_to_be16(sizeof(hostdata->caps.reserve));
  1146. hostdata->caps.reserve.common.server_support =
  1147. cpu_to_be16(SERVER_SUPPORTS_CAP);
  1148. hostdata->caps.reserve.type =
  1149. cpu_to_be32(CLIENT_RESERVE_SCSI_2);
  1150. req->common.length =
  1151. cpu_to_be16(sizeof(hostdata->caps));
  1152. } else
  1153. req->common.length = cpu_to_be16(sizeof(hostdata->caps) -
  1154. sizeof(hostdata->caps.reserve));
  1155. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1156. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1157. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  1158. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1159. };
  1160. /**
  1161. * fast_fail_rsp: - Handle response to MAD enable fast fail
  1162. * @evt_struct: srp_event_struct with the response
  1163. *
  1164. * Used as a "done" callback by when sending enable fast fail. Gets called
  1165. * by ibmvscsi_handle_crq()
  1166. */
  1167. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  1168. {
  1169. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1170. u16 status = be16_to_cpu(evt_struct->xfer_iu->mad.fast_fail.common.status);
  1171. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  1172. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  1173. else if (status == VIOSRP_MAD_FAILED)
  1174. dev_err(hostdata->dev, "fast_fail request failed\n");
  1175. else if (status != VIOSRP_MAD_SUCCESS)
  1176. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  1177. send_mad_capabilities(hostdata);
  1178. }
  1179. /**
  1180. * init_host - Start host initialization
  1181. * @hostdata: ibmvscsi_host_data of host
  1182. *
  1183. * Returns zero if successful.
  1184. */
  1185. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  1186. {
  1187. int rc;
  1188. unsigned long flags;
  1189. struct viosrp_fast_fail *fast_fail_mad;
  1190. struct srp_event_struct *evt_struct;
  1191. if (!fast_fail) {
  1192. send_mad_capabilities(hostdata);
  1193. return 0;
  1194. }
  1195. evt_struct = get_event_struct(&hostdata->pool);
  1196. BUG_ON(!evt_struct);
  1197. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  1198. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  1199. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  1200. fast_fail_mad->common.type = cpu_to_be32(VIOSRP_ENABLE_FAST_FAIL);
  1201. fast_fail_mad->common.length = cpu_to_be16(sizeof(*fast_fail_mad));
  1202. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1203. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1204. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1205. return rc;
  1206. }
  1207. /**
  1208. * adapter_info_rsp: - Handle response to MAD adapter info request
  1209. * @evt_struct: srp_event_struct with the response
  1210. *
  1211. * Used as a "done" callback by when sending adapter_info. Gets called
  1212. * by ibmvscsi_handle_crq()
  1213. */
  1214. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  1215. {
  1216. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  1217. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  1218. dev_err(hostdata->dev, "error %d getting adapter info\n",
  1219. evt_struct->xfer_iu->mad.adapter_info.common.status);
  1220. } else {
  1221. dev_info(hostdata->dev, "host srp version: %s, "
  1222. "host partition %s (%d), OS %d, max io %u\n",
  1223. hostdata->madapter_info.srp_version,
  1224. hostdata->madapter_info.partition_name,
  1225. be32_to_cpu(hostdata->madapter_info.partition_number),
  1226. be32_to_cpu(hostdata->madapter_info.os_type),
  1227. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]));
  1228. if (hostdata->madapter_info.port_max_txu[0])
  1229. hostdata->host->max_sectors =
  1230. be32_to_cpu(hostdata->madapter_info.port_max_txu[0]) >> 9;
  1231. if (be32_to_cpu(hostdata->madapter_info.os_type) == SRP_MAD_OS_AIX &&
  1232. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  1233. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  1234. hostdata->madapter_info.srp_version);
  1235. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  1236. MAX_INDIRECT_BUFS);
  1237. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  1238. }
  1239. if (be32_to_cpu(hostdata->madapter_info.os_type) == SRP_MAD_OS_AIX) {
  1240. enable_fast_fail(hostdata);
  1241. return;
  1242. }
  1243. }
  1244. send_srp_login(hostdata);
  1245. }
  1246. /**
  1247. * send_mad_adapter_info: - Sends the mad adapter info request
  1248. * and stores the result so it can be retrieved with
  1249. * sysfs. We COULD consider causing a failure if the
  1250. * returned SRP version doesn't match ours.
  1251. * @hostdata: ibmvscsi_host_data of host
  1252. *
  1253. * Returns zero if successful.
  1254. */
  1255. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  1256. {
  1257. struct viosrp_adapter_info *req;
  1258. struct srp_event_struct *evt_struct;
  1259. unsigned long flags;
  1260. evt_struct = get_event_struct(&hostdata->pool);
  1261. BUG_ON(!evt_struct);
  1262. init_event_struct(evt_struct,
  1263. adapter_info_rsp,
  1264. VIOSRP_MAD_FORMAT,
  1265. info_timeout);
  1266. req = &evt_struct->iu.mad.adapter_info;
  1267. memset(req, 0x00, sizeof(*req));
  1268. req->common.type = cpu_to_be32(VIOSRP_ADAPTER_INFO_TYPE);
  1269. req->common.length = cpu_to_be16(sizeof(hostdata->madapter_info));
  1270. req->buffer = cpu_to_be64(hostdata->adapter_info_addr);
  1271. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1272. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1273. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  1274. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1275. };
  1276. /**
  1277. * init_adapter: Start virtual adapter initialization sequence
  1278. *
  1279. */
  1280. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  1281. {
  1282. send_mad_adapter_info(hostdata);
  1283. }
  1284. /**
  1285. * sync_completion: Signal that a synchronous command has completed
  1286. * Note that after returning from this call, the evt_struct is freed.
  1287. * the caller waiting on this completion shouldn't touch the evt_struct
  1288. * again.
  1289. */
  1290. static void sync_completion(struct srp_event_struct *evt_struct)
  1291. {
  1292. /* copy the response back */
  1293. if (evt_struct->sync_srp)
  1294. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1295. complete(&evt_struct->comp);
  1296. }
  1297. /**
  1298. * ibmvscsi_abort: Abort a command...from scsi host template
  1299. * send this over to the server and wait synchronously for the response
  1300. */
  1301. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1302. {
  1303. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1304. struct srp_tsk_mgmt *tsk_mgmt;
  1305. struct srp_event_struct *evt;
  1306. struct srp_event_struct *tmp_evt, *found_evt;
  1307. union viosrp_iu srp_rsp;
  1308. int rsp_rc;
  1309. unsigned long flags;
  1310. u16 lun = lun_from_dev(cmd->device);
  1311. unsigned long wait_switch = 0;
  1312. /* First, find this command in our sent list so we can figure
  1313. * out the correct tag
  1314. */
  1315. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1316. wait_switch = jiffies + (init_timeout * HZ);
  1317. do {
  1318. found_evt = NULL;
  1319. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1320. if (tmp_evt->cmnd == cmd) {
  1321. found_evt = tmp_evt;
  1322. break;
  1323. }
  1324. }
  1325. if (!found_evt) {
  1326. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1327. return SUCCESS;
  1328. }
  1329. evt = get_event_struct(&hostdata->pool);
  1330. if (evt == NULL) {
  1331. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1332. sdev_printk(KERN_ERR, cmd->device,
  1333. "failed to allocate abort event\n");
  1334. return FAILED;
  1335. }
  1336. init_event_struct(evt,
  1337. sync_completion,
  1338. VIOSRP_SRP_FORMAT,
  1339. abort_timeout);
  1340. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1341. /* Set up an abort SRP command */
  1342. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1343. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1344. int_to_scsilun(lun, &tsk_mgmt->lun);
  1345. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1346. tsk_mgmt->task_tag = (u64) found_evt;
  1347. evt->sync_srp = &srp_rsp;
  1348. init_completion(&evt->comp);
  1349. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1350. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1351. break;
  1352. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1353. msleep(10);
  1354. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1355. } while (time_before(jiffies, wait_switch));
  1356. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1357. if (rsp_rc != 0) {
  1358. sdev_printk(KERN_ERR, cmd->device,
  1359. "failed to send abort() event. rc=%d\n", rsp_rc);
  1360. return FAILED;
  1361. }
  1362. sdev_printk(KERN_INFO, cmd->device,
  1363. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1364. (((u64) lun) << 48), (u64) found_evt);
  1365. wait_for_completion(&evt->comp);
  1366. /* make sure we got a good response */
  1367. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1368. if (printk_ratelimit())
  1369. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1370. srp_rsp.srp.rsp.opcode);
  1371. return FAILED;
  1372. }
  1373. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1374. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1375. else
  1376. rsp_rc = srp_rsp.srp.rsp.status;
  1377. if (rsp_rc) {
  1378. if (printk_ratelimit())
  1379. sdev_printk(KERN_WARNING, cmd->device,
  1380. "abort code %d for task tag 0x%llx\n",
  1381. rsp_rc, tsk_mgmt->task_tag);
  1382. return FAILED;
  1383. }
  1384. /* Because we dropped the spinlock above, it's possible
  1385. * The event is no longer in our list. Make sure it didn't
  1386. * complete while we were aborting
  1387. */
  1388. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1389. found_evt = NULL;
  1390. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1391. if (tmp_evt->cmnd == cmd) {
  1392. found_evt = tmp_evt;
  1393. break;
  1394. }
  1395. }
  1396. if (found_evt == NULL) {
  1397. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1398. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1399. tsk_mgmt->task_tag);
  1400. return SUCCESS;
  1401. }
  1402. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1403. tsk_mgmt->task_tag);
  1404. cmd->result = (DID_ABORT << 16);
  1405. list_del(&found_evt->list);
  1406. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1407. found_evt->hostdata->dev);
  1408. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1409. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1410. atomic_inc(&hostdata->request_limit);
  1411. return SUCCESS;
  1412. }
  1413. /**
  1414. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1415. * template send this over to the server and wait synchronously for the
  1416. * response
  1417. */
  1418. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1419. {
  1420. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1421. struct srp_tsk_mgmt *tsk_mgmt;
  1422. struct srp_event_struct *evt;
  1423. struct srp_event_struct *tmp_evt, *pos;
  1424. union viosrp_iu srp_rsp;
  1425. int rsp_rc;
  1426. unsigned long flags;
  1427. u16 lun = lun_from_dev(cmd->device);
  1428. unsigned long wait_switch = 0;
  1429. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1430. wait_switch = jiffies + (init_timeout * HZ);
  1431. do {
  1432. evt = get_event_struct(&hostdata->pool);
  1433. if (evt == NULL) {
  1434. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1435. sdev_printk(KERN_ERR, cmd->device,
  1436. "failed to allocate reset event\n");
  1437. return FAILED;
  1438. }
  1439. init_event_struct(evt,
  1440. sync_completion,
  1441. VIOSRP_SRP_FORMAT,
  1442. reset_timeout);
  1443. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1444. /* Set up a lun reset SRP command */
  1445. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1446. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1447. int_to_scsilun(lun, &tsk_mgmt->lun);
  1448. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1449. evt->sync_srp = &srp_rsp;
  1450. init_completion(&evt->comp);
  1451. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1452. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1453. break;
  1454. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1455. msleep(10);
  1456. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1457. } while (time_before(jiffies, wait_switch));
  1458. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1459. if (rsp_rc != 0) {
  1460. sdev_printk(KERN_ERR, cmd->device,
  1461. "failed to send reset event. rc=%d\n", rsp_rc);
  1462. return FAILED;
  1463. }
  1464. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1465. (((u64) lun) << 48));
  1466. wait_for_completion(&evt->comp);
  1467. /* make sure we got a good response */
  1468. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1469. if (printk_ratelimit())
  1470. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1471. srp_rsp.srp.rsp.opcode);
  1472. return FAILED;
  1473. }
  1474. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1475. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1476. else
  1477. rsp_rc = srp_rsp.srp.rsp.status;
  1478. if (rsp_rc) {
  1479. if (printk_ratelimit())
  1480. sdev_printk(KERN_WARNING, cmd->device,
  1481. "reset code %d for task tag 0x%llx\n",
  1482. rsp_rc, tsk_mgmt->task_tag);
  1483. return FAILED;
  1484. }
  1485. /* We need to find all commands for this LUN that have not yet been
  1486. * responded to, and fail them with DID_RESET
  1487. */
  1488. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1489. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1490. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1491. if (tmp_evt->cmnd)
  1492. tmp_evt->cmnd->result = (DID_RESET << 16);
  1493. list_del(&tmp_evt->list);
  1494. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1495. tmp_evt->hostdata->dev);
  1496. free_event_struct(&tmp_evt->hostdata->pool,
  1497. tmp_evt);
  1498. atomic_inc(&hostdata->request_limit);
  1499. if (tmp_evt->cmnd_done)
  1500. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1501. else if (tmp_evt->done)
  1502. tmp_evt->done(tmp_evt);
  1503. }
  1504. }
  1505. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1506. return SUCCESS;
  1507. }
  1508. /**
  1509. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1510. * @cmd: struct scsi_cmnd having problems
  1511. */
  1512. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1513. {
  1514. unsigned long wait_switch = 0;
  1515. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1516. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1517. ibmvscsi_reset_host(hostdata);
  1518. for (wait_switch = jiffies + (init_timeout * HZ);
  1519. time_before(jiffies, wait_switch) &&
  1520. atomic_read(&hostdata->request_limit) < 2;) {
  1521. msleep(10);
  1522. }
  1523. if (atomic_read(&hostdata->request_limit) <= 0)
  1524. return FAILED;
  1525. return SUCCESS;
  1526. }
  1527. /**
  1528. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1529. * @crq: Command/Response queue
  1530. * @hostdata: ibmvscsi_host_data of host
  1531. *
  1532. */
  1533. static void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1534. struct ibmvscsi_host_data *hostdata)
  1535. {
  1536. long rc;
  1537. unsigned long flags;
  1538. /* The hypervisor copies our tag value here so no byteswapping */
  1539. struct srp_event_struct *evt_struct =
  1540. (__force struct srp_event_struct *)crq->IU_data_ptr;
  1541. switch (crq->valid) {
  1542. case VIOSRP_CRQ_INIT_RSP: /* initialization */
  1543. switch (crq->format) {
  1544. case VIOSRP_CRQ_INIT: /* Initialization message */
  1545. dev_info(hostdata->dev, "partner initialized\n");
  1546. /* Send back a response */
  1547. rc = ibmvscsi_send_crq(hostdata, 0xC002000000000000LL, 0);
  1548. if (rc == 0) {
  1549. /* Now login */
  1550. init_adapter(hostdata);
  1551. } else {
  1552. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1553. }
  1554. break;
  1555. case VIOSRP_CRQ_INIT_COMPLETE: /* Initialization response */
  1556. dev_info(hostdata->dev, "partner initialization complete\n");
  1557. /* Now login */
  1558. init_adapter(hostdata);
  1559. break;
  1560. default:
  1561. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1562. }
  1563. return;
  1564. case VIOSRP_CRQ_XPORT_EVENT: /* Hypervisor telling us the connection is closed */
  1565. scsi_block_requests(hostdata->host);
  1566. atomic_set(&hostdata->request_limit, 0);
  1567. if (crq->format == 0x06) {
  1568. /* We need to re-setup the interpartition connection */
  1569. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1570. hostdata->client_migrated = 1;
  1571. hostdata->reenable_crq = 1;
  1572. purge_requests(hostdata, DID_REQUEUE);
  1573. wake_up(&hostdata->work_wait_q);
  1574. } else {
  1575. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1576. crq->format);
  1577. ibmvscsi_reset_host(hostdata);
  1578. }
  1579. return;
  1580. case VIOSRP_CRQ_CMD_RSP: /* real payload */
  1581. break;
  1582. default:
  1583. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1584. crq->valid);
  1585. return;
  1586. }
  1587. /* The only kind of payload CRQs we should get are responses to
  1588. * things we send. Make sure this response is to something we
  1589. * actually sent
  1590. */
  1591. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1592. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1593. evt_struct);
  1594. return;
  1595. }
  1596. if (atomic_read(&evt_struct->free)) {
  1597. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1598. evt_struct);
  1599. return;
  1600. }
  1601. if (crq->format == VIOSRP_SRP_FORMAT)
  1602. atomic_add(be32_to_cpu(evt_struct->xfer_iu->srp.rsp.req_lim_delta),
  1603. &hostdata->request_limit);
  1604. del_timer(&evt_struct->timer);
  1605. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1606. evt_struct->cmnd->result = DID_ERROR << 16;
  1607. if (evt_struct->done)
  1608. evt_struct->done(evt_struct);
  1609. else
  1610. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1611. /*
  1612. * Lock the host_lock before messing with these structures, since we
  1613. * are running in a task context
  1614. */
  1615. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1616. list_del(&evt_struct->list);
  1617. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1618. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1619. }
  1620. /**
  1621. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1622. * @sdev: struct scsi_device device to configure
  1623. *
  1624. * Enable allow_restart for a device if it is a disk. Adjust the
  1625. * queue_depth here also as is required by the documentation for
  1626. * struct scsi_host_template.
  1627. */
  1628. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1629. {
  1630. struct Scsi_Host *shost = sdev->host;
  1631. unsigned long lock_flags = 0;
  1632. spin_lock_irqsave(shost->host_lock, lock_flags);
  1633. if (sdev->type == TYPE_DISK) {
  1634. sdev->allow_restart = 1;
  1635. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1636. }
  1637. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1638. return 0;
  1639. }
  1640. /**
  1641. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1642. * @sdev: scsi device struct
  1643. * @qdepth: depth to set
  1644. * @reason: calling context
  1645. *
  1646. * Return value:
  1647. * actual depth set
  1648. **/
  1649. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1650. {
  1651. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1652. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1653. return scsi_change_queue_depth(sdev, qdepth);
  1654. }
  1655. /* ------------------------------------------------------------
  1656. * sysfs attributes
  1657. */
  1658. static ssize_t show_host_vhost_loc(struct device *dev,
  1659. struct device_attribute *attr, char *buf)
  1660. {
  1661. struct Scsi_Host *shost = class_to_shost(dev);
  1662. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1663. int len;
  1664. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1665. hostdata->caps.loc);
  1666. return len;
  1667. }
  1668. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1669. .attr = {
  1670. .name = "vhost_loc",
  1671. .mode = S_IRUGO,
  1672. },
  1673. .show = show_host_vhost_loc,
  1674. };
  1675. static ssize_t show_host_vhost_name(struct device *dev,
  1676. struct device_attribute *attr, char *buf)
  1677. {
  1678. struct Scsi_Host *shost = class_to_shost(dev);
  1679. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1680. int len;
  1681. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1682. hostdata->caps.name);
  1683. return len;
  1684. }
  1685. static struct device_attribute ibmvscsi_host_vhost_name = {
  1686. .attr = {
  1687. .name = "vhost_name",
  1688. .mode = S_IRUGO,
  1689. },
  1690. .show = show_host_vhost_name,
  1691. };
  1692. static ssize_t show_host_srp_version(struct device *dev,
  1693. struct device_attribute *attr, char *buf)
  1694. {
  1695. struct Scsi_Host *shost = class_to_shost(dev);
  1696. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1697. int len;
  1698. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1699. hostdata->madapter_info.srp_version);
  1700. return len;
  1701. }
  1702. static struct device_attribute ibmvscsi_host_srp_version = {
  1703. .attr = {
  1704. .name = "srp_version",
  1705. .mode = S_IRUGO,
  1706. },
  1707. .show = show_host_srp_version,
  1708. };
  1709. static ssize_t show_host_partition_name(struct device *dev,
  1710. struct device_attribute *attr,
  1711. char *buf)
  1712. {
  1713. struct Scsi_Host *shost = class_to_shost(dev);
  1714. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1715. int len;
  1716. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1717. hostdata->madapter_info.partition_name);
  1718. return len;
  1719. }
  1720. static struct device_attribute ibmvscsi_host_partition_name = {
  1721. .attr = {
  1722. .name = "partition_name",
  1723. .mode = S_IRUGO,
  1724. },
  1725. .show = show_host_partition_name,
  1726. };
  1727. static ssize_t show_host_partition_number(struct device *dev,
  1728. struct device_attribute *attr,
  1729. char *buf)
  1730. {
  1731. struct Scsi_Host *shost = class_to_shost(dev);
  1732. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1733. int len;
  1734. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1735. be32_to_cpu(hostdata->madapter_info.partition_number));
  1736. return len;
  1737. }
  1738. static struct device_attribute ibmvscsi_host_partition_number = {
  1739. .attr = {
  1740. .name = "partition_number",
  1741. .mode = S_IRUGO,
  1742. },
  1743. .show = show_host_partition_number,
  1744. };
  1745. static ssize_t show_host_mad_version(struct device *dev,
  1746. struct device_attribute *attr, char *buf)
  1747. {
  1748. struct Scsi_Host *shost = class_to_shost(dev);
  1749. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1750. int len;
  1751. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1752. be32_to_cpu(hostdata->madapter_info.mad_version));
  1753. return len;
  1754. }
  1755. static struct device_attribute ibmvscsi_host_mad_version = {
  1756. .attr = {
  1757. .name = "mad_version",
  1758. .mode = S_IRUGO,
  1759. },
  1760. .show = show_host_mad_version,
  1761. };
  1762. static ssize_t show_host_os_type(struct device *dev,
  1763. struct device_attribute *attr, char *buf)
  1764. {
  1765. struct Scsi_Host *shost = class_to_shost(dev);
  1766. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1767. int len;
  1768. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1769. be32_to_cpu(hostdata->madapter_info.os_type));
  1770. return len;
  1771. }
  1772. static struct device_attribute ibmvscsi_host_os_type = {
  1773. .attr = {
  1774. .name = "os_type",
  1775. .mode = S_IRUGO,
  1776. },
  1777. .show = show_host_os_type,
  1778. };
  1779. static ssize_t show_host_config(struct device *dev,
  1780. struct device_attribute *attr, char *buf)
  1781. {
  1782. return 0;
  1783. }
  1784. static struct device_attribute ibmvscsi_host_config = {
  1785. .attr = {
  1786. .name = "config",
  1787. .mode = S_IRUGO,
  1788. },
  1789. .show = show_host_config,
  1790. };
  1791. static struct device_attribute *ibmvscsi_attrs[] = {
  1792. &ibmvscsi_host_vhost_loc,
  1793. &ibmvscsi_host_vhost_name,
  1794. &ibmvscsi_host_srp_version,
  1795. &ibmvscsi_host_partition_name,
  1796. &ibmvscsi_host_partition_number,
  1797. &ibmvscsi_host_mad_version,
  1798. &ibmvscsi_host_os_type,
  1799. &ibmvscsi_host_config,
  1800. NULL
  1801. };
  1802. /* ------------------------------------------------------------
  1803. * SCSI driver registration
  1804. */
  1805. static struct scsi_host_template driver_template = {
  1806. .module = THIS_MODULE,
  1807. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1808. .proc_name = "ibmvscsi",
  1809. .queuecommand = ibmvscsi_queuecommand,
  1810. .eh_timed_out = srp_timed_out,
  1811. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1812. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1813. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1814. .slave_configure = ibmvscsi_slave_configure,
  1815. .change_queue_depth = ibmvscsi_change_queue_depth,
  1816. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1817. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1818. .this_id = -1,
  1819. .sg_tablesize = SG_ALL,
  1820. .use_clustering = ENABLE_CLUSTERING,
  1821. .shost_attrs = ibmvscsi_attrs,
  1822. };
  1823. /**
  1824. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1825. *
  1826. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1827. *
  1828. * Return value:
  1829. * Number of bytes of IO data the driver will need to perform well.
  1830. */
  1831. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1832. {
  1833. /* iu_storage data allocated in initialize_event_pool */
  1834. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1835. /* add io space for sg data */
  1836. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1837. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1838. return desired_io;
  1839. }
  1840. static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  1841. {
  1842. int rc;
  1843. char *action = "reset";
  1844. if (hostdata->reset_crq) {
  1845. smp_rmb();
  1846. hostdata->reset_crq = 0;
  1847. rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  1848. if (!rc)
  1849. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1850. vio_enable_interrupts(to_vio_dev(hostdata->dev));
  1851. } else if (hostdata->reenable_crq) {
  1852. smp_rmb();
  1853. action = "enable";
  1854. rc = ibmvscsi_reenable_crq_queue(&hostdata->queue, hostdata);
  1855. hostdata->reenable_crq = 0;
  1856. if (!rc)
  1857. rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
  1858. } else
  1859. return;
  1860. if (rc) {
  1861. atomic_set(&hostdata->request_limit, -1);
  1862. dev_err(hostdata->dev, "error after %s\n", action);
  1863. }
  1864. scsi_unblock_requests(hostdata->host);
  1865. }
  1866. static int ibmvscsi_work_to_do(struct ibmvscsi_host_data *hostdata)
  1867. {
  1868. if (kthread_should_stop())
  1869. return 1;
  1870. else if (hostdata->reset_crq) {
  1871. smp_rmb();
  1872. return 1;
  1873. } else if (hostdata->reenable_crq) {
  1874. smp_rmb();
  1875. return 1;
  1876. }
  1877. return 0;
  1878. }
  1879. static int ibmvscsi_work(void *data)
  1880. {
  1881. struct ibmvscsi_host_data *hostdata = data;
  1882. int rc;
  1883. set_user_nice(current, MIN_NICE);
  1884. while (1) {
  1885. rc = wait_event_interruptible(hostdata->work_wait_q,
  1886. ibmvscsi_work_to_do(hostdata));
  1887. BUG_ON(rc);
  1888. if (kthread_should_stop())
  1889. break;
  1890. ibmvscsi_do_work(hostdata);
  1891. }
  1892. return 0;
  1893. }
  1894. /**
  1895. * Called by bus code for each adapter
  1896. */
  1897. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1898. {
  1899. struct ibmvscsi_host_data *hostdata;
  1900. struct Scsi_Host *host;
  1901. struct device *dev = &vdev->dev;
  1902. struct srp_rport_identifiers ids;
  1903. struct srp_rport *rport;
  1904. unsigned long wait_switch = 0;
  1905. int rc;
  1906. dev_set_drvdata(&vdev->dev, NULL);
  1907. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1908. if (!host) {
  1909. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1910. goto scsi_host_alloc_failed;
  1911. }
  1912. host->transportt = ibmvscsi_transport_template;
  1913. hostdata = shost_priv(host);
  1914. memset(hostdata, 0x00, sizeof(*hostdata));
  1915. INIT_LIST_HEAD(&hostdata->sent);
  1916. init_waitqueue_head(&hostdata->work_wait_q);
  1917. hostdata->host = host;
  1918. hostdata->dev = dev;
  1919. atomic_set(&hostdata->request_limit, -1);
  1920. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1921. if (map_persist_bufs(hostdata)) {
  1922. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1923. goto persist_bufs_failed;
  1924. }
  1925. hostdata->work_thread = kthread_run(ibmvscsi_work, hostdata, "%s_%d",
  1926. "ibmvscsi", host->host_no);
  1927. if (IS_ERR(hostdata->work_thread)) {
  1928. dev_err(&vdev->dev, "couldn't initialize kthread. rc=%ld\n",
  1929. PTR_ERR(hostdata->work_thread));
  1930. goto init_crq_failed;
  1931. }
  1932. rc = ibmvscsi_init_crq_queue(&hostdata->queue, hostdata, max_events);
  1933. if (rc != 0 && rc != H_RESOURCE) {
  1934. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1935. goto kill_kthread;
  1936. }
  1937. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1938. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1939. goto init_pool_failed;
  1940. }
  1941. host->max_lun = IBMVSCSI_MAX_LUN;
  1942. host->max_id = max_id;
  1943. host->max_channel = max_channel;
  1944. host->max_cmd_len = 16;
  1945. dev_info(dev,
  1946. "Maximum ID: %d Maximum LUN: %llu Maximum Channel: %d\n",
  1947. host->max_id, host->max_lun, host->max_channel);
  1948. if (scsi_add_host(hostdata->host, hostdata->dev))
  1949. goto add_host_failed;
  1950. /* we don't have a proper target_port_id so let's use the fake one */
  1951. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1952. sizeof(ids.port_id));
  1953. ids.roles = SRP_RPORT_ROLE_TARGET;
  1954. rport = srp_rport_add(host, &ids);
  1955. if (IS_ERR(rport))
  1956. goto add_srp_port_failed;
  1957. /* Try to send an initialization message. Note that this is allowed
  1958. * to fail if the other end is not acive. In that case we don't
  1959. * want to scan
  1960. */
  1961. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1962. || rc == H_RESOURCE) {
  1963. /*
  1964. * Wait around max init_timeout secs for the adapter to finish
  1965. * initializing. When we are done initializing, we will have a
  1966. * valid request_limit. We don't want Linux scanning before
  1967. * we are ready.
  1968. */
  1969. for (wait_switch = jiffies + (init_timeout * HZ);
  1970. time_before(jiffies, wait_switch) &&
  1971. atomic_read(&hostdata->request_limit) < 2;) {
  1972. msleep(10);
  1973. }
  1974. /* if we now have a valid request_limit, initiate a scan */
  1975. if (atomic_read(&hostdata->request_limit) > 0)
  1976. scsi_scan_host(host);
  1977. }
  1978. dev_set_drvdata(&vdev->dev, hostdata);
  1979. spin_lock(&ibmvscsi_driver_lock);
  1980. list_add_tail(&hostdata->host_list, &ibmvscsi_head);
  1981. spin_unlock(&ibmvscsi_driver_lock);
  1982. return 0;
  1983. add_srp_port_failed:
  1984. scsi_remove_host(hostdata->host);
  1985. add_host_failed:
  1986. release_event_pool(&hostdata->pool, hostdata);
  1987. init_pool_failed:
  1988. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_events);
  1989. kill_kthread:
  1990. kthread_stop(hostdata->work_thread);
  1991. init_crq_failed:
  1992. unmap_persist_bufs(hostdata);
  1993. persist_bufs_failed:
  1994. scsi_host_put(host);
  1995. scsi_host_alloc_failed:
  1996. return -1;
  1997. }
  1998. static int ibmvscsi_remove(struct vio_dev *vdev)
  1999. {
  2000. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  2001. unsigned long flags;
  2002. srp_remove_host(hostdata->host);
  2003. scsi_remove_host(hostdata->host);
  2004. purge_requests(hostdata, DID_ERROR);
  2005. spin_lock_irqsave(hostdata->host->host_lock, flags);
  2006. release_event_pool(&hostdata->pool, hostdata);
  2007. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  2008. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  2009. max_events);
  2010. kthread_stop(hostdata->work_thread);
  2011. unmap_persist_bufs(hostdata);
  2012. spin_lock(&ibmvscsi_driver_lock);
  2013. list_del(&hostdata->host_list);
  2014. spin_unlock(&ibmvscsi_driver_lock);
  2015. scsi_host_put(hostdata->host);
  2016. return 0;
  2017. }
  2018. /**
  2019. * ibmvscsi_resume: Resume from suspend
  2020. * @dev: device struct
  2021. *
  2022. * We may have lost an interrupt across suspend/resume, so kick the
  2023. * interrupt handler
  2024. */
  2025. static int ibmvscsi_resume(struct device *dev)
  2026. {
  2027. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  2028. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  2029. tasklet_schedule(&hostdata->srp_task);
  2030. return 0;
  2031. }
  2032. /**
  2033. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  2034. * support.
  2035. */
  2036. static const struct vio_device_id ibmvscsi_device_table[] = {
  2037. {"vscsi", "IBM,v-scsi"},
  2038. { "", "" }
  2039. };
  2040. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  2041. static const struct dev_pm_ops ibmvscsi_pm_ops = {
  2042. .resume = ibmvscsi_resume
  2043. };
  2044. static struct vio_driver ibmvscsi_driver = {
  2045. .id_table = ibmvscsi_device_table,
  2046. .probe = ibmvscsi_probe,
  2047. .remove = ibmvscsi_remove,
  2048. .get_desired_dma = ibmvscsi_get_desired_dma,
  2049. .name = "ibmvscsi",
  2050. .pm = &ibmvscsi_pm_ops,
  2051. };
  2052. static struct srp_function_template ibmvscsi_transport_functions = {
  2053. };
  2054. int __init ibmvscsi_module_init(void)
  2055. {
  2056. int ret;
  2057. /* Ensure we have two requests to do error recovery */
  2058. driver_template.can_queue = max_requests;
  2059. max_events = max_requests + 2;
  2060. if (!firmware_has_feature(FW_FEATURE_VIO))
  2061. return -ENODEV;
  2062. ibmvscsi_transport_template =
  2063. srp_attach_transport(&ibmvscsi_transport_functions);
  2064. if (!ibmvscsi_transport_template)
  2065. return -ENOMEM;
  2066. ret = vio_register_driver(&ibmvscsi_driver);
  2067. if (ret)
  2068. srp_release_transport(ibmvscsi_transport_template);
  2069. return ret;
  2070. }
  2071. void __exit ibmvscsi_module_exit(void)
  2072. {
  2073. vio_unregister_driver(&ibmvscsi_driver);
  2074. srp_release_transport(ibmvscsi_transport_template);
  2075. }
  2076. module_init(ibmvscsi_module_init);
  2077. module_exit(ibmvscsi_module_exit);